There are parking spaces arranged in a circle and numbered from to in clockwise order. There are also cars, numbered from to .
Car enters the parking area at space . It may park there or continue moving clockwise until it reaches another space. Cars cannot move counterclockwise.
Before any car enters, you must assign a distinct parking space to every car. If a car parks at its entry space, its cost is . Each move from one space to the next space clockwise increases its cost by .
Find the minimum possible value of the maximum cost among all cars.
The input consists of:
The output consists of:
where is the minimum possible value of the maximum cost among all cars.
Assign every car to its entry space. Every car then has cost .
The five cars can be assigned to spaces , giving costs . Since all cars enter at the same space and must receive distinct spaces, no smaller maximum cost is possible.
Assign the cars, in input order, to spaces . Their costs are .
A maximum cost of is impossible: the four cars entering at spaces and could then only use spaces .