#include using namespace std; using lint = long long int; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector &vec, int len) { vec.resize(len); } template void ndarray(vector &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool mmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template istream &operator>>(istream &is, vector &vec){ for (auto &v : vec) is >> v; return is; } ///// This part below is only for debug, not used ///// template ostream &operator<<(ostream &os, const vector &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const deque &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template ostream &operator<<(ostream &os, const set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const pair &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template ostream &operator<<(ostream &os, const map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; ///// END ///// /* #include #include #include using namespace __gnu_pbds; // find_by_order(), order_of_key() template using pbds_set = tree, rb_tree_tag, tree_order_statistics_node_update>; template using pbds_map = tree, rb_tree_tag, tree_order_statistics_node_update>; */ using T = vector; struct SegTree { int N; int head; vector x; T defaultT; using func = function; func merger; lint _get(int begin, int end, int pos, int l, int r, lint q) const { if (r <= begin or l >= end) return 0; if (l >= begin and r <= end) { return upper_bound(ALL(x[pos]), q) - x[pos].begin(); } return _get(begin, end, 2 * pos + 1, l, (l + r) / 2, q) + _get(begin, end, 2 * pos + 2, (l + r) / 2, r, q); } SegTree(int N, T defaultT, func merger) : N(N), defaultT(defaultT), merger(merger) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; x.assign(N_tmp*2, defaultT), head = N_tmp - 1; } SegTree(const vector &vals, T defaultT, func merger) : N(vals.size()), defaultT(defaultT), merger(merger) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; x.assign(N_tmp*2, defaultT), head = N_tmp - 1; copy(vals.begin(), vals.end(), x.begin() + head); IREP(i, head) x[i] = merger(x[i * 2 + 1], x[i * 2 + 2]); } SegTree() : SegTree(0, T(), [](T, T) { return T(); }) {} void build(const vector &vals) { copy(vals.begin(), vals.end(), x.begin() + head); IREP(i, head) x[i] = merger(x[i * 2 + 1], x[i * 2 + 2]); } void update(int pos, T val) { pos += head, x[pos] = val; while (pos) pos = (pos - 1) / 2, x[pos] = merger(x[pos*2+1], x[pos*2+2]); } lint get(int begin, int end, lint q) const { return _get(begin, end, 0, 0, (int)x.size() / 2, q); } // friend ostream &operator<<(ostream &os, const SegTree &s) { os << "["; REP(i, s.N) os << s.get(i, i + 1) << ","; os << "]"; return os; } lint median(int l, int r) { int lo = -1e9 - 100, hi = 1e9 + 100; while (hi - lo > 1) { int c = (lo + hi) / 2; lint n = get(l, r, c); // c以下の個数を数える if (n + (r - l) / 2 >= r - l) hi = c; else lo = c; } return hi; } }; int N; vector> A; lint ret = 0; int main() { cin >> N; A.resize(N); REP(i, N) { lint a; cin >> a; A[i] = vector(1, a); } SegTree st(A, vector{}, [](T l, const T &r) { l.insert(l.end(), ALL(r)); sort(ALL(l)); return l; }); FOR(k, 1, N + 1) { int nmax = N / k; vector l(nmax + 1), r(nmax + 1); REP(i, nmax) { l[i + 1] = st.median(i * k, (i + 1) * k); r[i + 1] = st.median(N - (i + 1) * k, N - i * k); } REP(i, nmax) l[i + 1] += l[i], r[i + 1] += r[i]; REP(i, nmax) mmax(l[i + 1], l[i]), mmax(r[i + 1], r[i]); REP(i, nmax + 1) mmax(ret, (l[i] + r[nmax - i]) * k); } cout << ret << endl; }