fork download
  1. //ssenseeeeeeeeeeeeeeeeeeeeeeeeeeeee
  2. #include<bits/stdc++.h>
  3. #define startt ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  4. typedef unsigned long long ull;
  5. typedef long long ll;
  6. #define MOD 1000000007
  7. #define MX 1000000000
  8. #define NMAX 100005
  9. #define MXL 1000000000000000000
  10. #define PI 3.14159265
  11. #define pb push_back
  12. using namespace std;
  13. #define sc second
  14. #define endl '\n'
  15. #define fr first
  16.  
  17. int main()
  18. {
  19. int n, k;
  20. cin >> n >> k;
  21. int a[n+1];
  22. for(int i = 0; i < n; i++)
  23. {
  24. cin >> a[i];
  25. }
  26. int dp[10001];
  27. dp[0] = 0;
  28. for(int i = 1; i < n; i++)
  29. {
  30. dp[i] = MX;
  31. for(int j = i-1; j >= max(0, i-k); j--)
  32. {
  33. dp[i] = min(dp[i], dp[j]+abs(a[j]-a[i]));
  34. }
  35. }
  36. cout << dp[n-1] << endl;;
  37. }
Success #stdin #stdout 0s 4476KB
stdin
Standard input is empty
stdout
0