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. vector<int> a;
  30.  
  31.  
  32. int consistency1(int n) {
  33.  
  34. vector<int> pMaxiDp(n);
  35.  
  36. int maxi = -INF;
  37. int last = 0;
  38. for(int i=0; i<n; i++){
  39. int curr = max(last + a[i], a[i]);
  40. last = curr;
  41. maxi = max(maxi, curr);
  42. pMaxiDp[i] = maxi;
  43. }
  44.  
  45.  
  46. vector<int> sDp(n+1, 0);
  47.  
  48. for(int i=n-1; i>=0; i--){
  49. sDp[i] = max(sDp[i+1] + a[i], a[i]);
  50. }
  51.  
  52.  
  53. int ans = -INF;
  54. for(int i=0; i<=n-2; i++){
  55.  
  56. for(int j=i+1; j<=n-1; j++){
  57. int sum = pMaxiDp[i] + sDp[j];
  58.  
  59. ans = max(ans , sum );
  60. }
  61. }
  62.  
  63. return ans;
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. int consistency2(int n) {
  73.  
  74. vector<int> pMaxiDp(n);
  75. int last = 0;
  76. int maxi = -INF;
  77.  
  78. for(int i=0; i<n; i++){
  79. int curr = max(last + a[i], a[i]);
  80. last = curr;
  81. maxi = max(maxi, curr);
  82. pMaxiDp[i] = maxi;
  83. }
  84.  
  85.  
  86. vector<int> sMaxiDp(n);
  87. last = 0;
  88. maxi = -INF;
  89.  
  90. for(int i=n-1; i>=0; i--){
  91. int curr = max(last + a[i], a[i]);
  92. last = curr;
  93. maxi = max(maxi, curr);
  94. sMaxiDp[i] = maxi;
  95. }
  96.  
  97. int ans = -INF;
  98. for(int i=0; i<n-1; i++){
  99. ans = max(ans, pMaxiDp[i] + sMaxiDp[i+1]);
  100. }
  101.  
  102. return ans;
  103.  
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. int practice(int n) {
  127.  
  128. return 0;
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135. void solve() {
  136.  
  137. int n;
  138. cin >> n;
  139.  
  140. a.resize(n);
  141. for(int i=0; i<n; i++) cin >> a[i];
  142.  
  143. cout << consistency1(n) << " " << consistency2(n) << endl ;
  144.  
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151. int32_t main() {
  152. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  153.  
  154. int t = 1;
  155. // cin >> t;
  156. while (t--) {
  157. solve();
  158. }
  159.  
  160. return 0;
  161. }
Success #stdin #stdout 0s 5320KB
stdin
7
1 5 -3 4 -9 9 2
stdout
18 18