N = int(input())
array = list(map(int,input().split()))

su = sum(array)

def func(c,ar):
    num = max(ar[0:c])
    idx = ar.index(num)
    for i in range(idx+1,c):
        ar[i] = num 
    if idx == 0:
        print(sum(ar)-su)
        return
    func(idx,ar)
    
res = func(N,array)