n = int(input())
l = list(map(int,input().split()))
suma,temp,j = 0,0,1
for i in range(n):
    if temp == l[n - i - 1] - i + j:
        j += 1
    else:
        j = 1
        temp = l[n - i - 1] - i
    if temp>0:
        suma += temp
    else:
        break
print(suma)