Roller Coaster 2
Time limit8sMemory limit1024 MB
Given groups in a line for a coaster with capacity M, find the fewest runs after swapping at most one pair of adjacent groups.
- Level
Medium5 of 10
- Topics
- Array, Greedy, Simulation
- Solved
- No attempts yet
Problem
JAG (Japan Amusement Group) is a popular amusement park that attracts many tourists. Its roller coaster is the most popular attraction.
Today the park is crowded, and many group guests are waiting in line in front of the roller coaster. As a staff member, you check the line. There are currently groups in the waiting line, and the -th group from the front has people.
The roller coaster can carry up to people in one run. For each run, you seat guests by the following rules, and you repeat this until the waiting line is empty.
- If the line is empty, or if the group at the front has more people than the number of empty seats, the coaster departs and this run ends.
- Otherwise, seat all people in the front group in the empty seats.
- Return to step 1.
You notice that the park is about to close. You consider whether swapping the order of one pair of adjacent groups can reduce the number of runs. Specifically, you may perform the following operation at most once.
Choose an integer with , and swap the -th and -th groups in the waiting line.
If you perform the operation optimally, what is the minimum number of runs needed to empty the waiting line? No new guests join the line, and no waiting guests leave it.
Input
The first line contains integers and . is the number of waiting groups, with . is the maximum number of people who can ride in one run, with .
The second line contains integers , with .
The sum of over all datasets does not exceed . The input contains at most 50 datasets and ends with a line containing two zeros.
Output
For each dataset, print on one line the minimum number of runs needed when the operation is performed optimally.