import math n = int(input()) a = list(map(int, input().split())) s = sum(a) min_cost = float('inf') max_m = int(math.isqrt(s)) # Maximum possible m such that m^2 <= s for m in range(1, max_m + 1): k = 2 * m - 1 e_pyramid = 0 d_pyramid = 0 for j in range(1, k + 1): distance = abs(j - m) b_j = m - distance if j <= n: available = a[j - 1] else: available = 0 # Calculate excess and deficit excess = available - b_j if excess > 0: e_pyramid += excess deficit = b_j - available if deficit > 0: d_pyramid += deficit # Calculate sum of non-pyramid columns (j > k) sum_non = 0 if k < n: for j in range(k + 1, n + 1): sum_non += a[j - 1] total_available_surplus = e_pyramid + sum_non # Check if surplus can cover the deficit if total_available_surplus >= d_pyramid: current_cost = e_pyramid + sum_non if current_cost < min_cost: min_cost = current_cost # If no valid m found (unlikely given the constraints), default to 0 print(min_cost if min_cost != float('inf') else 0)