#P58. [KBC005B] Sheep
[KBC005B] Sheep
Source
This problem is adapted from Long Long OJ. All rights reserved.
Problem Description
Little A built a circular sheepfold with rooms, numbered clockwise from to . The room numbered is adjacent to the room numbered , and adjacent rooms are connected by passages. Each room has a door opening outward.
Little A wants to raise sheep in these rooms, with sheep in the -th room. To drive the sheep into the rooms in an orderly manner, he plans to open the outward door of one room so that all sheep enter through this door.
After entering the room, the sheep walk clockwise through the rooms until they reach their own room. Little A wants to minimize the total distance traveled by all sheep. Please help Little A determine which room's door to open for all sheep to enter, such that the total distance traveled by the sheep is minimized, and find this minimum total distance. The distance traveled by each sheep is the number of rooms it passes through.
Input Format
The first line contains only one integer .
The next line contains (separated by spaces), representing the number of sheep in the -th room.
Output Format
Output only one number, which is the minimum value of the total distance traveled by the sheep.
The answer should be taken modulo .
Samples
5
4 7 8 6 4
48
Sample 1 Explanation
There are rooms in total: sheep in Room , sheep in Room , sheep in Room , sheep in Room , and sheep in Room .
The optimal way is to open the door of Room and let all sheep enter through Room . In this case:
- sheep (in Room ) do not need to walk;
- sheep walk to Room , with a total distance of ;
- sheep walk to Room , with a total distance of ;
- sheep walk to Room , with a total distance of ;
- sheep walk to Room , with a total distance of .
The total distance is .
Data Range
- For of the data: ;
- For of the data: ;
- For of the data: ;
- For of the data: ;
- For of the data: ;
- For of the data: ;
- For of the data: ;
- For of the data: .