Pumpkin Placement
Time limit2sMemory limit1024 MB
Pick fence positions, always including the first and last, to maximize total gap satisfaction minus the chosen discontentment costs.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Divide and conquer, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
For Halloween, residents decided to place a number of pumpkins along the fence of their house. In advance, candidate positions were chosen. Position has two parameters: , its distance from the start of the fence, and , the discontentment level of the residents if a pumpkin is placed there. Regardless of anything else, pumpkins are definitely placed at position and position .
Each resident named a favorite number . For resident , the satisfaction from two adjacent pumpkins at distance apart is . The total satisfaction of a resident is the sum of these values over every pair of adjacent pumpkins.
If pumpkins are placed at positions (in order along the fence), the total satisfaction is computed as .
The residents want to maximize this total satisfaction by choosing the positions for the pumpkins optimally.
Input
The first line contains two integers and , the number of candidate positions and the number of residents (, ).
The second line contains integers , the favorite number of each resident ().
Each of the next lines contains two integers and , the parameters of a candidate position (, ). It is guaranteed that .
Output
Print one integer, the maximum total satisfaction of the residents.