Advertisement
Josif_tepe

Untitled

May 25th, 2025
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. const int maxn = 1e5 + 10;
  5. const int INF = 2e9;
  6. int n;
  7. int a[maxn];
  8. int dp[maxn];
  9. int rec(int at) {
  10.     if(at == n - 1) {
  11.         return 0;
  12.     }
  13.     if(dp[at] != -1) {
  14.         return dp[at];
  15.     }
  16.    
  17.     int res = INF;
  18.    
  19.     if(at + 1 < n) {
  20.         res = min(res, rec(at + 1) + abs(a[at] - a[at + 1]));
  21.     }
  22.     if(at + 2 < n) {
  23.         res = min(res, rec(at + 2) + abs(a[at] - a[at + 2]));
  24.     }
  25.     dp[at] = res;
  26.     return res;
  27. }
  28.  
  29. int main() {
  30.     cin >> n;
  31.     for(int i = 0; i < n; i++) {
  32.         cin >> a[i];
  33.     }
  34.    
  35.     memset(dp, -1, sizeof dp);
  36.     cout << rec(0) << endl;
  37.     return 0;
  38. }
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement