Josif_tepe

Untitled

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