coderchesser

atcoder_dp_A

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