#P6170. [USACO16FEB] Circular Barn G

[USACO16FEB] Circular Barn G

Description

As a fan of modern architecture, Farmer John built a new circular barn. Inside the barn there are nn rooms arranged in a ring (3n1053 \leq n \leq 10^5), numbered 1n1 \ldots n in clockwise order. Each room has doors to its left and right neighboring rooms, and also a door leading outside.

Now FJ has nn cows, and his goal is to have exactly one cow in each room. Unfortunately, the cows are currently in random rooms: room ii contains cic_i cows. It is guaranteed that ci=n\sum c_i = n.

FJ decides to solve this problem as follows: he will have some cows move clockwise, passing through some rooms to reach their assigned positions. If a cow passes through dd doors, the energy it uses is d2d^2. You need to help FJ compute the minimum possible total energy used by all cows.

Input Format

The first line contains an integer nn. The next nn lines each contain an integer cic_i on line ii.

Output Format

Output the minimum total energy used by all cows.

10
1
0
0
2
0
0
1
2
2
2
33

Hint

Translated by ChatGPT 5