Train
InterviewTime limit1sMemory limit1024 MB
Given passenger counts per car, find the minimum total number of car-to-car moves so that every car ends with exactly K passengers.
- Level
Medium5 of 10
- Topics
- Prefix sum, Greedy
- Solved
- No attempts yet
Problem
It is ski season! The train station is crowded with holidaymakers heading to the slopes. Afraid of missing their train, the passengers boarded the cars without checking where the free seats were.
Each car of the train has seats. The total number of seats equals the number of passengers exactly. That is always the case at this time of year.
Given how many passengers boarded each car, find the minimum total number of cars that passengers must pass through so that every passenger can sit down.
Input
The first line contains two integers , the number of cars, and , the number of seats in each car. The second line contains integers , where is the number of passengers who boarded car .
It is given that .
Output
Print one integer, the minimum total number of cars the passengers must pass through so that everyone can sit down.
The answer may exceed the range of a 32-bit integer. Use a 64-bit integer type to store it (long long in C/C++, Int64 in Pascal).
Constraints