Advertisement
Josif_tepe

Untitled

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