n = int(input())
a = [int(i) for i in input().split()]
cur = 0
ans = 0
ind = n - 1
prev = a[ind - 1] + 1
while ind >= 0:
    x = a[ind]
    if x - cur <= 0:
        ind -= 1
        continue
    st = 1
    while ind > 0 and x == a[ind - 1]:
        ind -= 1
        st += 1
#	print(ind, x, cur, st, ans)
    ans += (x - cur) * st
    cur += st
    ind -= 1
print(ans)