#ifndef LOCAL #include using namespace std; #define debug(...) (void(0)) #else #include "algo/debug.h" #endif template struct MergeSortTree { private: int N; int sz; T mn, mx; std::vector> dat; int LB(const std::vector& v, const T& x) const { return std::distance(v.begin(), std::lower_bound(v.begin(), v.end(), x)); } public: MergeSortTree() = default; MergeSortTree(const std::vector& v) : N((int)v.size()) { mn = std::numeric_limits::max(); mx = std::numeric_limits::lowest(); sz = (int)std::bit_ceil((unsigned int)N); dat.assign(2 * sz, {}); for (int i = 0; i < N; i++) { dat[i + sz].push_back(v[i]); mn = std::min(mn, v[i]); mx = std::max(mx, v[i]); } for (int i = sz - 1; i >= 1; i--) { dat[i].resize(dat[2 * i].size() + dat[2 * i + 1].size()); std::merge(dat[2 * i].begin(), dat[2 * i].end(), dat[2 * i + 1].begin(), dat[2 * i + 1].end(), dat[i].begin()); } }; int count_lt(int l, int r, const T& x) const { assert(0 <= l && l <= r && r <= N); l += sz; r += sz; int ans = 0; while (l < r) { if (l & 1) ans += LB(dat[l++], x); if (r & 1) ans += LB(dat[--r], x); l >>= 1; r >>= 1; } return ans; } int count_le(int l, int r, const T& x) const { return count_lt(l, r, x + 1); } int count_gt(int l, int r, const T& x) const { return r - l - count_le(l, r, x); } int count_ge(int l, int r, const T& x) const { return r - l - count_lt(l, r, x); } T kth(int l, int r, const T& k) const { T lo = mn, hi = mx + T{1}; while (hi > lo + 1) { T mi = (lo + hi) >> 1; (count_lt(l, r, mi) <= k ? lo : hi) = mi; } return lo; } }; #include using S = int64_t; S op(S a, S b) { return max(a, b); } S e() { return -1e18; } void solve() { int N; cin >> N; vector A(N), B(N); for (int i = 0; i < N; i++) { cin >> A[i]; B[N - i - 1] = A[i]; } MergeSortTree A_freq(A); MergeSortTree B_freq(B); constexpr S o = S(0); int64_t ans = 0; for (int K = 1; K <= N; K++) { int sz = (N + K - 1) / K; vector pref, suff; pref.reserve(sz); suff.reserve(sz); { int med = (K - 1) / 2; for (int i = 0; i + K <= N; i += K) { if (i == 0) { pref.push_back(A_freq.kth(i, i + K, med)); suff.push_back(B_freq.kth(i, i + K, med)); } else { pref.push_back(pref.back() + A_freq.kth(i, i + K, med)); suff.push_back(suff.back() + B_freq.kth(i, i + K, med)); } } } atcoder::segtree pp(pref), ss(suff); for (unsigned i = 0; i <= pref.size(); i++) { int64_t res = max(o, pp.prod(0, i)) + max(o, ss.prod(0, suff.size() - i)); if (res < 0) continue; ans = max(ans, res * K); } } cout << ans << endl; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int tt = 1; // std::cin >> tt; while (tt--) { solve(); } }