def main():

    N = int(eval(input()))

    A = list(map(int, input().split()))

    h = A[0]

    ans = 0



    for i in range(N):

        if A[i] < h:

            ans += h - A[i]

        if A[i] > h:

            h = A[i]



    print(ans)





if __name__ == "__main__":

    main()

def main():

    N = int(eval(input()))

    A = list(map(int, input().split()))



    tallest = A[0]

    ans = 0



    for height in A:

        if height < tallest:

            ans += tallest - height

        elif height > tallest:

            tallest = height



    print(ans)





if __name__ == "__main__":

    main()

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    h = A[0]
    ans = 0
    for i in range(N):
        if A[i] < h:
            ans += h - A[i]
        if A[i] > h:
            h = A[i]
    print(ans)


if __name__ == "__main__":
    main()

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    tallest = A[0]
    ans = 0
    for height in A:
        if height < tallest:
            ans += tallest - height
        elif height > tallest:
            tallest = height
    print(ans)


if __name__ == "__main__":
    main()

-    h = A[0] +    tallest = A[0] -    for i in range(N): -        if A[i] < h: -            ans += h - A[i] -        if A[i] > h: -            h = A[i] +    for height in A: +        if height < tallest: +            ans += tallest - height +        elif height > tallest: +            tallest = height