/** * 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()); template struct RBST { VAL SUM_UNITY = 0; // to be set unsigned int randInt() { static unsigned int tx = 123456789, ty = 362436069, tz = 521288629, tw = 88675123; unsigned int tt = (tx ^ (tx << 11)); tx = ty; ty = tz; tz = tw; return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8))); } struct NODE { NODE *left, *right; VAL val; // the value of the node int size; // the size of the subtree VAL sum; // the value-sum of the subtree NODE() : val(0), size(1), sum(0) { left = right = NULL; } NODE(VAL v) : val(v), size(1), sum(v) { left = right = NULL; } /* additional update */ inline void update() { } /* additional lazy-propagation */ inline void push() { /* ex: reverse */ /* if (this->rev) { swap(this->left, this->right); if (this->left) this->left->rev ^= true; if (this->right) this->right->rev ^= true; this->rev = false; } */ } }; /////////////////////// // root /////////////////////// NODE* root; RBST() : root(NULL) { } RBST(NODE* node) : root(node) { } /////////////////////// // basic operations /////////////////////// /* size */ inline int size(NODE *node) { return !node ? 0 : node->size; } inline int size() { return this->size(this->root); } /* sum */ inline VAL sum(NODE *node) { return !node ? SUM_UNITY : node->sum; } /* update, push */ inline NODE* update(NODE *node) { node->size = size(node->left) + size(node->right) + 1; node->sum = sum(node->left) + sum(node->right) + node->val; node->update(); return node; } inline void push(NODE *node) { if (!node) return; node->push(); } /* lower_bound */ inline int lowerBound(NODE *node, VAL val) { push(node); if (!node) return 0; if (val <= node->val) return lowerBound(node->left, val); else return size(node->left) + lowerBound(node->right, val) + 1; } inline int lowerBound(VAL val) { return this->lowerBound(this->root, val); } /* upper_bound */ inline int upperBound(NODE *node, VAL val) { push(node); if (!node) return 0; if (val >= node->val) return size(node->left) + upperBound(node->right, val) + 1; else return upperBound(node->left, val); } inline int upperBound(VAL val) { return this->upperBound(this->root, val); } /* count */ inline int count(VAL val) { return upperBound(val) - lowerBound(val); } /* get --- k: 0-index */ inline VAL get(NODE *node, int k) { push(node); if (!node) return -1; if (k == size(node->left)) return node->val; if (k < size(node->left)) return get(node->left, k); else return get(node->right, k - size(node->left) - 1); } inline VAL get(int k) { return get(this->root, k); } /////////////////////// // merge-split /////////////////////// NODE* merge(NODE *left, NODE *right) { push(left); push(right); if (!left || !right) { if (left) return left; else return right; } if (randInt() % (left->size + right->size) < left->size) { left->right = merge(left->right, right); return update(left); } else { right->left = merge(left, right->left); return update(right); } } pair split(NODE* node, int k) { // [0, k), [k, n) push(node); if (!node) return make_pair(node, node); if (k <= size(node->left)) { pair sub = split(node->left, k); node->left = sub.second; return make_pair(sub.first, update(node)); } else { pair sub = split(node->right, k - size(node->left) - 1); node->right = sub.first; return make_pair(update(node), sub.second); } } /////////////////////// // insert-erase /////////////////////// void insert(const VAL val) { pair sub = this->split(this->root, this->lowerBound(val)); this->root = this->merge(this->merge(sub.first, new NODE(val)), sub.second); } void erase(const VAL val) { if (!this->count(val)) return; pair sub = this->split(this->root, this->lowerBound(val)); this->root = this->merge(sub.first, this->split(sub.second, 1).second); } /////////////////////// // debug /////////////////////// void print(NODE *node) { if (!node) return; push(node); print(node->left); cout << node->val << " "; print(node->right); } void print() { cout << "{"; print(this->root); cout << "}" << endl; } }; class Median { priority_queue low; priority_queue, greater<>> high; int count; public: void push(ll a) { if (count % 2 == 0) { low.push(a); } else { high.push(a); } if (low.top() > high.top()) { auto left = low.top(); low.pop(); auto right = high.top(); high.pop(); high.push(left); low.push(right); } count++; } Median() { low.push(LLONG_MIN); high.push(LLONG_MAX); count = 0; } public: ll get() { return low.top(); } }; const ll h = pow(10000, (double)2/3); class CLCMs { public: void solve(istream& cin, ostream& cout) { SPEED; ll n; cin >> n; assert(n>=1); assert(n<=10000); vector a(n); rep(i, n) { cin >> a[i]; assert(-1000000000 <= a[i]); assert(a[i] <= 1000000000); } auto ra = a; reverse(all(ra)); ll ans = 0; for(int k=1; k<=min(h,n); k++) { vector v1, v2; v1.push_back(0); v2.push_back(0); ll no1 = 0; ll no2 = 0; ll ma1 = 0; ll ma2 = 0; for(int i=0; i+k<=n; i+=k) { auto med1 = Median(); auto med2 = Median(); for(int j=i; j> vm1(q), vm2(q); rep(i, q*(h+1)) { vm1[i/(h+1)].insert(a[i]); vm2[i/(h+1)].insert(ra[i]); } // WA code for(int k=h+1; k v1(1), v2(1); ll no1 = 0; ll no2 = 0; ll ma1 = 0; ll ma2 = 0; rep(i, t) { no1 += vm1[i].get((k-1)/2); chmax(ma1, no1); v1.push_back(ma1); no2 += vm2[i].get((k-1)/2); chmax(ma2, no2); v2.push_back(ma2); } reverse(all(v2)); rep(i, t+1) { chmax(ans, (v1[i]+v2[i])*k); } rep(i, t) { int bleft = i*k; int bright = (i+1)*k; int aleft = i*(k+1); int aright = (i+1)*(k+1); if (aright > n) { vm1.pop_back(); vm2.pop_back(); continue; } for(int j=bleft; j