from heapq import * N = int(input()) B = list(map(int,input().split())) zero = [] stock = [] count = 0 for i in range(N): if B[i] == 0: heappush(zero,i) else: heappush(stock,(i,B[i])) while zero != [] and stock != []: z = heappop(zero) s,i = heappop(stock) if i == 1: heappush(zero,s) else: heappush(stock,(s,i-1)) count += abs(z-s) print(count)