fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<vector<int>> a;
  31.  
  32. int consistency1(int n){
  33.  
  34. vector<int> dpE(n);
  35. vector<int> dpH(n);
  36. vector<int> dpN(n);
  37.  
  38. dpE[0] = a[0][0];
  39. dpH[0] = a[0][1];
  40. dpN[0] = 0;
  41.  
  42. for(int i=1; i<n; i++){
  43. dpE[i] = a[i][0] + max({ dpE[i-1], dpH[i-1], dpN[i-1] });
  44.  
  45. dpH[i] = a[i][1] + dpN[i-1];
  46.  
  47. dpN[i] = max({ dpE[i-1], dpH[i-1], dpN[i-1] });
  48. }
  49.  
  50.  
  51. return max({ dpE[n-1], dpH[n-1], dpN[n-1] }) ;
  52.  
  53. }
  54.  
  55.  
  56.  
  57. int consistency2(int n){
  58.  
  59. vector<int> dpE(n);
  60. vector<int> dpH(n);
  61.  
  62. dpE[0] = a[0][0];
  63. dpH[0] = a[0][1];
  64.  
  65. for(int i=1; i<n; i++){
  66. dpE[i] = a[i][0] + max( dpE[i-1], dpH[i-1] );
  67.  
  68. dpH[i] = a[i][1];
  69. if(i-2>=0) dpH[i] += max(dpE[i-2], dpH[i-2]);
  70.  
  71. }
  72.  
  73.  
  74. return max(dpE[n-1], dpH[n-1]) ;
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. int practice(int n){
  89.  
  90.  
  91. return 0;
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. void solve() {
  99.  
  100. int n;
  101. cin>> n;
  102.  
  103. a.resize(n);
  104. for(int i=0; i<n; i++){
  105. int x, y;
  106. cin >> x >> y;
  107. a[i] = {x, y};
  108. }
  109.  
  110. cout << consistency1(n) << " " << consistency2(n) << endl;
  111.  
  112.  
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119. int32_t main() {
  120. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  121.  
  122. int t = 1;
  123. // cin >> t;
  124. while (t--) {
  125. solve();
  126. }
  127.  
  128. return 0;
  129. }
Success #stdin #stdout 0.01s 5296KB
stdin
4
1 2
4 10
20 21
2 23 
stdout
33 33