fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. int main() {
  7. int n;
  8. cin>>n;
  9. vector<int> h(n);
  10.  
  11. for(int i=0;i<n;i++)cin>>h[i];
  12.  
  13. vector<int> dp(n,0);
  14. dp[1]=abs(h[1]-h[0]);
  15.  
  16. for(int i=2;i<n;i++) {
  17. dp[i]=min(abs(h[i]-h[i-1])+dp[i-1],abs(h[i]-h[i-2])+dp[i-2]);
  18. }
  19. cout<<dp[n-1]<<endl;
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0.01s 5284KB
stdin
6
30 10 60 10 60 50
stdout
40