for _ in range(1):
    n=int(input())
    a=list(map(int,input().split()))
    k=a[0]
    ans=0
    
    for i in range(n):
        if i==0:
            m=a[i]
            a[i]=0
        else:
            m=max(m,a[i])
            a[i]=m-a[i]
    u=0
    for i in range(n):
        ans+=max(0,a[i]-u)
        u=a[i]
    print(ans)