fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main(){
  4. int n;cin>>n;int k;cin>>k;
  5. vector<int>v(n),dp(n,0);
  6. for(int i=0;i<n;i++)cin>>v[i];
  7. for(int i=1;i<n;i++){
  8. int j=1;dp[i]=INT_MAX;
  9. while(j<=k && j<=i){
  10. dp[i]=min(abs(v[i]-v[i-j])+dp[i-j],dp[i]);
  11. j++;
  12. }
  13. }
  14. cout<<dp[n-1]<<endl;
  15. return 0;
  16. }
Success #stdin #stdout 0.01s 5280KB
stdin
3
1
11 2 3
stdout
10