#include using namespace std; using Int = long long; template inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template inline void chmax(T1 &a,T2 b){if(a 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 l(len),r(len); for(Int dep=0;dep>(MAXLOG-(dep+1)))&1; if(k) r[q++]=data[i],mat[dep].set(i); else l[p++]=data[i]; } zs[dep]=p; mat[dep].build(); swap(l,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; } }; //INSERT ABOVE HERE signed main(){ Int n; cin>>n; vector as(n); for(Int i=0;i>as[i]; const Int OFS = 1e9; for(Int &a:as) a+=OFS; WaveletMatrix wm(as); Int ans=0; for(Int k=1;k<=n;k++){ Int p=(k-1)/2; vector ls,rs; for(Int i=0;i+k<=n;i+=k) ls.emplace_back((wm.rquantile(i,i+k,p)-OFS)*k); for(Int i=n;i-k>=0;i-=k) rs.emplace_back((wm.rquantile(i-k,i,p)-OFS)*k); /* for(Int l:ls) cout< dp(sz+1,0),pd(sz+1,0); for(Int i=0,sum=0;i0;i--){ sum+=rs[sz-i]; chmax(pd[i-1],pd[i]); chmax(pd[i-1],sum); } /* cout<