Trucks
InterviewTime limit1sMemory limit512 MB
Trucks cross a one-lane bridge in order under a weight limit; find the earliest time all have finished.
- Level
Easy3 of 10
- Topics
- Queue, Simulation
- Solved
- No attempts yet
Problem
A single lane bridge crosses a river. trucks cross it in the given order. The order cannot be changed, and the trucks do not all weigh the same.
The bridge is units long, so at most trucks stand on it at the same time. Each truck moves exactly one unit of distance per unit of time. The total weight of the trucks that are on the bridge at any moment must be at most the maximum load . A truck that has not yet moved completely onto the bridge does not count toward that total.
Figure 1 shows four trucks with weights 7, 4, 5, 6 crossing from right to left when the bridge length is 2 and the maximum load is 10. The shortest time for all of them to cross is 8.

Figure 1. Trucks crossing the bridge.
Given the bridge length, the maximum load, and the weights of the trucks in crossing order, write a program that computes the shortest time for every truck to finish crossing.
Input
Input comes from standard input and has two lines.
The first line contains three integers , , and separated by spaces. is the number of trucks, is the length of the bridge, and is the maximum load of the bridge (, , ).
The second line contains integers separated by spaces. is the weight of the -th truck ().
Output
Print to standard output the shortest time for all trucks to cross the bridge, on one line.