N=int(input()) L = list(map(int, input().split())) for i in range(N): L[i] -=1 ans = 0 temp = 0 for i in range(N): if L[i]<0: for j in range(max(i+1,temp),N): if L[j]>0: L[i]=0 L[j]-=1 ans +=j-i temp = j break if L[i]>0: j = i+1 while L[i]>0 and j