#!/usr/bin/env python3

n = int(input())
widths = [int(x) for x in input().split()]
mins = [x for x in widths]

for i in range(1, n):
    mins[i] = min(mins[i], mins[i-1]+1)

for i in range(n-2, -1, -1):
    mins[i] = min(mins[i], mins[i+1]+1)

ans = 0
for i in range(n):
    ans += widths[i] - mins[i]

print(ans)
