/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author aajisaka */ #include using namespace std; void debug_out() { cerr << endl; } template void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr) #define rep(i,n) for(int i=0; i<(int)(n); i++) #define all(v) v.begin(), v.end() template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using P = pair; constexpr double PI = 3.14159265358979323846; mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count()); // Original: https://github.com/beet-aizu/library struct FullyIndexableDictionary{ int len,blk; vector bit; vector sum; FullyIndexableDictionary(){} FullyIndexableDictionary(int len) :len(len),blk((len+31)>>5),bit(blk,0),sum(blk,0){} void set(int k){ bit[k>>5]|=1u<<(k&31); } void build(){ sum[0]=0; for(int i=1;i>5]>>(k&31))&1); } int rank(int k){ return sum[k>>5]+__builtin_popcount(bit[k>>5]&((1u<<(k&31))-1)); } int rank(bool v,int k){ return (v?rank(k):k-rank(k)); } int select(bool v,int k){ if(k<0||rank(v,len)<=k) return -1; int l=0,r=len; while(l+1>1; if(rank(v,m)>=k+1) r=m; else l=m; } return r-1; } int select(bool v,int i,int l){ return select(v,i+rank(v,l)); } }; template struct WaveletMatrix{ int len; FullyIndexableDictionary mat[MAXLOG]; int zs[MAXLOG],buff1[MAXLOG],buff2[MAXLOG]; static const T npos=-1; int freq_dfs(int d,int l,int r,T val,T a,T b){ if(l==r) return 0; if(d==MAXLOG) return (a<=val&&val data){ len=data.size(); vector ls(len),rs(len); for(int dep=0;dep>(MAXLOG-(dep+1)))&1; if(k) rs[q++]=data[i],mat[dep].set(i); else ls[p++]=data[i]; } zs[dep]=p; mat[dep].build(); swap(ls,data); for(int i=0;i>(MAXLOG-(dep+1)))&1; l=mat[dep].rank(bit,l)+zs[dep]*bit; r=mat[dep].rank(bit,r)+zs[dep]*bit; } return r-l; } // return the position of k-th v int select(T v,int k){ rank(v,len); for(int dep=MAXLOG-1;dep>=0;dep--){ bool bit=(v>>(MAXLOG-(dep+1)))&1; k=mat[dep].select(bit,k,buff1[dep]); if(k>=buff2[dep]||k<0) return -1; k-=buff1[dep]; } return k; } int select(T v,int k,int l){ return select(v,k+rank(v,l)); } // return k-th largest value in [l,r) T quantile(int l,int r,int k){ if(r-l<=k||k<0) return -1; T res=0; for(int dep=0;depk){ l=p+zs[dep]; r=q+zs[dep]; res|=T(1)<<(MAXLOG-(dep+1)); }else{ k-=(q-p); l-=p; r-=q; } } return res; } T rquantile(int l,int r,int k){ return quantile(l,r,r-l-k-1); } // return number of points in [left, right) * [lower, upper) int rangefreq(int left,int right,T lower,T upper){ return freq_dfs(0,left,right,0,lower,upper); } pair ll(int l,int r,T v){ int res=0; for(int dep=0;dep>(MAXLOG-(dep+1)))&1; if(bit) res+=r-l+mat[dep].rank(bit,l)-mat[dep].rank(bit,r); l=mat[dep].rank(bit,l)+zs[dep]*bit; r=mat[dep].rank(bit,r)+zs[dep]*bit; } return make_pair(res,r-l); } int lt(int l,int r,T v){ auto p=ll(l,r,v); return p.first; } int le(int l,int r,T v){ auto p=ll(l,r,v); return p.first+p.second; } T succ(int l,int r,T v){ int k=le(l,r,v); return k==r-l?npos:rquantile(l,r,k); } T pred(int l,int r,T v){ int k=lt(l,r,v); return k?rquantile(l,r,k-1):npos; } }; class project_manager { public: void solve(istream& cin, ostream& cout) { SPEED; int n; cin >> n; vector a(n); const int C = 1e+9; rep(i, n) { cin >> a[i]; a[i] += C; } WaveletMatrix wm(a); ll ans = 0; for(int k=1; k<=n; k++) { vector left, right; left.push_back(0); right.push_back(0); ll now = 0; for(int l=0; l+k<=n; l+=k) { now += wm.rquantile(l, l+k, (k-1)/2)-C; debug(now); left.push_back(max(now, left.back())); } now = 0; for(int r=n; r-k>=0; r-=k) { now += wm.rquantile(r-k, r, (k-1)/2)-C; right.push_back(max(now, right.back())); } int s = left.size(); rep(i, s) { chmax(ans, (left[i] + right[s-1-i])*k); } } cout << ans << endl; } }; signed main() { project_manager solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }