fork download
  1. //Solution By SlavicG
  2. #include "bits/stdc++.h"
  3. using namespace std;
  4.  
  5. #define ll long long
  6.  
  7. #define forn(i,n) for(int i=0;i<n;i++)
  8. #define all(v) v.begin(), v.end()
  9. #define rall(v) v.rbegin(),v.rend()
  10.  
  11. #define pb push_back
  12. #define sz(a) (int)a.size()
  13.  
  14. #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  15. #define endl "\n"
  16.  
  17. int main()
  18. {
  19. int n,k;
  20. cin >> n >> k;
  21. int a[n];
  22. int dp[n];
  23. forn(i,n)cin >> a[i];
  24. forn(i,n)dp[i] = INT_MAX;
  25. dp[0] = 0;
  26. for(int i = 0;i<n;i++)
  27. {
  28. for(int j = 1 ; j <= k;j++)
  29. {
  30. if(i + j < n)dp[i+j] = min(dp[i + j], dp[i] + max(a[i + j] , a[i]) - min(a[i + j] , a[i]));
  31. }
  32. }
  33. cout << dp[n-1] << endl;
  34. }
Time limit exceeded #stdin #stdout 5s 68964KB
stdin
Standard input is empty
stdout
Standard output is empty