N = int(input())
A = list(map(int, input().split()))
count = 0
for i in range (0,N-1):
    while(A[i]>A[i+1]):
        count += 1
        A[i+1] += 1
    if(A[i]<=A[i+1]):
        continue

print(count)