-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatcoderdpa.cpp
38 lines (29 loc) · 1.05 KB
/
atcoderdpa.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define int long long
#define inp freopen("file.inp", "r", stdin)
#define out freopen("file.out", "w", stdout)
#define TIME 1.0*clock()/CLOCKS_PER_SEC
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0)
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
int n, h[MAXN], dp[MAXN];
signed main() {
fastIO;
cin >> n;
fill(dp, dp + MAXN, INT_MAX);
for (int i = 0; i < n; i++)
cin >> h[i];
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
for (int i = 2; i < n; i++)
dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]));
cout << dp[n-1];
return 0;
}