import numpy as np
N = int(input())
A = list(map(int,input().split()))
min = 0
for i in range(N):
    if(i > 0) :
        if np.max(A[:i]) - A[i] > 0:
            min = min + np.max(A[:i]) - A[i]
print(min)