Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <algorithm>
- #include <vector>
- #include <set>
- using namespace std;
- int a[30001];
- int dp[30001];
- int main()
- {
- int n;
- cin >> n;
- for (int i =0; i < n; i++)
- {
- cin >> a[i];
- }
- if(n == 1)
- {
- cout << a[0];
- return 0;
- }
- if(n == 2)
- {
- int kol1 = 0;
- kol1 = abs(a[0] - a[1]);
- cout << kol1;
- return 0;
- }
- for(int i = 2; i < n; i++)
- {
- int kol = 0, kol1 = 0;
- kol = abs(a[i - 2] - a[i - 1]) + abs(a[i - 1] - a[i]) + dp[i - 1];
- kol1 = abs(a[i] - a[i - 2]) * 3 + dp[i - 1];
- dp[i] = min(kol, kol1) ;
- }
- cout << dp[n -1];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement