#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #ifdef LOCAL #define dbg(x) cerr << #x " = " << x << endl; #include "pretty_print.h" #else #define dbg(x) #endif typedef long double ld; typedef long long ll; typedef unsigned long long ull; #define snd second #define fst first template T sqr(T x) { return x * x; } template T abs(T x) { return x < 0? -x : x; } template T gcd(T a, T b) { return b? gcd(b, a % b) : a; } template bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; } template bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; } #define X first #define ID second #define P second const int LIMIT_ITER = 2e+8; int main(int /* argc */, char** /* argv */) { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifdef LOCAL assert(freopen("inp", "r", stdin)); assert(freopen("out", "w", stdout)); #endif int n; while (cin >> n) { int max_iter = LIMIT_ITER / n; dbg(max_iter); ll s[n + 1]; ll q[n + 1]; s[0] = 0; q[0] = 0; for (int i = 1; i <= n; ++i) { ll x; cin >> x; s[i] = s[i - 1] + x; q[i] = q[i - 1] + x * x; } ll answer = 0; set < pair > b; for (int r = 0; r <= n; ++r) { auto it = b.rbegin(); for (auto& it : b) { int l = it.second; ll res = sqr(s[r]) - 2 * s[r] * s[l] + sqr(s[l]) - q[r] + q[l]; chmax(answer, res); } b.insert(make_pair(sqr(s[r]) + q[r], r)); while ((int)b.size() >= max_iter) { b.erase(b.begin()); } } cout << answer / 2 << endl; } cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl; return 0; }