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;
int main(void) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> dp(n);
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
for (int i = 2; i < n; i++) {
dp[i] = min(dp[i - 1] + abs(a[i - 1] - a[i]), dp[i - 2] + abs(a[i - 2] - a[i]));
}
cout << dp[n - 1] << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment