Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdlib>
using namespace std;
vector<int> a;
int f(int x){
if (x==0){
return 0;
}else if (x==1){
return abs(a[1] - a[0]);
} else{
return min(f(x - 1) + abs(a[x - 1] - a[x]), f(x - 2) + abs(a[x - 2] - a[x]));
}
}
int main(void) {
int n;
cin >> n;
a.resize(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
cout << f(n - 1) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment