N = int(input())
A_high_list = list(map(int,input().split()))
B_list = [A_high_list[0]]
Fumidai = 0
s = 0

for n in range(N):
    if B_list[n] > A_high_list[n]:
        s = max(B_list) - A_high_list[n]
        Fumidai += s
        B_list.append(A_high_list[n])
    else:
        B_list.append(A_high_list[n])
        continue

print(Fumidai)