#include using namespace std; using ll = long long; int partition(vector& a, int L, int R) { ll v = a[R]; for (int i = L - 1, j = R; ; ) { i++; j--; while (a[i] < v) i++; while (a[j] > v) j--; if (i >= j) { swap(a[i], a[R]); return i; } swap(a[i], a[j]); } } void quickselect(vector& a, int L, int R, int k) { if (L >= R) return; int i = partition(a, L, R); if (k < i) quickselect(a, L, i-1, k); else quickselect(a, i+1, R, k); } ll select_kth(vector& a, int start, int end, int k) { int sz = end - start; vector v(sz); for (int i = 0; i < sz; i++) v[i] = a[i + start]; quickselect(v, 0, sz - 1, k); return v[k]; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } /* for (int i = 0; i < n; i++) { for (int j = i + 1; j <= n; j++) { cerr << i << " " << j << " " << select_kth(a, i, j, (j - i - 1) / 2) << "\n"; } }*/ ll ans = 0; for (int k = 1; k <= n; k++) { int max_num = n / k; vector memoL(max_num + 1, 0); ll tmp = 0; for (int i = 0; i < max_num; i++) { tmp += select_kth(a, i * k, (i + 1) * k, (k - 1) / 2) * k; memoL[i + 1] = max(memoL[i], tmp); } vector memoR(max_num + 1, 0); tmp = 0; for (int i = 0; i < max_num; i++) { tmp += select_kth(a, n - (i + 1) * k, n - i * k, (k - 1) / 2) * k; memoR[i + 1] = max(memoR[i], tmp); } for (int i = 0; i <= max_num; i++) { ans = max(ans, memoL[i] + memoR[max_num - i]); } } cout << ans << "\n"; return 0; }