QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#21168. Circular Barn

Statistics

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 $n$ rooms, numbered clockwise from $1 \ldots n$ around the perimeter of the barn ($3 \leq n \leq 100,000$). Each room has doors to its two neighboring rooms, and also a door opening to the exterior of the barn.

Farmer John owns $n$ cows, and he wants exactly one cow to end up in each room in the barn. However, the cows, being slightly confused, line up at haphazard doors, with possibly multiple cows lining up at the same door. Precisely $c_i$ cows line up outside the door to room $i$, so $\sum c_i = n$.

To manage the process of herding the cows so that one cow ends up in each room, Farmer John wants to use the following approach: each cow enters at the door at which she initially lined up, then walks clockwise through the rooms until she reaches a suitable destination. Given that a cow walking through $d$ doors consumes $d^2$ energy, please determine the minimum amount of energy needed to distribute the cows so one ends up in each room.

Input

The first line of input contains $n$. Each of the remaining $n$ lines contain $c_1 \ldots c_n$.

Output

Please write out the minimum amount of energy consumed by the cows.

Examples

Input 1

10
1
0
0
2
0
0
1
2
2
2

Output 1

33

Problem credits: Brian Dean

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.