Bowling for Numbers++
Time limit1sMemory limit128 MB
Choose at most k windows of length w, possibly overlapping beyond the row ends, so the sum of the covered pins is as large as possible.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum, Array, Greedy
- Solved
- No attempts yet
Problem
You are playing a game called Bowling for Numbers. A row of bowling pins stands in a line. The -th pin has an integer score printed on it — the points you gain by knocking that pin over. Some pins are penalty pins whose score is negative, so knocking one over lowers your total.
You are given bowling balls. Each ball is wide enough to knock over up to consecutive pins: a single throw covers a window of adjacent positions and knocks over every pin still standing inside that window. A pin's score is counted at most once, no matter how many balls pass over it.
A throw's window may extend past the left or right end of the row (everything beyond the ends is empty space), and it may pass through gaps left by pins that earlier throws already knocked down. You can exploit this: by aiming a throw so that some of its positions land on empty space — off an end of the row, or on positions a previous throw already cleared — you can knock over fewer than pins and skip over penalty pins. A ball may even be thrown so that it knocks over nothing.
Throw your balls to maximize the total score of the knocked-over pins. You do not have to use every ball.
Equivalently: the pins you knock over are exactly those whose position is covered by at least one of your (at most ) length- windows, and your score is the sum of over every covered position. Report the maximum score you can achieve.
Input
The first line contains an integer () — the number of test cases. The test cases follow.
The first line of each test case contains three integers , , and :
- () — the number of pins;
- () — the number of balls;
- () — the width of a ball, i.e. how many consecutive pins one throw can knock over.
Each of the next lines contains one integer — the scores of the pins from left to right. Every score satisfies .
Output
For each test case, print a single line containing the maximum total score that can be achieved. The answer is guaranteed to be less than one billion.