fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. void sol(){
  5. int n;
  6. cin >> n;
  7. int a[n];
  8. for(int &i:a) cin >> i;
  9. int l[n], r[n];
  10. l[0] = a[0];
  11. r[n - 1] = a[n - 1];
  12. for(int i = 1; i < n; i++) l[i] = max(l[i - 1] + a[i], a[i]);
  13. for(int i = n - 2; i >= 0; i--) r[i] = max(r[i + 1] + a[i], a[i]);
  14. int ans = -1e9;
  15. for(int i = 0; i < n; i++) ans = max(ans, l[i]);
  16. for(int i = 0; i < n; i++) ans = max(ans, r[i]);
  17. for(int i = 1; i < n - 1; i++) ans = max(ans, r[i + 1] + l[i - 1]);
  18. cout << ans << "\n";
  19. }
  20. signed main(){
  21. ios_base::sync_with_stdio(false);
  22. cin.tie(NULL);
  23. cout.tie(NULL);
  24. int t = 1;
  25. // cin >> t;
  26. while(t--){
  27. sol();
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
9222167041858206806