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):
N =int(input())
A_n =list(map(int,input().split()))

maxh = A_n[0]
ans =0
for i  in range(1,N):
    if A_n[i] < maxh:
        sa = maxh - A_n[i]
        ans += sa
        maxh = A_n[i] +sa
    else:
        maxh = A_n[i]


print(ans)

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

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

for i in range(0,len(A)-1):
    if(A[i]>A[i+1]):
        s = A[i]-A[i+1]
        ans += s
        A[i+1]=A[i]
    
print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
humidais = [0]
for i in range(N-1):
    humidais.append(max(A[i]+humidais[i]-A[i+1], 0))

print(sum(humidais))

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

prev = 0
ans = 0
for i in range(0, N):
    cur = A[i]
    if cur >= prev:
        prev = cur
        continue

    ans += prev - cur

print(ans)

Solution (Python):
__DEUBG__ = 0

import math
N = int(input())
high = list(map(int, input().split()))
stack = []

for idx, h in enumerate(high):
    if idx == 0:
        stack.append((idx, h))
    elif h > stack[-1][-1]:
        stack.append((idx, h))

if __DEUBG__:
    print(N, stack)
    

prev = 3
prev = N
total = 0
for idx, h in stack[::-1]:
    if __DEUBG__:
        print(prev, idx, h)
        print((prev - idx) * h)
    total += (prev - idx) * h
    prev = idx

if __DEUBG__:
    print(total, sum(high))
print(total - sum(high))

Solution (Python):

num = int(input())
strnum_line = input().split(" ")

num_line = [int(n) for n in strnum_line]

fumidai = 0 

for i in range(1,len(num_line)):
    if(num_line[i] < num_line[i-1]):
        
        a = num_line[i-1] - num_line[i]
        
        fumidai += a
        num_line[i] = num_line[i-1]
        
        
print(fumidai)

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

count = 0
s = {}

for i in range(N - 1):
    d = (A[i] + s.get(i, 0)) - A[i + 1]
    if d > 0:
        count += d
        s[i+1] = d
print(count)

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

Solution (Python):

N=int(input())
A_arr=list(map(int,input().split()))
ans=0
h=0
for i in A_arr:
    if i<h:
        ans += h-i
    else:
        h=i
print(ans)

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

Solution (Python):
N = int(input())
high = list(map(int, input().split()))
dai = []
est = high[0]
for i in range(N):
  if est > high[i]:
    
    
    dai.append(est-high[i])
  elif est <= high[i]:
    est = high[i]
print(sum(dai))

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))
t = 0
for i in range(1,n):
    t+=max(arr[i-1]-arr[i],0)
    arr[i] = max(arr[i-1],arr[i])
print(t)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
    if i == 0:
        M = A[0]
    else:
        if A[i] > M:
            M = A[i]
        else:
            ans = ans + M - A[i]
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())
li = list(map(int,input().split()))
sum = 0
max = 0
for a_i in li:
    
    if max <= a_i:  
        max = a_i
    else:  
        sum += max - a_i
print(sum)

Solution (Python):
N = int(input())
an = list(map(int, input().split()))
now = 0
front = 0
step = 0
for ai in an:
    now = ai
    if now < front:
        step += (front - now)
    else:
        front = now
print(step)

Solution (Python):
if __name__ == '__main__':

    n = int(input())

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

    top = A[0]
    ans = 0
    for i in range(n-1):
        if top > A[i+1]:
            ans += top - A[i+1]
        else:
            top = A[i+1]
    print(ans)

Solution (Python):
input()
r = m = 0
for x in map(int, input().split()):
  r += m - min(m, x)
  m = max(m, x)
print(r)

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

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


max = a[0]
wa = 0

for i in range(ninzu) : 
  if a[i] > max:
    max = a[i]
  else:
    wa += max - a[i]

print(wa)

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

count = 0
for i in range(n-1):
    if lis[i] > lis[i+1]:
        dif = lis[i] - lis[i+1]
        count += dif
        lis[i+1] += dif
print(count)

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

Solution (Python):
N = int(input())
li = list(map(int,input().split()))
li_sum = sum(li)

for i in range(len(li)):
    if i == 0:
        continue
    j = i - 1
    if li[i] < li[j]:
        li[i] += li[j] - li[i]

print(sum(li) - li_sum)

Solution (Python):
n = int(input())
A = tuple(map(int, input().split()))
ans = 0
m = A[0]
for a in A[1:]:
  ans += max(m-a, 0)
  m = max(a, m)
print(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 = t + A[i] - A[i+1]
    A[i+1] = A[i]
print(t)

Solution (Python):


n=int(input())
arr=[int(x) for x in input().split()]
maxa=arr[0]
count=0
for i in range(n):
    if arr[i] >= maxa:
        maxa=arr[i]
    else:
        count+=(maxa-arr[i])
print(count)

Solution (Python):
import sys
def input():return sys.stdin.readline()[:-1]
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())

n=N()
l=L()
ans=0
for i in range(n-1):
    if l[i]>l[i+1]:
        ans+=l[i]-l[i+1]
        l[i+1]=l[i]
print(ans)

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

tallest = 0
ans =0
for i in range(N):
    if tallest >A[i]:
        ans +=tallest -A[i]
    else:
        tallest =A[i]

print(ans)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
W = [0]*(N+1)
p = 0
for i in range(1,N):
    if A[i-1] > A[i]:
        p = A[i-1]-A[i]
        A[i] = A[i] + p
        W[i] = p
print(sum(W))

Solution (Python):
n=int(input())
a=[int(x) for x in input().split()]
l=0
s=0
for c in range(n):
    if l>a[c]:
        s+=l-a[c]
    else:
        l=a[c]
print(s)

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

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

pre_num, sum_stand = 0, 0

for i in A:
    if pre_num > i:
        sum_stand += pre_num - i
    else:
        pre_num = i

print(sum_stand)

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

sum = 0
for n in range(1, N):
    if A[n] < A[n-1]:
        sum += A[n-1] - A[n]
        A[n] = A[n-1]
print(sum)

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

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

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

flg = True
before = 0
for a in A:
    if flg:
        flg = False
        B.append(0)
        before = a
    else:
        add = max(before - a, 0)
        B.append(add)
        before = a + add

print(sum(B))

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

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

ans = 0
for i in range(len(A)):
    if i == len(A)-1:
        break
    else:
        front = A[i]
        behind = A[i+1]
        if front <= behind:
            pass
        else:
            ans += (front - behind)
            A[i+1] = front

print(ans)

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

B = []

for i in range(len(A)):
    if i == 0:
        B.append(A[i])
    else:
        if A[i] < B[i-1]:
            B.append(B[i-1])
        else:
            B.append(A[i])

diff = map(lambda a,b: a - b, B,A)
print(sum(list(diff)))

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

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

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

th = 0
ph = 0
for a in A:
    if ph > a:
        th += ph - a
    else:
        ph = a
print(th)

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

step = 0

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

print(step)

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

Solution (Python):

import sys
from itertools import product,combinations
from functools import reduce,lru_cache
import heapq

def vfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:f(*x,**kwargs),*lst))

def list_t(l): return vfunc(lambda *args:args)(*l)

def rvfunc(f): return lambda *lst, **kwargs: list(map(lambda *x:rvfunc(f)(*x,**kwargs) if all(vfunc(lambda y:isinstance(y,list))(x)) else f(*x,**kwargs),*lst))

def reader():return vfunc(lambda l:l.split())(sys.stdin.readlines())

n,a = rvfunc(int)(reader())
tmax=0
f=0
for i in range(n[0]):
    ai=a[i]
    if a[i]>tmax:tmax=a[i]
    f+=tmax-a[i]
print(f)

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

count = 0
height = 0

for i in range(1,n):
    height = a[i-1]-a[i]
    if height >= 0:
        count += height
        a[i] += height

print(count)

Solution (Python):
N = input()
tab = []
data = input().split()
res = 0
last = -1
for i in data:
    val = int(i)
    if (val < last):
        res += (last - val)
    else:
        last = val
print(res)

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

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

    s = 0

    i = 1
    while i < n:
        while True:
            if (a[i-1] <= a[i]) :
                i+=1
                if i == n:
                    break
                continue
            else:
                s += a[i-1] - a[i]
                a[i]  = a[i-1]
                break
        i += 1
        
    print(s)
        
if __name__=='__main__':
    main()

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):
n=int(input())
a=list(map(int,input().split()))
step=0
for i in range(n-1):
  if a[i]>a[i+1]:
    step+=a[i]-a[i+1]
    a[i+1]=a[i]
print(step)

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

max_height = A[0]
step = [0] * N
ans = 0

for a in A:
    if a < max_height:
        ans += max_height - a
    else:
        max_height = a
print(ans)

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

res = 0

for i in range(len(A)-1):
  if A[i] > A[i+1]:
    res += abs(A[i] - A[i+1])
    A[i+1] = A[i+1] + abs(A[i] - A[i+1])
    
print(res)

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

total = 0
for i in range(1, n):
    if p[i-1] > p[i]:
        diff = p[i-1] - p[i]
        total += diff
        p[i] = p[i-1]
        
print(total)

Solution (Python):
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    global a
    ans = 0
    i = 1
    piv = a[0]
    while i<n:
        while a[i]<piv:
            ans += piv-a[i]
            i += 1
            if n<=i:
                break
        if i<n:
            piv = a[i]
            i += 1
    print(ans)
    return

if __name__=='__main__':
    n = I()
    a = list(IL())
    solve()

Solution (Python):

N = int(input())
A = list(map(int,input().split()))
max = A[0]
F = [0]

for i in range(1,N):
  if max > A[i]:
    F.append(max - A[i])
  else:
    max = A[i]
    F.append(0)

print(sum(F))

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

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]:
        b = a[i] - a[i+1]
        a[i+1] += b
        sum += b
print(sum)

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

ans=0

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

print(ans)

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


prev=A[0]
dai=0
count=0

for i in range(0,len(A)):

    
    if prev>A[i]:
        
        
        dai=prev-A[i]
        count=count+dai
        A[i]=A[i]+dai
    prev=A[i]


print(count)

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


max_a = 0
result = 0
for i in A_list:
    
    if i >= max_a:
        max_a = i
    
    else: 
        diff = max_a - i
        result += diff

print(result)

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


ans = 0
naw = A[0]

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

print(ans)

Solution (Python):
n = input()
a_list = list(map(int, input().split())) 
cnt = 0
tmp = 0

for a in range(len(a_list)-1):
    if (a_list[a] > a_list[a+1]):
        cnt += a_list[a] -a_list[a+1]
        a_list[a+1] += a_list[a] -a_list[a+1]
        

print(cnt)

Solution (Python):
x=int(input())
l=list(map(int,input().split(" ")))
sum=0
mx=-1000
for i in range(len(l)-1):
    mx=max(mx,l[i])
    if l[i+1]<mx:
        sum+=mx-l[i+1]

print(sum)

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

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

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

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
ans = 0

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


print(ans)

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

print(ans)

Solution (Python):
n = int(input())
height = input().split()

suma = 0
highest = int(height[0])
for i in range(1,n):
  if int(height[i]) > highest:
    highest = int(height[i])
  if int(height[i]) < highest:
    suma += highest-int(height[i])
print(suma)

Solution (Python):
import math
N = int(input())
A = [int(x) for x in input().split()]
B = A.copy()
max =-1
for i in range(len(A)):
    if(A[i] > max):
        max = A[i]
        B[i] = A[i]
    else:
        B[i] = max

C = [B[i] - A[i] for i in range(len(A))]

print(sum(C))

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
maxNum = l[0]
total = 0

for i in range(1,len(l)):
    if maxNum <= l[i]:
        maxNum = l[i]
    else:
        total += maxNum - l[i]
print(total)

Solution (Python):

n = int(input())
tolls = [int(i) for i in input().split()]


max_n = 0
height_s = 0


for i in range(len(tolls)):
    if max_n < tolls[i]:
        max_n = tolls[i]
    height_s += max_n - tolls[i]


print(height_s)

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

cur_max = ls_a[0]
sum = 0
for i in range(1, n):
  cur_h = ls_a[i]
  diff = cur_max - cur_h
  if diff > 0:
    sum += diff
  elif diff < 0:
    cur_max = cur_h

print(sum)

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

for i in A:
    if i >= max:
        max = i

    else:
        sum += max-i

print(sum)

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

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

print(ans)

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())
A=list(map(int,input().split()))
h=A[0]
x=0
for i in range(n):
  if h>A[i]:
    x+=h-A[i]
  else:
    h=A[i]
print(x)

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

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

res = 0
m = -1
for i in A:
  if(m>i):
    res += m-i
  m = max(m,i)
print(res)

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

first_flg = True
before_tall = 0
add_dai = 0
for now_tall in a_list:
    if first_flg:
        before_tall = now_tall
        first_flg = False
    else:
        if before_tall > now_tall:
            add_dai += before_tall - now_tall
            before_tall = now_tall + (before_tall - now_tall)
        else:
            before_tall = now_tall
print(add_dai)

Solution (Python):
n=int(input())
m=0
ans=0
a=[int(x) for x in input().split()]
for i in range(len(a)):
    if a[i]>m:
        m=a[i]
    elif m>=a[i]:
        ans+=m-a[i]

print(ans)

Solution (Python):
def main():
    N = int(input())
    AList = list(map(int, input().split()))

    answer = 0

    for i in range(1, N):
        diff = AList[i-1] - AList[i]

        if(diff > 0):
            answer = answer + diff
            AList[i] = AList[i - 1]

    print(answer)

if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin

N = int(stdin.readline().rstrip())
A = [int(s) for s in stdin.readline().rstrip().split()]
ans = 0

for i in range(len(A)):
    if i == 0:
        continue

    if A[i-1] > A[i]:
        tmp = A[i-1]-A[i]
        ans = ans + tmp
        A[i] = A[i] + tmp

print(ans)

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

Solution (Python):
def Qc():
    n = int(input())
    l = list(map(int, input().split()))
    ans = 0
    last = l[0]
    for current in l:
        if last < current:
            last = current
        if current < last:
            stand = last - current
            ans += stand
            last = current + stand
    print(ans)


if __name__ == '__main__':
    Qc()

Solution (Python):

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

_sum = 0

for e in range(len(a_list)):
    
    if e != 0 :
        
        if a_list[e] < a_list[e - 1] :
            
            _sum = _sum + (a_list[e - 1] - a_list[e])
            a_list[e] = a_list[e] + (a_list[e - 1] - a_list[e])

print(_sum)

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

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

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

Solution (Python):


from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key
from bisect import bisect_left


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

def bindex(a,x):
    i = bisect_left(a,x)
    if i != len(a) and a[i] == x:
        return i
    return None

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    N = intinput()
    indatas = list(mulinputs())

    i = 0
    diffs = [0]
    l, r = 0, 1
    total = 0
    while r < N:
        while r < N and indatas[r] < indatas[l]:
            total += indatas[l] - indatas[r]
            r += 1
        l = r
        r += 1
    
    print(total)

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

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

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

ans = 0

length = len(A)
before = A[0]
for num in range(1, length):
    if before > A[num]:
        diff = before - A[num]
        ans += diff
        A[num] = before
    before = A[num]

print(ans)

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

ext = [0] * n
ans = 0
for i in range(1,n):
    if ary[i] < (ary[i - 1] + ext[i - 1]):
        ext[i] = (ary[i - 1] + ext[i - 1]) - ary[i]
print(sum(ext))

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

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
s = [0]*N
for i,n in enumerate(A):
    if i == 0:
        continue
    s[i] = max(0, A[i-1] + s[i-1] - A[i])

print(sum(s))

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

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
result = 0
mae = li[0]
for i in range(n):
    if mae > li[i]:
        result += mae - li[i]
        mae = li[i] + (mae - li[i])
    else:
        mae = li[i]
print(result)

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

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

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

    height = max(height, h)


print(ans)

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

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

Solution (Python):


user1 = int(input())
user2 = list(map(int, input().split()))

befHeight = -1
result = 0

for i in user2:
    if befHeight > i:
        result += befHeight - i
        i = befHeight
    befHeight = i

print(result)

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

count = 0
for i in range(n-1):
  if nlist[i]>nlist[i+1]:
    count += nlist[i]-nlist[i+1]
    nlist[i+1] = nlist[i]
print(count)

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

prev = 0
sum = 0
for A in input().split():
    a = int(A)
    if prev > a:
        sum += prev-a
    prev = max(prev, a)
print(sum)

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

    step = 0
    max_height = 0
    for i in A:
        if max_height < i:
            max_height = i
        else:
            step += max_height - i

    print(step)


def main():
    probC()


if __name__ == '__main__':
    main()

Solution (Python):

import sys

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

Solution (Python):
import sys
li  =   lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw  =   lambda : sys.stdin.readline().strip().split()
ni  =   lambda : int(sys.stdin.readline().strip())
nsi =   lambda : sys.stdin.readline().strip()

from collections import defaultdict as df
import math

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

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    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]
    print(ans)
resolve()

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
sm = 0
now = A[0]
for num in A:
    if num < now:
        sm += now-num
    elif num > now:
        now = num
print(sm)