#include #include #include #include #include #include #define llint long long using namespace std; typedef pair P; typedef pair Q; llint n, B; llint a[10005]; vector qvec; vector

avec; multiset head, tail; vector lvec, rvec; void adjust(llint m) { while((int)head.size() > (m+1)/2){ auto it = head.end(); it--; tail.insert(*it), head.erase(it); } while((int)head.size() < (m+1)/2){ head.insert(*tail.begin()), tail.erase(tail.begin()); } } void add(llint x) { llint m = head.size() + tail.size(); if(head.size() == 0) head.insert(x); else{ auto it = head.end(); it--; if(*it >= x) head.insert(x); else tail.insert(x); } m++; adjust(m); } void erase(llint x) { llint m = head.size() + tail.size(); if(head.count(x)) head.erase(head.lower_bound(x)); else tail.erase(tail.lower_bound(x)); m--; adjust(m); } llint get() { auto it = head.end(); it--; return *it; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; B = sqrt(n); llint qid = 0; for(int k = 1; k <= n; k++){ llint l, r; for(int i = 1; k*i <= n; i++){ l = (i-1)*k+1, r = i*k; qvec.push_back(Q(P(l/B, r), P(l, qid++))); l = n-i*k+1, r = n-(i-1)*k; qvec.push_back(Q(P(l/B, r), P(l, qid++))); } } sort(qvec.begin(), qvec.end()); llint l = qvec[0].second.first, r = qvec[0].first.second; for(int i = l; i <= r; i++) add(a[i]); avec.push_back(make_pair(qvec[0].second.second, get())); for(int i = 1; i < qvec.size(); i++){ llint nl = qvec[i].second.first, nr = qvec[i].first.second; while(l < nl) erase(a[l]), l++; while(l > nl) l--, add(a[l]); while(r < nr) r++, add(a[r]); while(r > nr) erase(a[r]), r--; llint qid = qvec[i].second.second; avec.push_back(make_pair(qid, get())); } sort(avec.begin(), avec.end()); llint ans = 0; qid = 0; for(int k = 1; k <= n; k++){ lvec.clear(), rvec.clear(); lvec.push_back(0), rvec.push_back(0); for(int i = 1; k*i <= n; i++){ lvec.push_back(avec[qid++].second); rvec.push_back(avec[qid++].second); } int m = (int)lvec.size() - 1; for(int i = 1; i <= m; i++) lvec[i] += lvec[i-1], rvec[i] += rvec[i-1]; llint mx = 0; for(int i = 0; i <= m; i++){ mx = max(mx, lvec[i]); ans = max(ans, k*(mx + rvec[m-i])); } } cout << ans << endl; return 0; }