fork download
  1. // written by Eng. Abdullah Haydari
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. int main(){
  5. int n; cin>>n;
  6. int h[n],memo[n]; // memo: memoization
  7. memo[n-1]=0;
  8. memo[n]=1e9; // Out of the array
  9. for (int i=0;i<n;i++)
  10. cin>>h[i];
  11. for (int i=n-2;i>=0;i--){
  12. int jump1=memo[i+1]+abs(h[i]-h[i+1]);
  13. int jump2=memo[i+2]+abs(h[i]-h[i+2]);
  14. memo[i]=min(jump1,jump2);
  15. }
  16. cout<<memo[0]<<endl;
  17. }
Success #stdin #stdout 0.01s 5320KB
stdin
4
10 30 40 20
stdout
30