Advertisement
Josif_tepe

Untitled

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