Search This Blog

Monday, 18 July 2016

HackerEarth - Choosing the Judges - (Dynamic Programming)

Choosing the Judges (HackerEarth)(Dynamic Programming)

Solution:-

#include<bits/stdc++.h>
using namespace std;
#define MAX_LEN 10010
#define ll long long
ll arr[MAX_LEN],dp[MAX_LEN];
ll g_max,N;

ll cal_max()
{
    dp[0]=arr[0];
    dp[1]=max(dp[0],arr[1]);

    g_max=dp[1];

    for(int i=2;i<N;i++)
    {
        dp[i]=max(dp[i-2]+arr[i],dp[i-1]);
        if(dp[i]>g_max)
            g_max=dp[i];
    }

    return g_max;
}


int main()
{
    int tc;
    cin>>tc;
    for(int j=1;j<=tc;j++)
    {
        cin>>N;
        g_max=0;
        for(int i=0;i<N;i++)
            cin>>arr[i];

        cout<<"Case "<<j<<": "<<cal_max()<<endl;
    }
    return 0;

No comments:

Post a Comment