Circular Barn (Bronze)#10581

Circular Barn (Bronze)

1 second
128 megabytes
Easy

Being a fan of contemporary architecture, Farmer John has built a new barn in the shape of a perfect circle. Inside, the barn consists of a ring of rooms, numbered clockwise from to around the perimeter. Each room has doors connecting it to its two neighboring rooms, as well as a door leading to the outside of the barn.

Farmer John wants exactly r_i cows to end up in each room . To herd the cows into the barn in an orderly fashion, he plans to unlock the exterior door of exactly one room, allowing the cows to enter through that door. Each cow then walks clockwise through the rooms until she reaches her destination. Farmer John wants to choose the exterior door that minimizes the total distance traveled by all the cows. Please determine the minimum total distance the cows must travel if he chooses the optimal door. The distance traveled by a single cow is defined as the number of interior doors through which she passes.

Input

The first line contains an integer n~(3 \le n \le 1000). The next lines each contain an integer r_i~(1 \le r_i \le 100).

Output

Print the minimum total distance that the cows collectively need to travel.

Examples

In this example, the optimal solution is to let the cows enter through the exterior door of the room that requires cows.

Example 1

Loading
Loading