Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define cy cout << "YES"
- #define cn cout << "NO"
- #define nl "\n"
- #define cin(v) for(int i=0;i<n,cin>>v[i];i++)
- #define cout(v) for(int i=0;i<v.size(),cout<<v[i]<<" ";i++)
- #define all(v) v.begin(),v.end()
- #define sz(s) s.size()
- void sherry()
- {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- }
- vector<int> saved, v;
- int n;
- int rec(int i){
- if(i == n - 1)
- return 0;
- int& ret = saved[i];
- if(~ret)
- return ret;
- ret = 1e9;
- if(i + 1 < n)
- ret = min(ret, abs(v[i] - v[i + 1])) + rec(i + 1);
- if(i + 2 < n)
- ret = min(ret, abs(v[i] - v[i + 2])) + rec(i + 2);
- return ret;
- }
- void solve(){
- cin >> n;
- v.resize(n);
- saved.assign(n + 5, -1);
- cin(v);
- cout << rec(0);
- }
- int main()
- {
- sherry();
- int t = 1;
- //cin >> t;
- while(t--){
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement