fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define fi first
  4. #define se second
  5. #define ll long long
  6. #define el cout<<"\n"
  7. #define sz(x) (int)(x).size()
  8. #define all(x) (x).begin(),(x).end()
  9. #define f0(i,n) for(int i=0;i<n;i++)
  10. #define f1(i,n) for(int i=1;i<=n;i++)
  11. #define fz(i,a,n,z) for(int i=a;i<n;i+=z)
  12. #define rep(i,a,n,z) for(int i=a;i>n;i-=z)
  13. #define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  14. #define file(name) freopen(name".inp","r",stdin);freopen(name".out","w",stdout);
  15. const int N = 2e5 + 5;
  16. const int maxn = 1e6 + 1;
  17. const int MOD = 1e9 + 7;
  18. int main() {
  19. faster
  20. int n;
  21. cin >> n;
  22. ll dpLeft[n + 5], dpRight[n + 5], A[n + 5];
  23. dpLeft[0] = dpRight[n + 1] = INT_MIN;
  24. ll sum = 0;
  25. for (int i = 1; i <= n; ++i) {
  26. cin >> A[i];
  27. sum = max(A[i], A[i] + sum);
  28. dpLeft[i] = max(dpLeft[i - 1], sum);
  29. }
  30. sum = 0;
  31. for (int i = n; i >= 1; --i) {
  32. sum = max(A[i], A[i] + sum);
  33. dpRight[i] = max(dpRight[i + 1], sum);
  34. }
  35. ll res = INT_MIN;
  36. for (int i = 1; i < n; ++i) {
  37. res = max(res, dpLeft[i] + dpRight[i + 1]);
  38. }
  39. cout << res;
  40.  
  41.  
  42.  
  43. }
  44. /*-----------------------END-----------------------*/
  45.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty