N = int(input()) B = list(map(int,input().split())) ans = 0 L = [] for i in range(N): if B[i]==0: L.append(i) elif B[i]>1: d = B[i]-1 while d>0 and L: j = L.pop() B[j] = 1 ans += i-j d -= 1 if d>0: B[i+1] += d ans += d print(ans)