Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- const int INF = 1e9;
- const int mod = 1e9 + 7;
- const int N = 1e6 + 5;
- int dp[N];
- signed main()
- {
- int n , k;
- cin >> n >> k;
- int a[n + 5];
- for(int i = 1; i <= n; i++){
- cin >> a[i];
- }
- dp[1] = 0;
- for(int i = 2; i <= n; i++){
- dp[i] = INF;
- for(int j = 1; j <= k; j++){
- if(i == j)
- break;
- dp[i] = min(dp[i] , dp[i - j] + abs(a[i] - a[i - j]));
- }
- }
- cout << dp[n] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement