#include using namespace std; using ll = long long; // 配列aの[start, end)の中でk番目に小さい値を求める template T select_kth(vector& a, int start, int end, int k) { vector v(a.begin() + start, a.begin() + end); nth_element(v.begin(), v.begin() + k, v.end()); 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]; } 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; }