n = int(input()) arr = [int(input()) for x in range(n)] answer = 0 arr = list(reversed(arr)) for i in range(len(arr)-1): if arr[i+1]>=arr[i]-1: answer += arr[i+1]-(arr[i]-1) arr[i+1] = arr[i]-1 print(answer)