import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, 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)); } // floor(a / b) Int divFloor(Int)(Int a, Int b) { return a / b - (((a ^ b) < 0 && a % b != 0) ? 1 : 0); } // ceil(a / b) Int divCeil(Int)(Int a, Int b) { return a / b + (((a ^ b) > 0 && a % b != 0) ? 1 : 0); } enum INF = 10L^^18; // a/b vs c/d int cmp(long a, long b, long c, long d) { const x = divFloor(a, b); const y = divFloor(c, d); if (x != y) return (x < y) ? -1 : (x > y) ? +1 : 0; a -= b * x; c -= d * y; if (b < 0) { a *= -1; b *= -1; } if (d < 0) { c *= -1; d *= -1; } return (a * d < c * b) ? -1 : (a * d > c * b) ? +1 : 0; } void main() { try { for (; ; ) { const N = readInt(); auto A = new long[N + 2]; foreach (i; 1 .. N + 1) { A[i] = readLong(); } auto S = new long[N + 3]; foreach (i; 0 .. N + 2) { S[i + 1] = S[i] + A[i]; } auto dp = new long[][](N + 1, N + 2); foreach (k; 0 .. N + 1) { dp[k][] = INF; } dp[0][0] = 0; foreach (k; 1 .. N + 1) { /* dp[k][i] = \min_{0<=j= 2 && cmp(-(que[tail - 1][0] - que[tail - 2][0]), que[tail - 1][1] - que[tail - 2][1], -(a0 - que[tail - 2][0]), a1 - que[tail - 2][1]) >= 0; --tail) {} que[tail++] = Entry(a0, a1); } for (; tail - head >= 2 && eval(que[head], S[i]) >= eval(que[head + 1], S[i]); ++head) {} if (tail - head >= 1) { dp[k][i] = S[i]^^2 + eval(que[head], S[i]); } } } debug { foreach (k; 0 .. N + 1) { writeln(dp[k]); } } foreach_reverse (k; 1 .. N + 1) { writeln(dp[k][N + 1]); } } } catch (EOFException e) { } }