Jump
Time limit2sMemory limit1024 MB
Given building heights, sum the minimum total squared-height-difference cost over all pairs of buildings that can reach each other by visible jumps, modulo 1000000007.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, Dynamic programming
- Solved
- No attempts yet
Problem
A road runs left and right, and buildings stand along it. The buildings are numbered from to from left to right in the order they were built, and building has height .
Chaewan, who has outstanding physical ability, can jump between the rooftops of two different buildings and that satisfy the following condition.
- Every building between building and building has height less than .
Each jump costs stamina equal to the square of the difference between the two building heights. How far apart the two buildings are to the left or right does not affect the stamina cost.
Let be the minimum total stamina needed for Chaewan to move from the rooftop of building to the rooftop of building using jumps only. Find .
Input
The first line contains the number of buildings .
The second line contains the heights , separated by spaces. All heights are integers and are distinct.
Output
Print the remainder of divided by .