n = int(eval(input()))

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



maxi = 0

humi = 0

maxi = an[0]

for i in range(1, n):

    if an[i] < maxi:

        hu = maxi -an[i]

        humi += hu

    maxi = max(maxi, an[i])



print(humi)
import sys

input = sys.stdin.readline

def main():

    n = int(eval(input()))

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

    humi = 0

    maxi = an[0]

    for i in range(1, n):

        if an[i] < maxi:

            hu = maxi -an[i]

            humi += hu

        maxi = max(maxi, an[i])



    print(humi)

if __name__ == '__main__':

    main()
n = int(eval(input()))
an = list(map(int, input().split()))
maxi = 0
humi = 0
maxi = an[0]
for i in range(1, n):
    if an[i] < maxi:
        hu = maxi - an[i]
        humi += hu
    maxi = max(maxi, an[i])
print(humi)

import sys

input = sys.stdin.readline


def main():
    n = int(eval(input()))
    an = list(map(int, input().split()))
    humi = 0
    maxi = an[0]
    for i in range(1, n):
        if an[i] < maxi:
            hu = maxi - an[i]
            humi += hu
        maxi = max(maxi, an[i])
    print(humi)


if __name__ == "__main__":
    main()

-n = int(eval(input())) -an = list(map(int, input().split())) -maxi = 0 -humi = 0 -maxi = an[0] -for i in range(1, n): -    if an[i] < maxi: -        hu = maxi - an[i] -        humi += hu -    maxi = max(maxi, an[i]) -print(humi) +import sys + +input = sys.stdin.readline + + +def main(): +    n = int(eval(input())) +    an = list(map(int, input().split())) +    humi = 0 +    maxi = an[0] +    for i in range(1, n): +        if an[i] < maxi: +            hu = maxi - an[i] +            humi += hu +        maxi = max(maxi, an[i]) +    print(humi) + + +if __name__ == "__main__": +    main()