Under construction

Still being built. Things may change

Social running

Interview

Time limit1sMemory limit1024 MB

Summary
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 NN friends, who live in NN different locations numbered 00 through N−1N-1. Everyone will run exactly one lap going through all the locations in cyclic order (if you live in location ii, you run i→i+1(modN)→…→N−1→0→…→i−1(modN)→ii\rightarrow i+1\pmod{N}\rightarrow\ldots\rightarrow N-1\rightarrow 0\rightarrow\ldots\rightarrow i-1\pmod{N}\rightarrow i. 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 33. 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 44.

Input

The first line of the input contains the number of friends in the group, NN (2≤N≤102\leq N\leq 10). It is followed by NN lines where the ii-th line contains a single integer, the distance from location ii to location i+1(modN)i+1\pmod{N}. Each distance is between 11 and 10410^4.

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.

Examples1

  1. Example 1

    Input
    4
    4
    1
    2
    3
    
    Expected output
    4