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

for i, d in enumerate(b):
    if i == 0:
        continue
    flag = False
    f = 0
    h = b[:i]
    h.sort(reverse=True)
    for z in h:
        if z > d:
            flag = True
            f = z - d
            break
    if flag:
        b[i] += f
        c += f

print(c)