1396 - Travel
Time Limit : 1 Second
Memory Limit : 128 MB
Submission: 89
Solved: 23
- Description
There are N contiguous roads in a line, each road having different height. The i-th road has the height of H[i]. From road i to road i+1, you can do two things:
1, walk to road i+1, which costs |H[i]-H[i+1]|
2, change the height of road i to K, which costs |H[i]-K|
Please calculate the minimum cost of the walk from road 1 to road N.
- Input
First line, a number N.(1<=N<=100000)
Second line, N numbers indicating the height of road i.(0<=H[i]<=10000000)
- Output
One line, containing a number, the minimum cost.
(please notice the answer maybe huge, equal or larger than 231).
- sample input
-
3 3 4 1
- sample output
-
3
- hint
Change the height of road 2 to 3, and road 3 to 2, cost 2.
Then the H[i] will be 3 3 2, walk from 1 to N cost 1.
The answer is 3.
- source
- Orz教主第4次模拟赛