fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. cin >> n;
  7. vector<int> b(n + 1);
  8.  
  9. for (int i = 1; i <= n; i++) {
  10. cin >> b[i];
  11. }
  12.  
  13. vector<int> p(n + 1, 0);
  14. vector<int> s(n + 1, 0);
  15. vector<int> u(n + 1, 0);
  16. vector<int> max_s(n + 1, 0);
  17.  
  18. p[1] = b[1];
  19.  
  20. for (int i = 2; i <= n; i++) {
  21. p[i] = max(b[i], b[i] + p[i - 1]);
  22. //cout << p[i] << endl;
  23. }
  24.  
  25. s[n] = b[n];
  26. u[n] = s[n];
  27.  
  28. for (int j = n - 1; j >= 1; j--) {
  29. if (b[j] < b[j + 1]) {
  30. s[j] = b[j] + s[j + 1];
  31. } else {
  32. s[j] = b[j];
  33. }
  34.  
  35. u[j] = max(b[j], s[j]);
  36. max_s[j] = max(u[j], max_s[j + 1]);
  37. }
  38.  
  39. int answer = 0;
  40.  
  41. for (int i = 1; i <= n - 1; i++) {
  42. int l = p[i] + max_s[i + 1];
  43. answer = max(answer, l);
  44. }
  45.  
  46. cout << answer << endl;
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0.01s 5288KB
stdin
7
6 3 4 9 17 -1 -11
stdout
39