A conveyor belt scans n parcels in order. Parcel i has an integer weight (a weight may be negative, representing a rebate). You must choose a contiguous run of exactly k parcels and report the largest possible total weight over any such run.
Line 1: two integers n and k.
Line 2: n space-separated integers, the parcel weights.
A single integer: the maximum sum of any k consecutive parcel weights.
Example 1
Input
5 2 4 -1 2 7 3
Expected
10
Explanation
The best 2-parcel run is the last two parcels: 7 + 3 = 10, which beats every other run of length 2.
Example 2
Input
4 4 -1 -2 -3 -4
Expected
-10
Explanation
k equals n, so the only possible run is the whole belt: -1 + -2 + -3 + -4 = -10.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →