Circular Barn

Choose the entry room on a circular barn so cows walking clockwise to their assigned rooms cover the smallest total distance.

Easy2Brute forceSimulationInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Farmer John has built a new barn in the shape of a perfect circle. Inside, the barn is a ring of nn rooms, numbered 11 through nn clockwise around the perimeter of the barn (3n10003 \le n \le 1000). Each room has a door to each of its two neighboring rooms, and also a door to the outside of the barn.

Farmer John wants exactly rir_i cows to end up in room ii (1ri1001 \le r_i \le 100). To herd the cows in without a mess, he unlocks the outside door of a single room, and every cow enters through that door. Each cow then walks clockwise through the rooms until she reaches the room she belongs in. The distance one cow walks is the number of interior doors she passes through. Farmer John wants to unlock the outside door that makes the total distance walked by all the cows as small as possible. Find that minimum total distance.

Input

The first line contains nn. Each of the next nn lines contains one integer, giving r1r_1 through rnr_n in order.

Output

Print the minimum total distance the cows walk.

Hint

In the first sample, the best choice is the outside door of the room that needs 7 cows.