Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- const int maxn = 1e5 + 10;
- const int INF = 2e9;
- int n;
- int a[maxn];
- int dp[maxn];
- int rec(int at) {
- if(at == n - 1) {
- return 0;
- }
- if(dp[at] != -1) {
- return dp[at];
- }
- int res = INF;
- if(at + 1 < n) {
- res = min(res, rec(at + 1) + abs(a[at] - a[at + 1]));
- }
- if(at + 2 < n) {
- res = min(res, rec(at + 2) + abs(a[at] - a[at + 2]));
- }
- dp[at] = res;
- return res;
- }
- int main() {
- cin >> n;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- memset(dp, -1, sizeof dp);
- cout << rec(0) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement