n =2 # nは入力回数
num_list = []
for i in range(n):
    num_list.append(list(map(int,input().split())))
# print(num_list)

A = num_list[1]
step = 0
num_min = 1000

for i in range(len(A)-1):
    if A[i] > A[i+1]:
        for j in range(A[i] - A[i+1]):
            step += 1
            A[i+1] += 1
    else: continue
    
print(step)