Eradication Sort
Time limit2sMemory limit1024 MB
Remove climbers so the rest form a non-decreasing sequence, minimizing the sum of squared lengths of the gaps left behind.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Segment tree, Sorting
- Solved
- No attempts yet
Problem
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, the members stood together in one row for a photograph. However, the climbers were not standing in order of height, and the photo cannot be reordered.
You will need to cut some of the climbers out of the picture.

Figure E.1: A photo of 7 climbers, edited down from an original group of 11.
An optimal solution minimizes the size and number of visible gaps in the photo. The cost is the sum of the squares of the lengths of the gaps left in the edited photo. For example, if two individual climbers are removed and one pair of adjacent climbers is removed, the total cost is .
Find the minimum cost you can reach by removing climbers.
Input
The first line contains the number of people in the photo, ().
The second line contains integers , the heights of the people in the photo ().
Output
Output the minimum cost achieved by removing climbers so that the remaining climbers form a non-decreasing sequence.