Advertisement
Josif_tepe

Untitled

May 12th, 2025
217
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 = 1e9;
  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.     int res = INF;
  17.     if(at + 1 < n) {
  18.         res = min(res, rec(at + 1) + abs(a[at] - a[at + 1]));
  19.     }
  20.     if(at + 2 < n) {
  21.         res = min(res, rec(at + 2) + abs(a[at] - a[at + 2]));
  22.     }
  23.     dp[at] = res;
  24.     return res;
  25.    
  26. }
  27. int main() {
  28.     memset(dp, -1, sizeof dp);
  29.     cin >> n;
  30.     for(int i = 0; i < n; i++) {
  31.         cin >> a[i];
  32.     }
  33.    
  34.     cout << rec(0) << endl;
  35.    
  36.     return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement