SHARE
TWEET

Untitled

double112233 Jan 27th, 2020 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4. const int N=1000000+10;
  5. int n,arr[N],ans=-9000000000000000000,lmx[N],rmx[N],ldp[N],rdp[N];
  6. signed main() {
  7.     cin>>n;
  8.     for (int i=1; i<=n; i++) {
  9.         cin>>arr[i];
  10.     }
  11.     lmx[1]=ldp[1]=arr[1];
  12.     rmx[n]=rdp[n]=arr[n];
  13.     for (int i=2; i<=n; i++) {
  14.         ldp[i]=max(ldp[i-1]+arr[i], arr[i]);
  15.         lmx[i]=max(lmx[i-1], ldp[i]);
  16.     }
  17.     for (int i=n-1; i>0; i--) {
  18.         rdp[i]=max(rdp[i+1]+arr[i], arr[i]);
  19.         rmx[i]=max(rdp[i+1], rdp[i]);
  20.     }
  21.     for (int i=2; i<n; i++) {
  22.         ans=max(ans, lmx[i-1]+rmx[i+1]);
  23.     }
  24.     cout<<ans;
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top