#ifdef __LOCAL #define _GLIBCXX_DEBUG #include "debug2.h" #endif #ifndef __LOCAL #define show(...) #define SET_GROUP(x) #define SET_MAXCNT(x) #endif #include using namespace std; //#include //using namespace atcoder; //#include //using bigint = boost::multiprecision::cpp_int; #define SIZE(x) (int)(x.size()) #define ALL(x) x.begin(),x.end() using ll = long long; //int di[] = {1, 0, -1, 0, 1, -1, -1, 1}; //int dj[] = {0, 1, 0, -1, 1, 1, -1, -1}; templatebool chmin(T& x,T y){if(x>y){x=y;return true;}return false;} templatebool chmax(T& x,T y){if(xvectormatrix(int n1,T val){return vector(n1,val);} templatevector>matrix(int n1,int n2,T val){return vector>(n1,matrix(n2,val));} templatevector>>matrix(int n1,int n2,int n3,T val){return vector>>(n1,matrix(n2,n3,val));} templatevector>>>matrix(int n1,int n2,int n3,int n4,T val){return vector>>>(n1,matrix(n2,n3,n4,val));} templateT sum(vectorx){T r=0;for(auto e:x)r+=e;return r;} templateT max(vectorx){T r=x[0];for(auto e:x)chmax(r,e);return r;} templateT min(vectorx){T r=x[0];for(auto e:x)chmin(r,e);return r;} templatesettoSet(vectorx){set r;for(auto e:x)r.emplace(e);return r;} templatemaptoMap(vectorx){mapmp;for(auto e:x)mp[e]++;return mp;} templatevectortoVector(setx){vector r;for(auto e:x)r.push_back(e);return r;} templatepair operator+(pairx,pairy){return make_pair(x.first+y.first,x.second+y.second);} templatepair operator-(pairx,pairy){return make_pair(x.first-y.first,x.second-y.second);} templateistream&operator>>(istream&is,tuple&x){cin>>get<0>(x)>>get<1>(x);return is;} templateistream&operator>>(istream&is,tuple&x){cin>>get<0>(x)>>get<1>(x)>>get<2>(x);return is;} templateistream&operator>>(istream&is,pair&x){cin>>x.first>>x.second;return is;} templateistream&operator>>(istream&is,vector&x){for(auto&&e:x)is>>e;return is;} templateistream&operator>>(istream&is,vector>&x){for(auto&&e:x)for(auto&&f:e)is>>f;return is;} templateostream&operator<<(ostream&os,const vector&x){for(auto e:x)os<ostream&operator<<(ostream&os,const vector>&x){for(auto e:x){for(auto f:e)os<& a){ int n = SIZE(a); vector sumA(n + 1, 0), sumB(n + 1, 0); for(int i = 0; i < n; ++i){ sumA[i + 1] = sumA[i] + a[i]; } for(int i = n - 1; i >= 0; --i){ sumB[i] = sumB[i + 1] + a[i]; } vector preMin(n + 1, 0), backMin(n + 1, 0); vector preMin2(n + 1, 0), backMin2(n + 1, 0); for(int i = 1; i <= n; ++i){ chmin(preMin[i], min(sumA[i], preMin[i - 1])); preMin2[i] = sumA[i] - preMin[i - 1]; chmax(preMin2[i], preMin2[i - 1]); } for(int i = n - 1; i >= 0; --i){ chmin(backMin[i], min(sumB[i], backMin[i + 1])); backMin2[i] = sumB[i] - backMin[i + 1]; chmax(backMin2[i], backMin2[i + 1]); } show(a); show(sumA); show(preMin); show(preMin2); show(sumB); show(backMin); show(backMin2); ll ans = -1LL << 60; for(int i = 1; i < n; ++i){ ll maxFront = preMin2[i]; ll maxBack = backMin2[i]; ll tmpans = maxFront * maxBack; if(maxFront >= 0 && maxBack >= 0){ chmax(ans, tmpans); } show(i, maxFront, maxBack, tmpans); } return ans; } int main(){ int n; cin >> n; vector a(n); cin >> a; if(n == 2){ cout << a[0] * a[1] << endl; return 0; } ll ans = 0; chmax(ans, solve(a)); for(auto&& e : a) e *= -1; show("----------"); chmax(ans, solve(a)); cout << ans << endl; return 0; }