Problem:
 Score : 300 points    Problem Statement N persons are standing in a row. The height of the i -th person from the front is A_i .  We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:  Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.  Find the minimum total height of the stools needed to meet this goal.      Constraints  1 \leq N \leq 2\times 10^5  1 \leq A_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A_1  \ldots  A_N       Output Print the minimum total height of the stools needed to meet the goal.        Sample Input 1 5
2 1 5 4 3      Sample Output 1 4  If the persons stand on stools of heights 0 , 1 , 0 , 1 , and 2 , respectively, their heights will be 2 , 2 , 5 , 5 , and 5 , satisfying the condition.  We cannot meet the goal with a smaller total height of the stools.       Sample Input 2 5
3 3 3 3 3      Sample Output 2 0  Giving a stool of height 0 to everyone will work.

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np

n = int(input())
A = [int(a) for a in input().strip().split()]

pre = A[0]
s = 0
for a in A:
    ad = max(0, pre - a)
    pre = max(pre, a)
    s += ad
print(s)

Solution (Python):
import sys

n = int(input())
a = list(map(int,input().split()))

h =[]

if n == 1:
    print(0)
    sys.exit()

if a[0] > a[1]:
    h.append(a[0] - a[1])
    a[1] = a[0]
else:
    h.append(0)
    
for i in range(2,n):
    if a[i-1] > a[i]:
        h.append(a[i-1]- a[i])
        a[i] = a[i-1]
    else:
        h.append(0)
        
print(sum(h))

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
L = list(map(int, input().split()))
if N == 1:
  print(0)
else:
  x = 0
  for i in range(0,len(L)-1):
    if L[i] > L[i + 1]:
      x = x + (L[i] - L[i + 1])
      L[i + 1] = L[i + 1] + (L[i] - L[i + 1])
  print(x)

Solution (Python):
N = int(input())

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

DEBUG = False

cnt = 0

for i in range(1, N):
    if A[i-1]>A[i]:
        diff = A[i-1]-A[i]
        cnt += diff
        A[i] += diff
        if DEBUG:print(f"A[{i-1}] = {A[i-1]}\t,\tA[{i}] = {A[i]}\t,\tcnt = {cnt}")

print(cnt)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(len(A)-1):
    if A[i] > A[i+1]:
        ans += A[i] - A[i+1]
        A[i+1] = A[i]
    else:
        continue

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(1, n):
  if a[i] < a[i-1]:
    count += a[i-1] - a[i]
    a[i] = a[i-1]
print(count)

Solution (Python):
import numpy as np
N = int(input())
A = np.asarray(input().split()).astype(np.int)

ind_maxA = A.argmax()

diffA = A.max() - A[ind_maxA:]
ind = np.where(diffA > 0)
cost = np.sum(diffA[ind])

maxA = 0
for n in range(1, ind_maxA):
    if A[n-1] > A[n]:
        diff = A[n-1] - A[n]
        cost += diff
        A[n] += diff 

print(cost)

Solution (Python):
n=int(input())
x=input().split()
a=[]
for i in range(n):
	a.append(int(x[i]))
counter=0
for i in range(n-1):
	if a[i+1]-a[i]<0: 
		counter+=-a[i+1]+a[i]
		a[i+1]+=-a[i+1]+a[i]

print(counter)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
fumi_sum = 0
for i in range(1,n):
    if a[i] < a[i-1]:
        fumi_sum += a[i-1] - a[i]
        a[i] += a[i-1] - a[i]
print(fumi_sum)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = [0,]
B = [A[0],]

for i in range(1,N):
    step = -min(0, A[i]-B[i-1])
    S.append(step)
    B.append(A[i] + step)

print(sum(S))

Solution (Python):

from functools import reduce
N = int(input())
An = list(map(int, input().split()))

b = 0
sum = 0

for i in range(N):
  if An[i] > b : 
    b = An[i]
  else:
    sum += b - An[i]

print(sum)

Solution (Python):
N = int(input())
A = input("").split(" ")
A = [int(i) for i in A]
n = 0

for i in range(1, N):
    if A[i-1] > A[i]:
        n += A[i-1] - A[i]
        A[i] = A[i-1]

print(n)

Solution (Python):
N = int(input())
arr = list(map(int, input().split()))
length = 0
for i in range(N-1):
    if arr[i+1] < arr[i]:
        length +=  arr[i] - arr[i+1]
        arr[i+1] += arr[i] - arr[i+1]
print(length)

Solution (Python):
N = int(input())
A = input()
A = A.split()
a = []
for i in A:
    a.append(int(i))

c = []
for i in range(N-1):
    if(a[i]<=a[i+1]):
        c.append(0)
    if(a[i]>a[i+1]):
        diff = a[i] - a[i+1]
        a[i+1] += diff
        c.append(diff)

result = sum(c)
print(result)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
height = 0
sum = 0
for i in range(N):
    a = height - A[i]
    if  a > 0:
        sum += a
        height = A[i] + a
    else:
        height = A[i]
print(sum)

Solution (Python):


from typing import List


def f(n: int, a_list: List[int]) -> int:
    """
    >>> f(5, [2, 1, 5, 4, 3])
    4
    """
    ans = 0

    n_max: int = a_list[0]
    for i, a_i in enumerate(a_list):
        if n_max > a_i:
            ans += n_max - a_i
        else:
            n_max = a_i

    return ans


if __name__ == '__main__':
    n = int(input())
    a_list = list(map(lambda x: int(x), input().split()))
    print(f(n, a_list))

Solution (Python):
n=int(input())
l=list(map(int, input().split()))
a=0
for i in range(n-1):
  if l[i]>l[i+1]:
    a=a+l[i]-l[i+1]
    l[i+1]=l[i]
print(a)

Solution (Python):

import math

num = int(input())
total = 0
max = 0
tall = list(map(int, input().split()))
for i in range(num):
    if max <= tall[i]:
        max = tall[i]
    if max > tall[i]:
        total += max -tall[i]

print(total)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
count = 0
geta = 0
for i in range(n-1):
    if a[i] + geta > a[i+1]:
        count += a[i] + geta -a[i+1]
        geta = a[i] + geta -a[i+1]
    else:
        geta = 0

print(count)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = [0]
for i in range(1, N):
    if A[i - 1] - A[i] > 0:
        B.append(A[i - 1] - A[i])
        A[i] += A[i - 1] - A[i]
print(sum(B))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sum = 0

for i in range(N-1):
    if A[i] - A[i+1] > 0:
        sum += A[i] - A[i+1]
        A[i+1] = A[i]

print(sum)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
m=a[0]
sum=0

for i in range(len(a)):
  m=max(m,a[i])
  sum += max(0,m-a[i])

print(sum)

Solution (Python):
N=int(input())
*A,=map(int,input().split())

Step=[0]
maxt=A[0]
i=0
while i<N-1:
  if maxt<A[i+1]:
    maxt=max(maxt,A[i+1])
    s=0
  else: 
    s=maxt-A[i+1]
  Step+=[s]
  i+=1
print(sum(Step))

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
s = 0
for i in range(1, n):
  if h[i] <= h[i-1]:
    s += h[i-1]-h[i]
    h[i] += h[i-1]-h[i]
print(s)

Solution (Python):
s = int(input())
arr = list(map(int, input().split()))

ans = 0

for i in range(1,s):
    if arr[i] < arr[i-1]:
        
        ans += (arr[i-1]-arr[i])
        arr[i] = arr[i-1]

print(ans)

Solution (Python):
n=int(input())
lst=list(map(int,input().strip().split()))
lst1=list(set(lst))
ans=0 
for i in range(1,n):
    if(lst[i-1]>lst[i]):
        a=(lst[i-1]-lst[i])
        lst[i]+=a 
        ans+=a
print(ans)

Solution (Python):
n = int(input())
l_a = list(map(int, input().split()))
high_max = 0
sum_hight = 0
for i in range(n):
    if high_max <= l_a[i]:
        high_max = l_a[i]
    else:
        sum_hight += high_max - l_a[i]

print(sum_hight)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
s = [0] * n

for i in range(1, n):
  if ((h[i-1] + s[i-1]) > h[i]):
    s[i] = (h[i-1] + s[i-1]) - h[i]

print(sum(s))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
import copy
b = copy.copy(a)
tall = a[0]
for i in range(n):
    if a[i]<tall:
        a[i] = tall
    elif a[i]>tall:
        tall = a[i]
print(sum(a)-sum(b))

Solution (Python):
n = int(input())
hight_list = list(map(int, input().split()))
sum_step = 0
max_hight = 0

for i in range(n):
    if hight_list[i] < max_hight:
        
        sum_step += max_hight - hight_list[i] 
    else:
        max_hight = hight_list[i]

print(sum_step)

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))
tmp = 0
sum = 0
for a in a_list:
  if tmp < a:
    tmp = a
  else:
    sum += (tmp-a)

print (sum)

Solution (Python):
n = int(input())
num = [int(i) for i in input().split()]
limit = num[0]
sum = 0
for i in range(n):
    if(limit > num[i]):
        sum += limit - num[i]
    else:
        limit = num[i]
print(sum)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))

ans=0
for i in range(n-1) :
    if a[i]<=a[i+1] :
        continue
    else :
        ans+=a[i]-a[i+1]
        a[i+1]=a[i]
        
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split(" ")))
count=0
m=0
for i in A:
    if i>=m:
        m=i
    else:
        count+=m-i
print(count)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = [0]
for i in range(1, N):
    if A[i - 1] - A[i] > 0:
        B.append(A[i - 1] - A[i])
        A[i] += A[i - 1] - A[i]
print(sum(B))

Solution (Python):







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


max=0
ans=0
for n in A:
	if max<n : max=n
	
	if max>n : ans=ans + max-n




print(ans)

Solution (Python):
N = int(input())
num = [int(i) for i in input().split()]
count = 0
for i in range(N):
    if i == 0:
        continue
    else:
        if num[i] < num[i-1]:
            count += num[i-1] - num[i]
            num[i] = num[i-1]
print(int(count))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.insert(0,0)
a_ = a.copy()
s = 0
for i in range(1,n):
    if a_[i+1] < a_[i]:
        a_[i+1] = a_[i]
ans = [a_[i] - a[i] for i in range(len(a))]
print(sum(ans))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
max = A[0]
min = 0
sum = 0
for i in range(N-1):
    if(A[i+1]>max):
        max = A[i+1]
    else:
        sum += max - A[i+1]
        
        

print(sum)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = [0]
for i in range(1, N):
    if A[i - 1] - A[i] > 0:
        B.append(A[i - 1] - A[i])
        A[i] += A[i - 1] - A[i]
print(sum(B))

Solution (Python):



















"""fact=[1]












from sys import stdin, stdout
import bisect            
from bisect import bisect_left as bl              
from bisect import bisect_right as br             
import itertools
from collections import Counter
from math import sqrt
import collections
import math
import heapq
import re
def modinv(n,p):
    return pow(n,p-2,p)
def cin():
    return map(int,sin().split())
def ain():                           
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
  
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l

def most_frequent(list):
    return max(set(list), key = list.count) 
def GCD(x,y):
    while(y):
        x, y = y, x % y
    return x

def ncr(n,r,p):                                              
    t=((fact[n])*((ifact[r]*ifact[n-r])%p))%p
    return t

def Convert(string): 
    li = list(string.split("")) 
    return li 

def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
prime=[]
q=[]
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            
    return j









"""*******************************************************"""
def main():
    n=inin()
    a=ain()
    arr=[0]*n
    for i in range(n-1):
        if a[i+1]<a[i]:
            arr[i+1]=abs(a[i]-a[i+1])
            a[i+1]=a[i]
    print(sum(arr))
    
    
"""*******************************************************"""

py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange
 
import os, sys
from io import IOBase, BytesIO
 
BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None
 
    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
 
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()
 
    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s:self.buffer.write(s.encode('ascii'))
            self.read = lambda:self.buffer.read().decode('ascii')
            self.readline = lambda:self.buffer.readline().decode('ascii')
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
if __name__== "__main__":
    main()

Solution (Python):
n = int(input())
l = input()
a = l.split(' ')

count = 0

for i in range(len(a)-1):
    if(int(a[i]) > int(a[i+1])):
        count += int(a[i]) - int(a[i+1])
        a[i+1]=a[i]

print(count)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
d = 0
for n in range(0, N-1):
    if A[n+1] < A[n]:
        d += A[n]-A[n+1]
        A[n+1] = A[n]
print(d)

Solution (Python):
N=int(input())

A=[int(i) for i in input().split()]
maxA=A[0]
sumA=0
for i in range(1,N):
    if A[i]<maxA:
        sumA+=maxA-A[i]
    else:
        maxA=A[i]
print(sumA)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

result = 0
for i in range(1, n):
    hoge = a[i]
    a[i] = a[i]+(a[i-1]-a[i]) if a[i-1] > a[i] else a[i]
    result += a[i] - hoge
    
print(result)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
m=0
result=0
for i in range(N):
    m=max(A[i],m)
    if A[i]<m:
        result+=m-A[i]
print(result)

Solution (Python):
import copy
a=int(input())
b=list(map(int,input().split()))
c=copy.deepcopy(b)
for i in range(a-1):
    if b[i]>b[i+1]:
        b[i+1]=b[i]
ans=0
for i in range(a):
    ans+=b[i]-c[i]
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
  if A[i] > A[i+1]:
    cnt += A[i] - A[i+1]
    A[i+1] += A[i] - A[i+1]
    
print(cnt)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))

step = 0

for i in range(n):
    if i == 0:
        continue
    if h[i] >= h[i-1]:
        continue

    step += (h[i-1] - h[i])
    h[i] = h[i-1]

print(step)

Solution (Python):
from sys import stdin
input = stdin.readline

n = input().rstrip()
a = list(map(int, input().split()))

ans = 0
for i in range(len(a)-1):
    if a[i] > a[i+1]:
        diff = a[i] - a[i+1]
        a[i+1] += diff
        ans += diff

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = [0]
for i in range(1, N):
    if A[i - 1] - A[i] > 0:
        B.append(A[i - 1] - A[i])
        A[i] += A[i - 1] - A[i]
print(sum(B))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = 0
K = 0

for i in range(N):
    if A[i] > K:
        K = A[i]
    else:
        S += (K - A[i])
print(S)

Solution (Python):
n = int(input())
height_list = list(map(int, input().split()))

need_height = height_list[0]
total_need_step = 0

for i in range(1, len(height_list)):
    if height_list[i] < need_height:
        degree = need_height - height_list[i]
        total_need_step += degree
    else:
        need_height = height_list[i]

print(total_need_step)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

count=0
for i in range(N):
    if N-i == 1:
        break
    if A[i] > A[i+1]:
        count += A[i]-A[i+1]
        A[i+1] = A[i]

print(count)

Solution (Python):
n = int(input())
a = [int(s) for s in input().split()]

cnt = 0
highest = 0
for x in a:
    if highest > x:
        cnt += (highest - x)
    else:
        highest = x
print(cnt)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans=0
for i in range(N-1):
    if A[i] > A[i+1]:
        ans+=A[i] - A[i+1]
        A[i+1] += A[i] - A[i+1]
print(ans)

Solution (Python):


"""
https://atcoder.jp/contests/abc176/tasks/abc176_c
Step
"""


def solve(a, n):
    ans = 0

    for i in range(1, n):
        if a[i] < a[i-1]:
            ans += a[i-1] - a[i]
            a[i] = a[i-1]

    return ans

if __name__ == "__main__":
    n = int(input())
    a = [(lambda x: int(x))(x) for x in input().split()]

    print(solve(a, n))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
dai=[0]*n
x=a[0]
for i in range(n-1):
    if a[i+1]-x<0:
        dai[i+1]=x-a[i+1]
        x=a[i+1]+dai[i+1]
    else:
        x=a[i+1]
print(sum(dai))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

max_num = 0
step = []
for i in range(n):
    if a[i] >= max_num:
        max_num = a[i]
    else:
        step.append(max_num - a[i])
print(sum(step))

Solution (Python):
import sys
readline = sys.stdin.readline

N = int(readline().strip())
A = list(map(int, readline().split()))

d = 0
for i in range(1, N):
  if A[i] < A[i-1]:
    d += A[i-1] - A[i]
    A[i] = A[i-1]

print(d)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
sum1=0
mini=a[0]
for i in range(len(a)-1):
    s=a[i+1]
    if mini<s:
        mini=s
        continue
    elif mini>=s:
        sum1+=abs(mini-s)

print(sum1)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
highest = A[0]
ans = 0
for i in range(1, N):
  highest = max(highest, A[i])
  ans += max(0, highest - A[i])
print(ans)

Solution (Python):

"""
N : 人数
A1.....An : n番目の人の身長
自分より前に自分より背の高い人がいないようにするための踏み台の最小値
"""

N = int(input())
A_list = [int(x) for x in input().split()]

before_p = A_list[0]
ans = 0

for i in A_list:
  comp = 0
  if i == before_p:
    pass
  elif i >= before_p:
    pass
  elif i < before_p:
    comp = before_p - i
    i += comp
  before_p = i
  ans += comp

print(ans)

Solution (Python):
N = int(input())
A = [i for i in map(int, input().split())]

height = []
for i in range(N-1):
    if A[i] >= A[i+1]:
        height.append(A[i] - A[i+1])
        A[i+1] = A[i]
    else:
        pass

print(sum(height))

Solution (Python):
N = input()
A = list(map(int, input().split()))
B = [0]

siz = len(A)
for i in range(siz-1):
    if A[i] <= A[i+1]:
        B.append(0)
    elif A[i] > A[i+1]:
        B.append(A[i] - A[i+1])
        A[i+1] = A[i+1] + B[i+1]
    else:
        pass
ans = 0
for i in range(siz):
    ans += B[i]
print("{0}".format(ans))

Solution (Python):
N = int(input())
A = (list(map(int,input().split())))

t = 0

for i in range(N-1):
     if A[i] >= A[i+1]:
      t += A[i] - A[i+1]
      A[i+1] = A[i]

print(t)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
X=0
for i in range(N-1):
  if A[i+1]<A[i]:
    X+=A[i]-A[i+1]
    A[i+1]=A[i]
print(X)

Solution (Python):
n=int(input())
a=input().split()
for i in range(n):
  a[i]=int(a[i])
r=0
for k in range(n-1):
  if a[k]>a[k+1]:
    w=a[k]-a[k+1]
    a[k+1]=a[k]
    r=r+w
print(r)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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

Solution (Python):
n = input()
heights = map(int, input().split())

max_height = 0
step_sum = 0

for i in heights:
    if max_height < i:
        max_height = i
    if max_height > i:
        step_sum += max_height - i
print(step_sum)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
cnt=0
for i in range(n-1):
  if A[i+1] < A[i]:
    s = A[i]-A[i+1]
    A[i+1] += s
    cnt += s

print(cnt)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

pre_max = 0
ans = 0
for An in A:
    if pre_max <= An:
        pre_max = An
    else:
        ans += pre_max - An

print(ans)

Solution (Python):
from typing import List
import sys


def solution(inp: List) -> int:
    inp.pop(0)
    ans: int = 0
    recent: int = 0
    for n in inp:
        if recent > int(n):
            ans += recent - int(n)
            continue
        recent = int(n)
    return ans


inp: List = []
for j in sys.stdin:
    inp += j.split()
print(solution(inp))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))

cnt, tmp = 0, A[0]
for a in A[1:]:
    if tmp > a:
        cnt += (tmp-a)
    else:
        tmp = a
print(cnt)

Solution (Python):
N = int(input())

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

max = A[0]
sum = 0
for i in range(N):
    if A[i] < max:
        sum += max - A[i]
    else:
        max = A[i]
        
print(sum)

Solution (Python):
n = int(input())
answer = 0
score = list(map(int,input().split()))
for i in range(len(score)-1):
    if score[i] > score[i+1]:
        answer += score[i]-score[i+1]
        score[i+1] = score[i]
print(answer)

Solution (Python):
N = int(input())

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

prev = 0
ans = 0
for i in range(N):
  if i != 0:
    if prev > A[i]:
      ans += prev-A[i]
      A[i] += prev-A[i]
  prev = A[i]
print(ans)

Solution (Python):
N = int(input())
ls = [int(s) for s in input().split(" ")]

fwd_tall = ls[0]
sum_e = 0
for i in range(N):
    tall = ls[i]
    e = 0
    if fwd_tall > tall:
        e = fwd_tall - tall
        ls[i] += e
    sum_e += e
    
    fwd_tall = ls[i]
print(sum_e)

Solution (Python):
n=int(input())
l=[int(x) for x in input().split()]
maxi=l[0]
sum1=0
for i in range(1,len(l)):
    if maxi>l[i]:
        sum1=sum1+(maxi-l[i])
    maxi=max(maxi,l[i])
print(sum1)

Solution (Python):


input()
a = list(map(int, input().split()))
r=0
l=a[0]
for i in range(1,len(a)):
  if l > a[i]:
    r=r+(l-a[i])
  else:
    l=a[i]
print(r)

Solution (Python):
import numpy as np
import sys
import math

N = int(input())

A = input().split()
A = np.array(A, dtype=np.int64)

front = 0
base = 0
basesum = 0
for i in range(N):
    if front > A[i]:
        base = front - A[i]
        basesum = basesum + base
        front = A[i] + base
    else:
        base = 0
        front = A[i]

print(basesum)

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
ans = 0

mx = a[0]
for i in a:
	if i < mx:
		ans += mx - i
	else:
		mx = i

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

A_tobi=[0]*N
A_max=A[0]
for i in range(len(A)):
    x_sum=A[i]
    if A[i]<A_max:
        x_sum=A_max
        A_max=x_sum
        A_tobi[i]=(x_sum-A[i])
    else :
        A_max=A[i]

print(sum(A_tobi))

Solution (Python):
n = int(input())
A = [int(x) for x in input().split()]

sum = 0

for i in range(1, n):
    stoolHeight = max(0, (A[i-1] - A[i]))
    A[i] += stoolHeight
    sum += stoolHeight

print(sum)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

count = 0

for i, j in enumerate(a):
    try:
        if j > a[i + 1]:
            b = j - a[i + 1]
            count += b
            a[i + 1] = j
        else:
            pass
    except IndexError:
        pass

print(count)

Solution (Python):
n = int(input())
heights = [int(i) for i in input().split()]

total = 0
for i in range(1, n):
    if heights[i] < heights[i - 1]:
        diff = heights[i - 1] - heights[i]
        heights[i] += diff
        total += diff

print(total)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))
fumidai_list = [0] * N

for i in range(1, N):
    tmp = A_list[i] - (A_list[i-1] + fumidai_list[i-1])
    if tmp < 1:
        fumidai_list[i] = -tmp

print(sum(fumidai_list))

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
sys.setrecursionlimit(10**7)

def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    N = I()
    A = LI()
    a_max = A[0]
    ans = 0
    for i in range(1,len(A)):
        if A[i]<a_max:
            ans += a_max-A[i]
        else:
            a_max = A[i]
    print(ans)

main()

Solution (Python):
N = int(input())
A_s = list(map(int, input().split()))
answer = 0
for i, A in enumerate(A_s):
    if i == 0:
        continue
    if A_s[i-1] > A_s[i]:
        answer += A_s[i-1] - A_s[i]
        A_s[i] = A_s[i-1]
print(answer)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
h=0
for i in range(1,N):
    a=A[i]-A[i-1]
    if a<0:
        h-=a
        A[i]-=a
print(h)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
c = A[0]
S = 0
count = 0
for i in range(len(A)):
  if A[i] < c :
    count = count + c
    S = S + A[i]
  else:
    count = count + A[i]
    c = A[i]
    S = S + A[i]

print(count - S)

Solution (Python):
N=int(input())
ai=list(map(int,input().split()))
ans:int=0
for i in range(N-1):
    fumi=ai[i]-ai[i+1]
    if fumi>=1:
        ai[i+1]+=fumi
        ans+=fumi
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
m = A[0]
ans = 0
for a in A[1:]:
    if a < m:
        ans += m - a
    else:
        m = a
print(ans)

Solution (Python):
N = input()
A = input().split()

counter = 0
result = 0

for i in A:
    if counter > int(i):
        add = counter - int(i)
        result = result + add
    else:
        counter = int(i)

print(result)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ladder=[0]*N
for i in range(N-1):
  diff=A[i]-A[i+1]
  if diff>0:
    ladder[i+1]=diff
    A[i+1]+=diff

print(sum(ladder))

Solution (Python):
import sys
from sys import stdin,stdout
import math
import time
from functools import lru_cache
from collections import Counter
import heapq

try:
    for _ in range(1):
        n=int(input())
        arr=[int(i) for i in input().split()]
        ans=0
        mx=arr[0]
        for i in range(n):
            if arr[i]<mx:
                ans+=mx-arr[i]
                mx=max(arr[i],mx)
            else:
                mx=arr[i]
        print(ans)
except EOFError as e:
    print(e)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mae=a[0]
sum=0
for i in range(len(a)-1):
  if a[i+1]<mae:
    sum=sum+(mae-a[i+1])
  else:
    mae=a[i+1]
print(sum)

Solution (Python):
import sys
from io import StringIO
import unittest

def resolve():
    N, = map(int, input().split())
    As = list(map(int, input().split()))
    ans = 0
    last = 0
    for a in As:
        dif = max(0,last-a)
        last = a+dif
        ans = ans+dif


    print("{}".format(ans))

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
2 1 5 4 3"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5
3 3 3 3 3"""
        output = """0"""
        self.assertIO(input, output)


if __name__ == "__main__":
    resolve()

Solution (Python):




n = int(input())


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



result = 0
before_a = 0
for a in a_list:
  
  
  diff = before_a - a
  
  if diff < 0:
    pass
    
  else:
    result += diff
    a = a + diff

  before_a = a

print(result)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))

tot=0
for i in range(N-1):
  if A[i]>A[i+1]:
    table=A[i]-A[i+1]
    A[i+1]+=table
    tot+=table
  else:
    pass

print(tot)

Solution (Python):
N = input()
A = input().split()

summation = 0
M = int(A[0])
for i in range(int(N)):
    if int(A[i]) < M:
        summation += M - int(A[i])
    else: M = int(A[i])

print(summation)

Solution (Python):
N = int(input())
L = list(map(int, input().split()))

count = 0
for i in range(N-1):
    if L[i] > L[i + 1]:
        difference = L[i] - L[i + 1]
        L[i+1] += difference
        count += difference

print(count)

Solution (Python):
N = int(input())
As = list(map(int, input().split()))

steps = 0
human_mae = -1
for a in As:
    if human_mae == -1:
        human_mae = a
        continue
    if a < human_mae:
        step = human_mae - a
        steps += step
        human_mae = a + step
        continue
    human_mae = a

print(steps)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
Max = 0
cnt = 0
for i in range(n):
    if Max < a[i]:
        Max = a[i]
    else:
        cnt += (Max - a[i])
print(cnt)

Solution (Python):
N=int(input())
Alist=list(map(int,input().split()))
sumheight=0
for n in range(N-1):
    if Alist[n]>Alist[n+1]:
        sumheight+=Alist[n]-Alist[n+1]
        Alist[n+1]=Alist[n]
print(sumheight)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ap = 0
xp = 0
xcount = 0

for i in range(N):
    a = A[i]
    x = (ap+xp)-a
    if x <= 0:
        xp = 0
    else:
        xp = x
    xcount = xcount + xp
    ap = a
print(xcount)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sum = 0

for i in range(N-1):
    if A[i] > A [i+1]:
        sum += A[i] - A[i+1]
        A[i+1] = A[i]
        
print(sum)

Solution (Python):
n=int(input())
li=list(map(int,input().split()))
ans=0
for i in range(1,n):
  prev=li[i-1]
  if li[i]<prev:
    ans+=prev-li[i]
    li[i]=prev
print(ans)

Solution (Python):


import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union































def main():
    Num = Union[int, float]
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input():  return sys.stdin.readline().rstrip()
    def ii():     return int(input())
    def isp():    return input().split()
    def mi():     return map(int, input().split())
    def mi_0():   return map(lambda x: int(x)-1, input().split())
    def lmi():    return list(map(int, input().split()))
    def lmi_0():  return list(map(lambda x: int(x)-1, input().split()))
    def li():     return list(input())
    def debug(x): print(x, file=sys.stderr)
    
    
    
    n = ii()
    h = lmi()
    step = 0
    for i in range(1, n):
        diff = h[i-1] - h[i]
        if diff > 0:
            
            step += diff
            h[i] += diff
    print(step)


if __name__ == "__main__":
    main()