Search This Blog

Saturday, 16 July 2016

HackerEarth- Once Upon A Time in Time-Land (Dynamic Problem Solution)

Once Upon A Time in Time-Land (HackerEarth)
Dynamic Programming, Easy,Algorithms :

Solution:-

#include<bits/stdc++.h>
using namespace std;
#define f(i,a,b) for(int i=a;i<=b;i++)
#define MAX 10010
#define ll long long
ll arr[MAX];

int main()
{
    int tc;
    int N,K,i,j;
    cin>>tc;
    while(tc--)
    {
        ll max_val=INT_MIN;
        ll Answer=INT_MIN;
        cin>>N>>K;
        f(i,1,N)
        {
            cin>>arr[i];
            if(arr[i]>Answer)
                Answer=arr[i];
        }
        max_val=arr[1];
        j=2;

        f(i,K+2,N)
        {
            if(max_val>0)
            {
                arr[i]+=max_val;
                if(arr[i]>Answer)
                    Answer=arr[i];
            }
            if(arr[j]>max_val)
                max_val=arr[j];
            j++;
        }
        if(Answer<0)
        Answer=0;
        cout<<Answer<<endl;
    }
    return 0;

}

No comments:

Post a Comment