Advertisement
Josif_tepe

Untitled

Oct 15th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace  std;
  4. int n, k;
  5. int niza[100005];
  6. int dp[100005];
  7. int jump(int i) {
  8.     if(i == n - 1) {
  9.         return 0;
  10.     }
  11.     if(dp[i] != -1) {
  12.         return dp[i];
  13.     }
  14.     int result = 2e9;
  15.     for(int j = 1; j <= k; j++) {
  16.         if(i + j < n) {
  17.             result = min(result, jump(i + j) + abs(niza[i] - niza[i + j]));
  18.         }
  19.     }
  20.     dp[i] = result;
  21.     return result;
  22. }
  23. int main() {
  24.     cin >> n >> k;
  25.     for(int i = 0; i < n; i++) {
  26.         cin >> niza[i];
  27.         dp[i] = -1;
  28.     }
  29.     cout << jump(0) << endl;
  30. }
  31.  
  32.  
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement