addNum = 0
n = input()
s = input().split()

for i in range(len(s)-1):
    if(s[i]>s[i+1]):
        print
        a = int(s[i])
        b = int(s[i+1])
        c =  a-b
        s[i+1] = s[i]
        addNum += c

print(addNum)