from collections import deque N=int(input()) B=list(map(int,input().split())) C=[i for i in range(N) if not B[i]] D=[i for i in range(N) if B[i]] C=deque(C) D=deque(D) X=0 while C: while B[D[0]]==1: _=D.popleft() c=C.popleft() X+=abs(c-D[0]) B[D[0]]-=1 B[c]+=1 print(X)