import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10L^^18; int N; long[] A; long[] ASum; alias Entry = Tuple!(long, int); Entry[][] es; long f(int i, int j) { return (cast(long)(j - i))^^2 + (ASum[j] - ASum[i]); } void solve(int dir, int iL, int iR, int jL, int jR) { if (iL == iR) return; const iMid = (iL + iR) / 2; long opt = INF; int jm = jL; foreach (j; jL .. jR) { if (iMid < j) { if (chmin(opt, f(iMid, j))) { jm = j; } } } if (dir == 0) { es[iMid] ~= Entry(opt, jm); } else { es[N - jm] ~= Entry(opt, N - iMid); } solve(dir, iL, iMid, jL, jm + 1); solve(dir, iMid + 1, iR, jm, jR); } void main() { try { for (; ; ) { N = readInt(); A = new long[N]; foreach (i; 0 .. N) { A[i] = readLong(); } es = new Entry[][N]; foreach (dir; 0 .. 2) { ASum = new long[N + 1]; foreach (i; 0 .. N) { ASum[i + 1] = ASum[i] + A[i]; } solve(dir, 0, N, 0, N + 1); A.reverse(); } debug { writeln("es = ", es); } BinaryHeap!(Array!Entry, "a > b") que; auto ans = new long[N]; foreach (i; 0 .. N) { foreach (e; es[i]) { que.insert(e); } for (; !que.empty; que.removeFront) { if (i < que.front[1]) { ans[i] = que.front[0]; break; } } } foreach (i; 0 .. N) { writeln(ans[i]); } } } catch (EOFException e) { } }