Advertisement
Josif_tepe

Untitled

Sep 25th, 2022
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     ios_base::sync_with_stdio(true);
  8.     int n;
  9.     cin>>n;
  10.     int x[n];
  11.     int dp[n];
  12.     memset(dp,0,sizeof dp);
  13.     for(int i=0;i<n;i++)cin>>x[i];
  14.     dp[0]=0;
  15.     dp[1]=abs(x[0]-x[1]);
  16.     for(int i=2;i<n;i++)
  17.     {
  18.         dp[i]=min(dp[i-1]+abs(x[i-1]-x[i]), dp[i-2]+abs(x[i-2]-x[i]));
  19.     }
  20.     cout<<dp[n-1];
  21.     return 0;
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement