Advertisement
Josif_tepe

Untitled

Oct 18th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 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.     cin >> n;
  9.     for(int i = 0; i < n; i++) {
  10.         cin >> arr[i];
  11.         memo[i] = 2e9;
  12.     }
  13.     memo[0] = 0;
  14.     for(int i = 0; i < n; i++) {
  15.         if(i + 1 < n) {
  16.             memo[i + 1] = min(memo[i + 1], memo[i] + abs(arr[i] - arr[i + 1]));
  17.         }
  18.         if(i + 2 < n) {
  19.             memo[i + 2] = min(memo[i + 2], memo[i] + abs(arr[i] - arr[i + 2]));
  20.         }
  21.     }
  22.     cout << memo[n - 1] << endl;
  23.     return 0;
  24. }
  25. // fib(7) = fib(6) + fib(5)
  26. //
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement