Once upon a time in Time-Land
In a mystical TimeLand, a person's health and wealth is measured in terms of time(seconds) left. Suppose a person there has 24x60x60 = 86400 seconds left, then he would live for another 1 day. A person dies when his time left becomes 0. Some time-amount can be borrowed from other person, or time-banks. Some time-amount can also be lend to another person, or can be used to buy stuffs.
Our hero Mr X, is in critical condition, has very less time left.
Today's the inaugural day of a new time-bank. So they are giving away free time-amount worth 1000 years.
Bank released N slips, A[1], A[2], .... A[N]. Each slip has a time-amount(can be +ve as well as -ve).
A person can pick any number of slips(even none, or all of them, or some of them) out of the N slips. But bank introduced a restriction, they announced one more number K. Restriction is that, if a person picks a slip A[i], then the next slip that he can choose to pick will be A[i+K+1]. It means there should be a difference of atleast K between the indices of slips picked.
Now slip(s) should be picked in such a way that their sum results in maximum positive time-amount sum possible with the given restriction.
If you predict the maximum positive sum possible, then you win.
Mr X has asked for your help. Help him win the lottery, and make it quick!
Input Format:
First line of the test file contains single number T, the number of test cases to follow.
Each test case consists of two lines.
First line contains two numbers N and K , separated by a space. Second line contains the N numbers A[1], A[2] ..... A[N]separated by space.
First line of the test file contains single number T, the number of test cases to follow.
Each test case consists of two lines.
First line contains two numbers N and K , separated by a space. Second line contains the N numbers A[1], A[2] ..... A[N]separated by space.
Output Format:
For every test case, output in a single line the maximum positive sum possible, that is output for the case.
For every test case, output in a single line the maximum positive sum possible, that is output for the case.
Constraints:
T ≤ 250
N ≤ 10000
-109 ≤ A[i] ≤ 109
0 ≤ K ≤ N-1
T ≤ 250
N ≤ 10000
-109 ≤ A[i] ≤ 109
0 ≤ K ≤ N-1
Sample Input
(Plaintext Link)2 10 1 1 2 -3 -5 4 6 -3 2 -1 2 10 2 1 2 -3 -5 4 6 -3 2 -1 2
Sample Output
(Plaintext Link)12 10
Explanation
1st Case: We can take slips { A[2]=2, A[6]=6, A[8]=2, A[10]=2 }, slips are atleast 1 indices apart this makes maximum sum, A[2]+A[6]+A[8]+A[10]=12
2nd Case: We can take slips { A[2]=2, A[6]=6, A[10]=2 }, slips are atleast 2 indices apart this makes maximum sum, A[2]+A[6]+A[10]=10
-----------------------------------------editorial------------------------------------------------------------------------------------------------------
Just like all dp problems we first need to define state here.
We take the state as
Base case will be when
f (idx)
where f (idx)
will tell us the current maximum until index idx.Base case will be when
f (idx >= N)
which is 0 where N is length of the given cost array.
It is a knapsack style dp, where for every index we make a choice whether to take it or leave it.
If we leave it, next state will be
If we leave it, next state will be
f (idx + 1)
. If we take it, next state will be f (idx + K + 1)
and we add the cost of ith item to the overall cost.
--------------------------------------------------------code-------------------------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
lli dp[400000];
int mark[100000];
int n,k;
lli arr[100000];
lli solve(int index)
{
if(index>=n) return 0;
if(mark[index]!=0)
{
return dp[index];
}
else
{
mark[index]=1;
lli ret=0;
dp[index]=max(solve(index+1),solve(index+k+1)+arr[index]);
return dp[index];
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>k;
memset(dp,0,sizeof(dp));
memset(mark,0,sizeof(mark));
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
lli ans=solve(0);
cout<<ans<<endl;
}
}
No comments:
Post a Comment