E. Eradication Sort
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

The members of the No-Weather-too-Extreme Recreational Climbing society completed their first successful summit seven years ago to this day!

At the time, we took a picture of all the members standing together in one row. However, the photograph looks messy, as the climbers were not standing in order of height, and we have no way to reorder them.

We will need to cut some of the climbers out of the picture.

This picture of 7 (formerly 11) climbers was edited to solve Sample Input 3.

An optimal solution minimises the size and number of visible gaps in the photo. We define the cost as the sum of the squares of the lengths of gaps left in the edited photo. For example, if two individual climbers are removed from the photo and one pair of adjacent climbers are removed, the total cost is $$$1^2 + 1^2 + 2^2 = 6$$$.

Find the minimum possible cost you can reach by removing climbers.

Input

  • The number of people in the photo $$$n$$$ ($$$1 \le n \le 10^6$$$).
  • $$$n$$$ integers representing the heights of people in the photo, $$$h_1 \ldots h_n$$$ ($$$0 \le h \le 10^6$$$).
Output

Output the minimum cost achieved by removing climbers from the photo, such that the remaining climbers in the photo make a non-decreasing sequence.

Examples
Input
7
1 2 3 0 5 6 7
Output
1
Input
9
4 5 6 4 2 3 6 6 6
Output
8
Input
11
3 6 12 7 7 7 6 8 10 5 5
Output
6