fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  6.  
  7. #include <ext/pb_ds/assoc_container.hpp>
  8. #include <ext/pb_ds/tree_policy.hpp>
  9. using namespace __gnu_pbds;
  10.  
  11. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
  12. typedef pair<int, int>pr;
  13. #define all(i) i.begin() , i.end()
  14. #define ft first
  15. #define sn second
  16. #define pb push_back
  17. #define totalone(mask) __builtin_popcount(mask)
  18. #define chkbit(mask,bit) (mask&(1LL << bit))
  19.  
  20. // debug section start
  21. void __print(int x) {cerr << x;}
  22. void __print(long x) {cerr << x;}
  23. void __print(long long x) {cerr << x;}
  24. void __print(unsigned x) {cerr << x;}
  25. void __print(unsigned long x) {cerr << x;}
  26. void __print(unsigned long long x) {cerr << x;}
  27. void __print(float x) {cerr << x;}
  28. void __print(double x) {cerr << x;}
  29. void __print(long double x) {cerr << x;}
  30. void __print(char x) {cerr << '\'' << x << '\'';}
  31. void __print(const char *x) {cerr << '\"' << x << '\"';}
  32. void __print(const string &x) {cerr << '\"' << x << '\"';}
  33. void __print(bool x) {cerr << (x ? "true" : "false");}
  34.  
  35. template<typename T, typename V>
  36. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  37. template<typename T>
  38. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  39. void _print() {cerr << "]\n";}
  40. template <typename T, typename... V>
  41. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  42. #ifndef ONLINE_JUDGE
  43. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  44. #else
  45. #define debug(x...)
  46. #endif
  47. // debug section closed
  48.  
  49. #define en "\n"
  50. #define sum(n) ((1LL*(n)*(n+1))/ 2LL)
  51. #define sqr(n) (1LL*(n)*(n))
  52. #define vag(a,b) ((a + b - 1)/b)
  53.  
  54. #define MAXN 100010
  55. #define inf 1e6+5
  56. const int mod = 1e9 + 7;
  57.  
  58. ll n;
  59. ll a[MAXN];
  60. ll mem[MAXN][2];
  61.  
  62. ll add(ll x, ll y)
  63. {
  64. return abs(x - y);
  65. }
  66. ll f(ll i, ll st)
  67. {
  68. if (i == n - 1) {
  69. if (st == 0) {
  70. return 0;
  71. }
  72. return add(a[i], a[i - 1]);
  73. }
  74. if (mem[i][st] != -1) return mem[i][st];
  75. ll an = 1e18;
  76. if (st == 0) {
  77. if (i + 1 < n)
  78. an = min(an, add(a[i], a[i + 1]) + f(i + 1, 0));
  79. if (i + 2 < n)
  80. an = min(an, add(a[i], a[i + 2]) + f(i + 2, 1));
  81. }
  82. else {
  83. // 0 2 1 3
  84. if (i + 1 < n)
  85. an = min(an, add(a[i], a[i - 1]) + add(a[i + 1], a[i - 1]) + f(i + 1, 0));
  86. if (i + 2 < n)
  87. an = min(an, add(a[i], a[i - 1]) + add(a[i + 2], a[i - 1]) + f(i + 2, 1));
  88. }
  89. return mem[i][st] = an;
  90. }
  91. void solve()
  92. {
  93. cin >> n;
  94. for (ll i = 0; i < n; i++) {
  95. cin >> a[i];
  96. }
  97. if (n == 1) {
  98. cout << 0 << en; return;
  99. }
  100.  
  101. memset(mem, -1, sizeof(mem));
  102.  
  103. ll an = f(0, 0);
  104. an = min(an, f(1, 1));
  105. cout << an << en;
  106. }
  107. int main()
  108. {
  109. IOS;
  110. ll t;
  111. t = 1;
  112. // cin >> t;
  113.  
  114. int c = 0;
  115. while ( t-- )
  116. {
  117. // cout << "Case " << ++c << ": ";
  118. solve();
  119. }
  120. return 0;
  121. }
Success #stdin #stdout 0.01s 5448KB
stdin
Standard input is empty
stdout
1000000000000000000