#P3137. [USACO16FEB] Circular Barn S

[USACO16FEB] Circular Barn S

Description

As an enthusiast of modern architecture, Farmer John built a new circular barn. Inside the barn, nn rooms are arranged in a ring (3n10003 \leq n \leq 1000), numbered in clockwise order 1n1\ldots n. Each room has doors to its two adjacent rooms, as well as a door to the outside.

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

FJ decides to solve this by letting some cows move clockwise through rooms to their designated positions. If a cow passes through dd doors, it expends d2d^2 energy. You need to compute the minimal possible total energy expended by all cows.

Input Format

The first line contains an integer nn. The next nn lines each contain one integer cic_i, where the ii-th line contains cic_i.

Output Format

Output the minimal total energy expended by all cows.

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

Hint

Translated by ChatGPT 5