Social running
InterviewTime limit1sMemory limit1024 MB
Given a cycle of N locations with edge lengths, choose the start point so that the total distance run alone by all runners is minimized.
- Level
Medium4 of 10
- Topics
- Brute force, Simulation, Array, Prefix sum
- Solved
- No attempts yet
Problem
You and a few friends run together once a week around Lund. You all agree that running on your own is boring and also dangerous compared to running with others. Therefore you and your friends decide to minimize the distance that anyone needs to run on their own.
You are friends, who live in different locations numbered through . Everyone will run exactly one lap going through all the locations in cyclic order (if you live in location , you run . Someone starts the run, then everyone joins the run as the group comes, runs exactly one lap and stops when the group comes back to their house.

Given that an optimal person starts the run, what is the minimal total distance that anyone has to run on their own? In the above example, an optimal choice would be to start the run at location . Then the first runner has to run the leg from 3 to 0 alone, and it is easily checked that your friend living in location 2 also will be running alone from 1 to 2, for a total solitary distance of .
Input
The first line of the input contains the number of friends in the group, (). It is followed by lines where the -th line contains a single integer, the distance from location to location . Each distance is between and .
Output
The output consists of a single integer, the minimal total distance anyone has to run on their own, given that the optimal person starts the run.