Advertisement
Josif_tepe

Untitled

Oct 27th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n;
  7.     cin >> n;
  8.     vector<int> v(n);
  9.     for(int i = 0; i < n; i++) {
  10.         cin >> v[i];
  11.     }
  12.     vector<int> dp(n, 2e9);
  13.     dp[0] = 0;
  14.    
  15.     for(int i = 0; i < n; i++) {
  16.         if(i + 1 < n) {
  17.             dp[i + 1] = min(dp[i + 1], dp[i] + abs(v[i] - v[i + 1]));
  18.         }
  19.         if(i + 2 < n) {
  20.             dp[i + 2] = min(dp[i + 2], dp[i] + abs(v[i] - v[i + 2]));
  21.         }
  22.     }
  23.     cout << dp[n - 1] << endl;
  24.     return 0;
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement