n = int(input())
#a, b = map(int,input().split())
a = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
ans=0
for i in range(1,n):
    if a[i-1]<=a[i]:
        pass
    else:
        dif=a[i-1]-a[i]
        ans+=dif
        a[i]=a[i]+dif

print(ans)


ee727080c39bfb2526eec5c75ac88e6d7ba7b9b0
<mask token>

<mask token>
for i in range(1, n):
    if a[i - 1] <= a[i]:
        pass
    else:
        dif = a[i - 1] - a[i]
        ans += dif
        a[i] = a[i] + dif
print(ans)

n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(1, n):
    if a[i - 1] <= a[i]:
        pass
    else:
        dif = a[i - 1] - a[i]
        ans += dif
        a[i] = a[i] + dif
print(ans)

n = int(input())
#a, b = map(int,input().split())
a = list(map(int,input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
ans=0
for i in range(1,n):
    if a[i-1]<=a[i]:
        pass
    else:
        dif=a[i-1]-a[i]
        ans+=dif
        a[i]=a[i]+dif

print(ans)

