Imperial Civilization
Time limit1.5sMemory limit512 MB
Choose a subset of stops that must include the first and last positions to maximize total resident satisfaction minus stop dissatisfaction, where each consecutive gap of length d scores the sum of |d - d_i|.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Divide and conquer, Geometry
- Solved
- No attempts yet
Problem
After the apocalypse, Mr. Malnar sets out to build a new civilization. His first project is to rebuild the tram network on the main street of his empire.
There are candidate positions along the street, and Mr. Malnar picks some of them for tram stops. The first and the -th positions are turnaround stops, so both must be chosen.
Each position has a distance from the start of the street and a dissatisfaction that residents feel if a stop is built there. Some stops have kiosks that always overcharge for tickets, and some have excellent bakeries.
There are residents who will use the line. Resident dislikes rides of length and rates a chosen set of stops as follows:
- The total satisfaction of this resident is the sum of satisfaction over every pair of consecutive chosen stops.
- Two consecutive chosen stops at distance give satisfaction .
Mr. Malnar adds up the satisfaction of all residents, then subtracts the dissatisfaction of every chosen stop . Find the maximum total he can achieve by choosing the stops optimally.
Input
The first line contains the natural numbers () and ().
The second line contains integers ().
The next lines describe the candidate positions. Each line contains the position () and the dissatisfaction (). All and are integers, and .
Output
Print one line with the maximum possible total satisfaction.
Hint
Note on the third sample: The optimal choice is positions 1, 3, 5, 7, and 9. The consecutive distances are 6, 17, 49, and 25, and the residents' satisfaction values are 61, 159, 89, 275, and 171 in that order. The satisfaction values sum to 755, and the chosen stops have a total dissatisfaction of 618, so the final result is 137.