Coin Quiz
InterviewTime limit1sMemory limit1024 MB
Pick a threshold b that empties cells holding at most b coins so every k consecutive cells have at least m empty ones, maximizing leftover coins minus b.
- Level
Medium6 of 10
- Topics
- Binary search, Sliding window, Prefix sum
- Solved
- No attempts yet
Statement
The rules of a new TV quiz are as follows. There are cells in a row, numbered from to . Cell contains coins.
A player may choose an integer and pay coins. Then the host takes the coins from every cell that holds at most coins, and those cells become empty. After that, among any consecutive cells there must be at least empty cells. Then the player takes all coins left on the board. If the player takes coins in total, the prize is .
Find the maximum prize the player can guarantee.
Input
The first line contains integers , , and ().
The second line contains integers ().
Output
Print one integer: the maximum prize the player can guarantee.
Hint
In the first example, the player chooses . After the coins are removed from the cells that hold at most coins, the cells contain . The player takes coins from the cells. Counting the coins paid earlier, the prize is .
In the second example, so that every two consecutive cells contain at least one empty cell, the player must choose . After that no coins remain, and the prize is negative: .