Advertisement
sherry_ahmos

Untitled

Nov 28th, 2022
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll long long
  7. #define cy cout << "YES"
  8. #define cn cout << "NO"
  9. #define nl "\n"
  10. #define cin(v) for(int i=0;i<n,cin>>v[i];i++)
  11. #define cout(v) for(int i=0;i<v.size(),cout<<v[i]<<" ";i++)
  12. #define all(v) v.begin(),v.end()
  13. #define sz(s) s.size()
  14.  
  15. void sherry()
  16. {
  17.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  18. #ifndef ONLINE_JUDGE
  19.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  20. #endif
  21. }
  22. vector<int> saved, v;
  23. int n;
  24. int rec(int i){
  25.     if(i == n - 1)
  26.         return 0;
  27.     int& ret = saved[i];
  28.     if(~ret)
  29.         return ret;
  30.     ret = 1e9;
  31.     if(i + 1 < n)
  32.         ret = min(ret, abs(v[i] - v[i + 1])) + rec(i + 1);
  33.     if(i + 2 < n)
  34.         ret = min(ret, abs(v[i] - v[i + 2])) + rec(i + 2);
  35.     return ret;
  36. }
  37. void solve(){
  38.     cin >> n;
  39.     v.resize(n);
  40.     saved.assign(n + 5, -1);
  41.     cin(v);
  42.     cout << rec(0);
  43. }
  44. int main()
  45. {
  46.     sherry();
  47.     int t = 1;
  48.     //cin >> t;
  49.     while(t--){
  50.         solve();
  51.     }
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement