Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define popcnt(a) __builtin_popcount(a)
  4. #define FastIO() ios::sync_with_stdio(false), cin.tie(0);
  5. #define IO() freopen("palindrome.in","rw",stdin)
  6. #define error(args...) \
  7. { \
  8. cerr<<"LINE "<<__LINE__; \
  9. string _s = #args; \
  10. replace(_s.begin(), _s.end(), ',', ' '); \
  11. stringstream _ss(_s); \
  12. istream_iterator<string> _it(_ss); \
  13. err(_it, args); \
  14. cerr<<endl; \
  15. }
  16. void err(istream_iterator<string> it)
  17. {
  18. }
  19. template <typename T, typename... Args>
  20. void err(istream_iterator<string> it, T a, Args... args)
  21. {
  22. cerr<<' '<< *it << " = " << a ;
  23. err(++it, args...);
  24. }
  25. typedef long long LL;
  26. const LL MOD = 1e9 + 7;
  27. const int N = 1e7+9, M = (1<<31), OO = 0x3f3f3f3f;
  28. int h[N],memo[N],n;
  29. int solve(int idx=0)
  30. {
  31. if(idx>=n) return OO;
  32. int &ret = memo[idx];
  33. if(~ret) return ret;
  34. ret = OO;
  35. return ret = min(ret,min(solve(idx+1)+ abs(h[idx]-h[idx+1]),solve(idx+2) + abs(h[idx]-h[idx+2]) ));
  36.  
  37. }
  38. int main()
  39. {
  40. memset(memo,-1,sizeof memo);
  41. scanf("%d",&n);
  42. for(int i=0;i<n;++i) scanf("%d",h+i);
  43. printf("%d",solve());
  44. #ifdef _LOCAL_DEFINE
  45. cerr << (double)clock() * 1.0 / CLOCKS_PER_SEC << endl;
  46. #endif
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement