N = int(input())
A = [int(x) for x in raw_input().split()[:N]]
temp = 0
overall = 0
# for i in range(N):
#     S = int(input())
#     A.append(S)
for i in range(0, len(A) -1 ):
    if A[i] > A[i+1]:
        temp = A[i] - A[i+1]
        A[i + 1] += 1
        overall += temp
print(overall)
