#include int ri() { int n; scanf("%d", &n); return n; } // dummy one struct AVL { struct Node { Node *l; Node *r; int val; int size; int height; Node *fetch() { size = 1 + l->size + r->size; height = 1 + std::max(l->height, r->height); return this; } Node *rotate_l() { Node *new_root = r; r = new_root->l; new_root->l = this; return fetch(), new_root->fetch(); } Node *rotate_r() { Node *new_root = l; l = new_root->r; new_root->r = this; return fetch(), new_root->fetch(); } int height_diff() { return l->height - r->height; } Node *balance() { int dif = height_diff(); if (dif == 2) { if (l->height_diff() < 0) l = l->rotate_l(); return rotate_r(); } else if (dif == -2) { if (r->height_diff() > 0) r = r->rotate_r(); return rotate_l(); } return fetch(); } }; Node nodes[10001]; int head = 0; Node * const NONE = nodes; Node *root = NONE; AVL() { nodes[head++] = {NONE, NONE, 0, 0, 0}; } size_t size_ = 0; private : Node *insert(Node *node, int val) { if (node == NONE) return &(nodes[head++] = {NONE, NONE, val, 1, 1}); if (val < node->val) node->l = insert(node->l, val); else node->r = insert(node->r, val); return node->balance(); } Node *removed_tmp; Node *remove_rightest(Node *node) { if (node->r != NONE) { node->r = remove_rightest(node->r); return node->balance(); } else return (removed_tmp = node)->l; } Node *remove(Node *node, int val) { if (val < node->val) node->l = remove(node->l, val); else if (val > node->val) node->r = remove(node->r, val); else { if (node->l == NONE) return node->r; node->l = remove_rightest(node->l); removed_tmp->l = node->l; removed_tmp->r = node->r; return removed_tmp->balance(); } return node->balance(); } public : int operator [] (int i) { for (Node *cur = root; ; ) { int lsize = cur->l->size; if (i < lsize) cur = cur->l; else if (i > lsize) cur = cur->r, i -= lsize + 1; else return cur->val; } } void insert(int val) { root = insert(root, val); size_++; } void remove(int val) { root = remove(root, val); size_--; } size_t size() { return size_; } }; int main() { int n = ri(); int a[n]; for (int i = 0; i < n; i++) a[i] = ri(); std::vector median_left[n + 1]; std::vector median_right[n + 1]; for (int i = 0; i <= n; i++) { median_left[i].resize(n / (i + 1)); median_right[i].resize(n / (i + 1)); } int sq = sqrt(n); for (int i = 0; i < sq; i++) { { int head = 0; int tail = 0; AVL set; for (int j = 0; i < (int) median_left[j].size(); j++) { // j + 1 while (head < (j + 1) * (i + 1)) set.insert(a[head++]); while (tail < (j + 1) * i) set.remove(a[tail++]); assert(set.size()); median_left[j][i] = set[(set.size() - 1) / 2]; } } { int head = n - 1; int tail = n - 1; AVL set; for (int j = 0; i < (int) median_left[j].size(); j++) { // j + 1 while (head >= n - (j + 1) * (i + 1)) set.insert(a[head--]); while (tail >= n - (j + 1) * i) set.remove(a[tail--]); assert(set.size()); median_right[j][i] = set[(set.size() - 1) / 2]; } } } for (int i = 0; i <= n; i++) { for (int j = sq; j < (int) median_left[i].size(); j++) { std::vector sorted; for (int k = j * (i + 1); k < (j + 1) * (i + 1); k++) sorted.push_back(a[k]); std::sort(sorted.begin(), sorted.end()); median_left[i][j] = sorted[(sorted.size() - 1) / 2]; } for (int j = sq; j < (int) median_right[i].size(); j++) { std::vector sorted; for (int k = n - (j + 1) * (i + 1); k < n - j * (i + 1); k++) sorted.push_back(a[k]); std::sort(sorted.begin(), sorted.end()); median_right[i][j] = sorted[(sorted.size() - 1) / 2]; } } int64_t res = 0; for (int i = 0; i < n; i++) { int max_index_sum = (int) median_left[i].size() - 2; for (int j = 0; j + 1 < (int) median_left[i].size(); j++) median_left[i][j + 1] += median_left[i][j]; for (int j = 0; j + 1 < (int) median_left[i].size(); j++) median_left[i][j + 1] = std::max(median_left[i][j + 1], median_left[i][j]); for (int j = 0; j + 1 < (int) median_right[i].size(); j++) median_right[i][j + 1] += median_right[i][j]; for (int j = 0; j + 1 < (int) median_right[i].size(); j++) median_right[i][j + 1] = std::max(median_right[i][j + 1], median_right[i][j]); int64_t cur = 0; for (int j = 0; j + 1 < (int) median_left[i].size(); j++) cur = std::max(cur, median_left[i][j] + median_right[i][max_index_sum - j]); cur = std::max(cur, median_left[i].back()); cur = std::max(cur, median_right[i].back()); res = std::max(res, cur * (i + 1)); } std::cout << res << std::endl; return 0; }