Xeroxinator
InterviewTime limit2sMemory limit1024 MB
Given each minute's arrivals and a per-minute service limit b, compute the total minutes all clones spend in the post office queue over n minutes.
- Level
Easy3 of 10
- Topics
- Queue, Simulation
- Solved
- No attempts yet
Problem
Doctor Heinz Fufelschmertz was tired of standing in lines, so he built a xeroxinator, a device that makes clones of people. He now sends his clones to stand in lines in his place. Unfortunately, the device malfunctioned. Now too many clones of Heinz are being made, and all of them go to the post office.
Today the post office is open for minutes, numbered from 1 to . At the start of minute , clones of Fufelschmertz enter the post office and join the end of the queue. In one minute, the post office serves at most clones. If the queue has at least clones, the first of them are served. Otherwise, everyone in the queue is served. All clones served in minute leave at the end of minute . The post office closes at the end of minute . Clones who were not served wait for one more minute, complain, and then leave. Help Heinz compute the total time all clones spend in the post office.
Note that a clone who enters at the start of minute and leaves at the end of minute spent one minute in the post office.
Input
The first line contains two integers and , the number of minutes the post office is open and the maximum number of clones served per minute (, ).
The second line contains integers , where is the number of clones arriving at the start of minute ().
Output
Print one integer: the total time all clones spend in the post office.