#include using namespace std; using ll = long long; 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++) { vector v(k); for (int j = 0; j < k; j++) { v[j] = a[i * k + j]; } sort(v.begin(), v.end()); tmp += v[(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++) { vector v(k); for (int j = 0; j < k; j++) { v[j] = a[n - 1 - i * k - j]; } sort(v.begin(), v.end()); tmp += v[(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; }