import std.algorithm, std.array, std.container, std.range; import std.string, std.conv, std.math, std.random; import std.stdio, std.typecons; void main() { auto n = readln.chomp.to!int; auto ai = readln.split.map!(to!int); auto min = int.max; auto sum = ai.sum; for (auto m = 1; m * m <= sum; ++m) { auto b = ai .zip(iota(n)) .map!(a => a[0] - blocks(m, a[1])) .filter!(a => a < 0) .sum * (-1); if (ai.length < m * 2 - 1) { auto c = (m * 2 - 1) - ai.length; b += c * (c + 1) / 2; } auto r = sum - m * m + b; if (r < min) min = r; } writeln(min); } int blocks(int m, int i) { if (i < m) return i + 1; else if (i >= m && i < m * 2 - 1) return m * 2 - i - 1; else return 0; }