N=int(input()) B=list(map(int,input().split())) P=0 ANS=0 for i in range(N): if B[i]==0: P-=1 ANS+=abs(P) else: P+=(B[i]-1) ANS+=abs(P) print(ANS)