n = int(input())
a = list(map(int, input().split()))
i = n-1
c = 0
cn = 0
s = 0
while i >= 0 and a[i] > c:
    s += a[i] - c
    while i-1 >= 0 and a[i] == a[i-1]:
        s += a[i] - c
        i -= 1
        cn += 1
    c += cn + 1
    cn = 0
    i -= 1
print(s)
