Submit solution

Points: 7
Time limit: 2.0s
Memory limit: 64M

Problem type

These problems are from the atcoder DP contest, and were transferred onto DMOJ. All problem statements were made by several atcoder users. As there is no access to the test data, all data is randomly generated. If there are issues with the statement or data, please contact Rimuru or Ninjaclasher on slack.

There are N stones, numbered 1, 2, \dots, N. For each i (1 \le i \le N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N:

  • If the frog is currently on Stone i, jump to Stone i+1 or Stone i+2. Here, a cost of \left|h_i-h_j \right | is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

  • All values in input are integers.
  • 2 \le N \le 10^5
  • 1 \le h_i \le 10^4

Input Specification

The first line of input will contain an integer N.

The second line of input will contain N spaced integers, h_i, the height of stone i.

Output Specification

Output a single integer, the minimum possible total cost incurred.

Sample Input 1

4
10 30 40 20

Sample Output 1

30

Sample Input 2

2
10 10

Sample Output 2

0

Sample Input 3

6
30 10 60 10 60 50

Sample Output 3

40

Sample Explanations

For the first sample, we can follow path 1 \rightarrow 2 \rightarrow 4, the total cost incurred would be \left | 10 - 30 \right | + \left | 30 - 20 \right | = 30.

For the second sample, we can follow the path 1 \rightarrow 2, with the total cost incurred being \left |10 - 10 \right | = 0.

In the last sample, we follow the path 1 \rightarrow 3 \rightarrow 5 \rightarrow 6, the total cost incurred would be \left | 30-60 \right | + \left | 60 - 60 \right | + \left | 60 - 50 \right | = 40.


Comments


  • 0
    raytonlin1  commented on Jan. 16, 2019, 10:00 p.m. edit 2

    Did this go from 5 to 7 points?