Potato wants to burn Po The Sloth's orchard (because Po owes him money and hasn't returned it to him for 2 weeks).
Po the Sloth's orchard contains a row of N trees, labelled from 1 to N from left to right. Potato has a flamethrower. This flamethrower can destroy a consecutive sequence of at most K trees. He can choose to destroy no trees at all.
Potato wants to cause as much damage as possible. He knows the cost of planting each tree. This can be negative because these trees have CCTVs on them, which means Potato will get sued and will lose money! Potato wants to aim his flamethrower in such a way that the sum of costs of trees he destroys is as high as possible. He wonders what this cost is.
As this problem deals with large outputs, you are strongly advised to insert these 2 lines of code at the start of your main()
function:
ios_base::sync_with_stdio(false);
cin.tie(NULL);
The first line contains two integers, N and K.
The following line contains N integers. The ith integer denotes the cost of the tree with label i.
Output the maximum cost of the trees Potato can damage.
1 ≤ K ≤ N ≤ 107
-109 ≤ Ci ≤ 109, where Ci denotes the cost of the ith tree.
Subtask 1 (13%): 1 ≤ K ≤ N ≤ 1000, Ci is guaranteed to be positive.
Subtask 2 (18%): 1 ≤ K ≤ N ≤ 1000
Subtask 3 (27%): 1 ≤ K ≤ N ≤ 106, Ci is guaranteed to be positive.
Subtask 4 (14%): 1 ≤ K ≤ N ≤ 106
Subtask 5 (28%): No additional constraints
Subtask 6 (0%): Sample Testcases
6 3 5 8 9 2 4 1
22
Potato can destroy Trees 1 to 3 to obtain a total cost of 22. It can be proven that there exists no contiguous subarray with length ≤ 3 that has a sum of more than 22.
5 2 10 -4 -5 3 0
10
Potato can destroy Tree 1 to obtain a total cost of 10. Note that Potato does not need to destroy 2 trees, he just needs to destroy up to 2 trees.
Subtask | Score |
---|---|
1 | 13 |
2 | 18 |
3 | 27 |
4 | 14 |
5 | 28 |
6 | 0 |