시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB42272165.625%

문제

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.

Figure E.1: 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.

입력

  • 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 the minimum cost achieved by removing climbers from the photo, such that the remaining climbers in the photo make a non-decreasing sequence.

예제 입력 1

7
1 2 3 0 5 6 7

예제 출력 1

1

예제 입력 2

9
4 5 6 4 2 3 6 6 6

예제 출력 2

8

예제 입력 3

11
3 6 12 7 7 7 6 8 10 5 5

예제 출력 3

6

출처

ICPC > Regionals > Europe > Northwestern European Regional Contest > The UK & Ireland Programming Contest > UKIEPC 2024 E번

  • 문제를 만든 사람: Robin Lee