def divis(l):
    seq = []
    beg, end = 0, 0
    le = len(l)
    for i in range(1,le):
        end = i
        if l[i] < l[i-1]:
            seq.append(l[beg:end])
            beg = i
    seq.append(l[beg:end+1])
    return seq

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    a.reverse()
    cost = 0
    a = divis(a)
    for i in a:
        for j in range(len(i)-1):
            cost += i[-1] - i[j]
    print(cost)
    return 0

if __name__ == "__main__":
    solve()