Bowling for Numbers
Time limit1sMemory limit128 MB
Given a row of valued pins, pick up to k non-overlapping blocks of exactly w consecutive pins to maximize the total score.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
At a carnival, a popular game is Bowling for Numbers. A large number of bowling pins are lined up in a row. Each pin has a number printed on it, which is the score you earn for knocking that pin over. You are given several bowling balls; each ball is wide enough to knock over a few consecutive, adjacent pins.
For example, suppose the row of pins is:
2 8 5 1 9 6 9 3 2
If you are given two balls, each able to knock over three adjacent pins, the best score you can achieve is : one throw knocks over and the other knocks over . No two throws may overlap, because a pin that has already been knocked over cannot be knocked over again.
A greedy approach — repeatedly taking the throw worth the most among the pins still standing — often comes close, but it does not always reach the maximum. Your task is to compute the true maximum score.
Each ball knocks over a block of exactly consecutive pins from the original row, and no two balls may cover the same pin. You do not have to use every ball.
Input
The input consists of several test cases.
The first line contains an integer (), the number of test cases.
Each test case begins with a line containing three integers , , and :
- () — the number of bowling pins;
- () — the number of balls available;
- () — the width of each ball, i.e. the number of adjacent pins it knocks over.
The next lines each contain a single non-negative integer less than , giving the score of each pin, in order.
Output
For each test case, output a single line containing the maximum score the player can achieve. This score is guaranteed to be less than one billion.