import sys

cin = sys.stdin.readline

n = int(cin())

height = cin().split()

last, ans = 0, 0

for i in height:

    diff = last - int(i)

    if diff > 0:

        ans += diff

        continue

    last = int(i)



print(ans)

import sys

cin = sys.stdin.readline

n = int(cin())

height = list(map(int, cin().split()))

last, ans = 0, 0

for i in height:

    diff = last - i

    if diff > 0:

        ans += diff

        continue

    last = i



print(ans)

import sys

cin = sys.stdin.readline
n = int(cin())
height = cin().split()
last, ans = 0, 0
for i in height:
    diff = last - int(i)
    if diff > 0:
        ans += diff
        continue
    last = int(i)
print(ans)

import sys

cin = sys.stdin.readline
n = int(cin())
height = list(map(int, cin().split()))
last, ans = 0, 0
for i in height:
    diff = last - i
    if diff > 0:
        ans += diff
        continue
    last = i
print(ans)

-height = cin().split() +height = list(map(int, cin().split())) -    diff = last - int(i) +    diff = last - i -    last = int(i) +    last = i