Advertisement
LEGEND2004

Frog 1

Aug 24th, 2023
919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. const int INF = 1000000000;
  6. const int mod = 1e9 + 7;
  7. const int N = 1e6 + 5;
  8. int dp[N];
  9.  
  10. signed main()
  11. {
  12.     int n;
  13.     cin >> n;
  14.     int a[n + 5];
  15.     for(int i = 1; i <= n; i++){
  16.         cin >> a[i];
  17.     }
  18.     dp[1] = 0;
  19.     dp[2] = abs(a[2] - a[1]);
  20.     for(int i = 3; i <= n; i++){
  21.         dp[i] = min(dp[i - 1] + abs(a[i] - a[i - 1]) , dp[i - 2] + abs(a[i] - a[i - 2]));
  22.     }
  23.     cout << dp[n] << endl;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement