a = int(input())
b = list(map(int, input().split()))
c = 0

for i, d in enumerate(b):
    if i == 0:
        continue
    h = b[:i]
    h.sort()
    if h[-1] > d:
        f = h[-1] - d
        b[i] += f
        c += f

print(c)