fork download
  1. //YOUTUBE:youtube.com/channel/UCctCQUpRrxYnCBT7Yh2TuAA
  2. //soicodoc10tin
  3. #include <bits/stdc++.h>
  4. #define sz(x) int(x.size())
  5. #define reset(x) memset(x, 0,sizeof(x))
  6. #define Rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
  7. #define For(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
  8. #define Ford(i,l,u) for(int (i)=(int)(l);(i)>=(int)(u);--(i))
  9. #define MIN(x,y) if (x > (y)) x = (y)
  10. #define MAX(x,y) if (x < (y)) x = (y)
  11. #define PB push_back
  12. #define mp make_pair
  13. #define F first
  14. #define S second
  15. #define maxn 1000006
  16. #define MOD 1000000007
  17. #define remain(x) if (x > MOD) x -= MOD
  18. #define pii pair<int, int>
  19. #define bit(x, i) (((x) >> (i)) & 1)
  20. #define LG 30
  21. #define Task "a"
  22. #define int long long
  23.  
  24. using namespace std;
  25.  
  26. typedef long long ll;
  27. typedef long double ld;
  28.  
  29. int dx[] = {0 , -1 , 1 , 0};
  30. int dy[] = {-1 , 0 , 0 , 1};
  31.  
  32. int n , a[maxn] , dp[maxn][2];
  33.  
  34. signed main(){
  35. ios_base::sync_with_stdio(0);
  36. cin.tie(0);cout.tie(0);
  37. if(fopen(Task".inp" , "r")){
  38. freopen(Task".inp" , "r" , stdin);
  39. freopen(Task".out" , "w" , stdout);
  40. }
  41. cin >> n;
  42. For(i , 1 , n) cin >> a[i];
  43. dp[1][0] = 0;
  44. dp[1][1] = a[1];
  45. For(i , 2 , n){
  46. dp[i][0] = max(dp[i - 1][1] - a[i] , dp[i - 1][0]);
  47. dp[i][1] = max(dp[i - 1][0] + a[i] , dp[i - 1][1]);
  48. }
  49. cout << max(dp[n][0] , dp[n][1]);
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0.01s 5604KB
stdin
7
4 9 2 4 1 3 7
stdout
17