Untitled

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

// 2504