Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
515 B
0
Indexable
Never
#include <iostream>
using namespace std;
const int maxn = 100005;
int n;
int niza[maxn];
int memo[maxn];
int main()
{
    cin >> n;
    for(int i = 0; i < n; i++) {
        cin >> niza[i];
        memo[i] = 2e9;
    }
    memo[0] = 0;
    for(int i = 1; i < n; i++) {
        memo[i] = min(memo[i], memo[i - 1] + abs(niza[i] - niza[i - 1]));
        if(i >= 2) {
            memo[i] = min(memo[i], memo[i - 2] + abs(niza[i] - niza[i - 2]));
        }
    }
    cout << memo[n - 1] << endl;
    return 0;
}

// 2504