indiahacks-pb-3

In a mystical Land, a person's health and wealth is measured in terms of time(in 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 things.

Our hero Deadpool, 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.

Deadpool 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.

Output Format:
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

SAMPLE INPUT
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
12
10

Difficulty: EASY
Tags: Dynamic programming
Skill types: Dynamic programming

Starter code:

Solutions:
for _ in range(int(input())):
	n, k = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	pre = [None]*n
	pre[0] = max(arr[0], 0)
	for i in range(1, n):
		if i <= k:
			pre[i] = max(pre[i - 1], arr[i])
		else:
			pre[i] = max(pre[i - 1], arr[i] + pre[i - k - 1])
	print(pre[n - 1])


for _ in range(int(input())):
	n, k = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	dp = [0]*n
	for i in range(k + 1):
		dp[i] = max(arr[i], dp[i - 1])
	for i in range(k + 1, n):
		dp[i] = max(arr[i] + dp[i - k - 1], dp[i - 1])
	print(dp[-1])


for _ in range(eval(input())):
	n, k = list(map(int, input().split()))
	slips= list(map(int, input().split()))
	sums = [0] * n
	sums[0] = max(slips[0], 0)
	for i in range(1, n):
		if slips[i] < 0:
			sums[i] = sums[i-1]
		elif (i - k -1 < 0):
			sums[i] = max(sums[i-1], slips[i])
		else:
			sums[i] = max(sums[i-1], slips[i] + sums[i-k-1])
	print(sums[n-1])        


from sys import stdin
def main():
	nextitem = iter(map(int, stdin.read().split())).__next__
	for _ in range(nextitem()):
		n, k = nextitem(), nextitem() + 1
		arr = [nextitem() for _ in range(n)]
		dp = [arr[0] if arr[0] > 0 else 0]
		for x in range(1, k):
			dp.append(max(dp[-1], arr[x]))
		for x in range(k, n):
			dp.append(max(dp[-1], arr[x] + dp[-k]))
		print(dp[-1])

main()


for _ in range(int(input())):
	ns, ks = input().split()
	n, k = int(ns), int(ks)
	slips = list(map(int, input().split()))
	slips[0] = max(0, slips[0])
	for x in range(1, n):
		if x > k:
			slips[x] = max(slips[x - 1], slips[x - k - 1] + slips[x])
		else:
			slips[x] = max(slips[x - 1], slips[x])
	print(slips[-1])