/** * 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()); class CLCMs { public: void solve(istream& cin, ostream& cout) { SPEED; int n; cin >> n; vector a(n); rep(i, n) { cin >> a[i]; } vector

vec; for(int k=1; k<=n; k++) { for(int i=0; i+k<=n; i+=k) { vec.emplace_back(i,i+k); } for(int i=n; i-k>=0; i-=k) { vec.emplace_back(i-k, i); } } sort(all(vec)); vec.erase(unique(all(vec)), vec.end()); int s = vec.size(); map mp; priority_queue que1; priority_queue, greater<>> que2; int now = -1; int k = 0; int cnt = 0; for(int idx = 0; idx < s; idx++) { int i = vec[idx].first; int j = vec[idx].second; if (now != i) { while(!que1.empty()) que1.pop(); while(!que2.empty()) que2.pop(); que1.push(INT_MIN); que2.push(INT_MAX); k = i; cnt = 0; } while(k < j) { if (cnt%2==0) { que1.push(a[k]); } else { que2.push(a[k]); } if (que1.top() > que2.top()) { int c1 = que1.top(); que1.pop(); int c2 = que2.top(); que2.pop(); que2.push(c1); que1.push(c2); } cnt++; k++; } mp[P(i, j)] = que1.top(); } ll ans = 0; for(int j=1; j<=n; j++) { ll no = 0; ll ma = 0; vector v1, v2; v1.push_back(0); for(int i=0; i+j<=n; i+=j) { no += mp[P(i, i+j)]; chmax(ma, no); v1.push_back(ma); } no = 0; ma = 0; v2.push_back(0); for(int i=n; i-j>=0; i-=j) { no += mp[P(i-j, i)]; chmax(ma, no); v2.push_back(ma); } reverse(all(v2)); int r = v1.size(); rep(i, r) { debug(i, v1[i], v2[i]); chmax(ans, (v1[i]+v2[i])*j); } } cout << ans << endl; } }; signed main() { CLCMs solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }