C. Bob the Builder

0

0 votes
Dynamic Programming
Problem

Bob the builder has come to Toyland to help Plod the policeman repair the gate of the jail cells that was damaged when the Goblins escaped last night. 

The door of the cell can be thought of as N bars placed in a row where the distance between every two adjacent bars is 1 unit. While escaping, the goblins cut all the bars by some length Li. The original height of the bars was H, same as the height of the gate. Because of recent budget cuts due to falling GDP, Plod doesn't want to repair all the bars, instead he wants to repair the bars in such a way that the gap before the first bar, between any two adjacent repaired bars and after the last bar is atmost W, so that the prisoners can't escape again. 

You being Bob the builder, have to tell Plod the minimum cost to repair the door while satisfying the constraints.

Note

The gap between the first bar and the frame of the gate and between the last bar and the frame of the gate is 1 unit too. 

Consider thickness of bars to be negligible.

Input format

The first line of input contains 3 integers: N, H, W representing the number of bars in the gate, the height of the gate and the maximum allowed width between the bars. 

The second line of input contains N space seperated integers representating the length of the bars cut by the Goblins. 

Output format

Print a single integer representing the minimum cost to repair the gate of the cell.

Constraints

1N30001WN1H1051LiH

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Since the maximum allowed width is 2, we need to take atlest one among 5 and 4. Let us take 4. Now we need to take either one of 6 and 7 to have a width of atmost 2. We will take 6. The total is 4 + 6 = 10. It can be shown that this is infact the minimum cost to repair te gate.

Jail
Gate of Jail Cell

 

Editor Image

?