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 = list(map(int, input().split())) 
s = [0]*(N-1)
 
for n in range(N-1):
  t = A[n] - A[n+1]
  if t <= 0: 
    continue
  else:
    A[n+1] += t
    s[n] = t

print(sum(s))

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

Solution (Python):
n = int(input())
a = list(map(int, list(input().split())))
cnt = 0
for i in range(len(a)-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):

import sys
import numpy as np
import time




n = int(input())
a = list(map(int, input().split()))
tmpMax = a[0]
ans = 0
for num in a:
	ans += max(0, tmpMax-num)
	if tmpMax < num:
		tmpMax = num
print(ans)

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

ans = 0
m = 0
for a in aa:
    if m > a:
        ans += (m-a)
    else:
        m = a
print(ans)

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

for i in range(1,n):
    if max > a[i]:
        sum += max - a[i];
    else:
        max = a[i];

print(sum);

Solution (Python):
def step(N, A):
    sum = 0
    for i in range(1, N):
        if A[i-1] > A[i]:
            sum += A[i-1] - A[i]
            A[i] = A[i-1]
    return sum
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    print(step(N, A))

Solution (Python):
n = int(input())
mn, *a = map(int, input().split())
ans = 0
for i in a:
    if i<mn:
        ans += mn-i
    else:
        mn = i
print(ans)

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

for i in range(len(l)):
    if m > l[i]:
        s += m - l[i]
    else:
        m = l[i]

print(s)

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

print(ans)

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

lb = A[0]
ans = 0

for a in A:
  if lb < a:
    lb = a
  elif lb > a:
    ans += lb - a
    
print(ans)

Solution (Python):


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

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

Solution (Python):
import sys
import math
def Ii():return int(sys.stdin.readline().rstrip())
def Mi():return map(int,sys.stdin.readline().split())
def Li():return list(map(int,sys.stdin.readline().split()))

n = Ii()
a = Li()
mx = 0
ans = 0
for i in a:
  if mx > i:
    ans += mx - i
  else:
    mx = i
    
    
print(ans)

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

tmax = A[0]
humidai = 0
humidai_sum = 0

for a in A :
  if tmax > a:
    humidai = tmax - a
    humidai_sum += humidai
  else :
    tmax = a
    
print(humidai_sum)

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

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

ans = 0
cur = 0

for a in arr:
    if a < cur:
        ans+=cur-a
    else:
        cur = a

print(ans)

Solution (Python):
kazu=int(input())
sintyo=list(map(int, input().split())) 
ans=0
takasa=sintyo[0]
for x in range(kazu-1):
    takasa2=sintyo[x+1]
    if(takasa>takasa2):
        sabun=takasa-takasa2
        ans+=sabun
    if(takasa<takasa2):
        takasa=sintyo[x+1]
print(ans)

Solution (Python):
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)

Solution (Python):
n = int(input())
a_lst = list(map(int, input().split()))
maximum = a_lst[0]
h_lst = []
for i in range(n):
    maximum = max(maximum, a_lst[i])
    h_lst.append(maximum - a_lst[i])
sum1 = sum(h_lst)
print(sum1)

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]
print(cnt)

Solution (Python):
n=int(input())
l=map(int,input().split())
max1=0
ans=0
for i in l:
  if max1>i:
    ans+=max1-i
  max1=max(max1,i)
print(ans)

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
H=0
M=0
for i in A:
    if i<M:
        H+=M-i
    else:
        M=i
print(H)

Solution (Python):
n = int(input())
dat = list(map(int, input().split()))
m = dat[0]
res = 0
for x in dat:
    res += max(0, m - x)
    m = max(m, x)
print(res)

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

min_h, sum_h = 0, 0
for a in A:
  if min_h < a:
    min_h = a
  sum_h += min_h - a

print(sum_h)

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

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

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

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

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

Solution (Python):

import math as M

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

num = input()
sum = 0
for ch in num :
    sum += ord(ch)-ord('0')
if sum%9==0 :
    print('Yes')
else :
    print('No')
exit()      

n,x,t = (map(int,input().split()))
print(t*(n//x) + t*(min(n%x,1)))
exit()

n = int(input())
a = list(map(int,input().split()))
cm = 1000
p1 = p2 = a[0]
for i in range(1,n):
    
    if(a[i]>=p2):
        p2 = a[i]
    elif(a[i]<p2):
        cm += (cm//p1)*(p2-p1)
        p1 = p2 = a[i]
cm += (cm//p1)*(p2-p1)
print(cm)
exit()

cm = 1000
for i in range(0,40):
    st = cm//100
    cm -= st*100
    cm += st*200
print(cm)
print(M.log10(cm))
print(M.log2(10**16))
exit()

isPrime = [1]*101
isPrime[0]=isPrime[1]=0
for i in range(2,int(M.sqrt(101)+1)) :
    if not isPrime[i] : continue
    for j in range(i*i,101,i) :
        isPrime[j]=0

pa = 5
pb = 1
ctr= 0
for i in range(2,101) : 
    if not isPrime[i] : continue
    if i==5 : 
        ctr+=1
        continue
    if(ctr&1) : 
        pa *= i
    else : 
        pb *= i
    ctr+=1
print(pa,' ',pa<=2*(10**18))
print(pb,' ',pb<=2*(10**18))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
 
total = 0
m = 0
 
for x in a:
  if m > x:
    total += m - x
  m = max(m,x)
  
print(total)

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

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

Solution (Python):


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

prev = 0
sum = 0
for i in a:
    if (prev > i):
       sum += prev - i
    else:
        prev = i

print(sum)

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

Solution (Python):
N, *A = map(int, open(0).read().split())

ans = 0
cur = A[0]
for a in A[1:]:
    if cur > a:
        ans += cur - a
    else:
        cur = a

print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
max=l[0]
c=0
for x in l:
    if max <x:
        max=x
    else:
        c+=(max-x)
print(c)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

ans = 0
ma = A[0]
for x in A[1:]:
    ans += max(0, ma-x)
    ma = max(ma, x)

print(ans)

Solution (Python):



def main():
  n = int(input())
  la = list(map(int, input().strip().split()))
  taller = -1
  step = 0
  for a in la:
    taller = max(taller, a)
    if taller > a:
      step += taller - a
  print(step)

if __name__=='__main__':
  main()

Solution (Python):
import sys

import math
def gcd(a,b):
	if a == 0:
		return b
	return gcd(b%a,a)

def solve(n):
	return n%9

def main():
	n = int(input())
	c= 0
	arr = list(map(int,input().split()))
	for i in range(1,n):
		if arr[i]<arr[i-1]:
			c+=arr[i-1]-arr[i]
			arr[i] = arr[i-1]
	print(c)

if __name__ == "__main__":
	main()

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

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

Solution (Python):
input()
step,height = 0,0
for i in map(int,input().split()):
    if height>i:
        step+=height-i
    height = max(height,i)
print(step)

Solution (Python):
  
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))


n = readint()
a = readints()

b = a[0]
ans = 0
for i in range(1,n):
    ans += max(b-a[i],0)
    b = max(a[i],b)
print(ans)

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

  for a in A:
      if m - a > 0:
          S += m - a
      if m < a:
          m = a

  print(S)


if __name__ == "__main__":
  main()

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

Solution (Python):
N=int(input())
l=list(map(int,input().split()))
s_1=sum(l)
n=len(l)

for i in range(n-1):
  if l[i]>l[i+1]:
    l[i+1]=l[i]
s_2=sum(l)

print(s_2-s_1)

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

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

Solution (Python):
N = int(input())
li = list(map(int,input().split()))
ans = 0
h = li[0]
for l in li[1:]:
    h = max(h,l)
    ans += h-l
print(ans)

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

AM = A[0]
dai = 0
for i in range(1, N):
  d = AM - A[i]
  if d > 0:
    dai += d
  else:
    AM = A[i]
print(dai)

Solution (Python):
def maxof(x, y):
    if x > y:
        return x
    return y

n=int(input())
a = list(map(int, input().strip().split()))
total=0
max_num=0
for i in range(len(a)):
    max_num=maxof(max_num,a[i])
    total=total+max_num-a[i]
print(total)

Solution (Python):
N = int(input())
data = input().split(" ")
for i in range(N):
  data [i] = int(data[i])

count = 0
for i in range(N-1):
  if data[i] < data[i+1]:
    None
  else:
    count = count + data[i] - data[i+1]
    data[i+1] = data[i]
print(count)

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

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

total_height = 0

pre = A[0]
for i in range(1, len(A)):
    if A[i] < pre:
        total_height += pre - A[i]
    else:
        pre = A[i]

print(total_height)

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

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

for i in range(N-1):
    v1, v2 = A[i], A[i+1]
    if v1<=v2:
        continue
    ans += v1-v2
    A[i+1] = v1
print(ans)

Solution (Python):


def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N = int(input())
    A = list(map(int, input().split()))

    return N, A


def main(A: list) -> None:
    """
    メイン処理.

    Args:\n
        A (list): 身長(1 <= A_i <= 10**9)
    """
    
    ans = 0
    highest = 0
    for A_n in A:
        highest = max(highest, A_n)
        ans += max(highest - A_n, 0)

    
    print(ans)


if __name__ == "__main__":
    
    N, A = get_input()

    
    main(A)

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

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

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

print(ans)

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

A_list = [int(i) for i in input().split()]
pre = 0
count = 0
for p in A_list:
    if pre > p:
        count += pre - p
    else:
        pre = p
print(count)

Solution (Python):
def main():
    from sys import stdin
    readline = stdin.readline
    n = int(readline())
    A = list(map(int, readline().split()))

    tallest = A[0]
    cnt = 0

    for height in A:
        if tallest > height:
            cnt += tallest - height
        else:
            tallest = height

    print(cnt)


if __name__ == "__main__":
    main()

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

    tallest = A[0]
    cnt = 0

    for height in A:
        if tallest > height:
            cnt += tallest - height
        else:
            tallest = height

    print(cnt)


if __name__ == "__main__":
    main()

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

    tallest = 0
    ans = 0

    for height in A:
        tallest = max(tallest, height)
        ans += tallest - height

    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):


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

max_h = -1
ans = 0

for i in range(N):
    
    if max_h > A[i]:
        ans += max_h - A[i]
    elif max_h < A[i]:
        max_h = A[i]

print(ans)

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

ans = 0
maxA = 0
for a in A:
    if a > maxA:
        maxA = a
    else:
        ans += (maxA - a)

print(ans)

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

    tallest = A[0]
    ans = 0

    for height in A:
        if height < tallest:
            ans += tallest - height
        elif height > tallest:
            tallest = height

    print(ans)


if __name__ == "__main__":
    main()

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

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

sum = 0
max_height_now = 0
for index in range(num):
  height_now = int(height[index])
  if height_now >= max_height_now:
    max_height_now = height_now
  else:
    sum += max_height_now - height_now

print(sum)

Solution (Python):

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

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

print(ans)

Solution (Python):
n = int(input())
cur = 0
step = 0
for a in map(int, input().split()):
  if a < cur:
    step += cur - a
  else:
    cur = a
print(step)

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]:
        tmp = A[i-1] - A[i]
        ans += tmp
        A[i] += tmp
print(ans)

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

ol = a[0]
s = 0
for i in a:
  if ol < i:
    ol = i
  else:
    s += ol - i
print (s)

Solution (Python):
import numpy as np

N=int(input())
A=list(map(int,input().split()))
F=[]
for i in np.arange(len(A)-1):
    if(A[i]>A[i+1]):
        F.append(A[i]-A[i+1])
        A[i+1]=A[i]

print(sum(F))

Solution (Python):

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    m = A[0]
    S = 0

    for a in A:
        if m - a > 0:
            S += m - a
        if m < a:
            m = a
    
    print(S)


if __name__ == "__main__":
    main()

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

h = 0
ans = 0
for a in A:
  if h > a:
    ans += h - a
  else:
    h = a
print(ans)

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

Solution (Python):
N = input()
A_list = input().split()
pre = 0
counter = 0

for i in A_list:
  if i == A_list[0]:
    pre = int(i)
    continue
  if pre > int(i):
    dist = pre - int(i)
    counter += dist
    pre = int(i) + dist
    continue
  pre = int(i)

print(counter)

Solution (Python):
N=int(input())
A=[int(i) for i in 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]
print(ans)

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

l = [0]*N

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

print(sum(l))

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

ans = 0
max = a[0]

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

    if max < a[i]:
        max = a[i]

print(ans)

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

ans = 0
max = a[0]

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

    if max < a[i]:
        max = a[i]

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split( )))
front = 0
step = 0
for height in a :
    x = 0
    if front > height :
        x = front - height
        step += x
    if front < height:
        front = height
print(step)

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

print(ans)

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

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

print(ans)

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

x = 0
front = 0
step = 0
for height in a :
    
    if front > height :
        x = front - height
        
        step += x
    if front < height:
        front = height
print(step)

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

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

Solution (Python):

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

print(Step)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
tmp = 0
sm = 0
for i in range(len(A)):
    sm += max(tmp, A[i])
    tmp = max(tmp, A[i])
print(sm-sum(A))

Solution (Python):
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)

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

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

cnt, high = 0, 0
for ai in a:
    cnt += max(0, high - ai)
    high = max(high, ai)
print(cnt)

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

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

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

Solution (Python):
n = int(input())
a = [int(i) for i in 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] - a[i])
print(ans)

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

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

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]
print(ans)

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

Solution (Python):

def main():
    import sys
    imput = sys.stdin.readline
    
    
    
    n = int(input())
    a = list(map(int, input().split()))

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

    print(answer)

    
if __name__ == "__main__":
    main()

Solution (Python):

def main():
    import sys
    imput = sys.stdin.readline
    
    
    
    n = int(input())
    a = list(map(int, input().split()))

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

    print(answer)

    
if __name__ == "__main__":
    main()

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())
array = []
array+=map(int,input().split())
ans=0
premax=0
for n in range(N):
    premax=max(array[n],premax)
    ans+=premax-array[n]
print(ans)

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

def tako(An):
    stool = 0
    Anstool = [An[0]]
    for n in range(len(An)-1):
        pers = An[n+1]
        fr = Anstool[n]
        if fr > pers:
            stool += fr - pers
            pers = fr
        Anstool.append(pers)
    return stool

print(tako(An))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
maxa=0
total=0
for i in a:
  maxa=max(maxa,i)
  total=total+maxa-i
print(total)

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

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

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

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

sum=0

for i in range(0,len(length)-1):
  if length[i]>length[i+1]:
    dai=length[i]-length[i+1]
    length[i+1]+=dai
    sum+=dai
  else:
    continue

print(sum)

Solution (Python):
n = int(input())
a =list(map(int,input().split()))
maxVal = 0
total = 0
for num in a:
    maxVal = max(maxVal,num)
    total += maxVal - num
print(total)

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

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

print(step)

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

for i in range(1, len(A)):
  if A[i] < h:
    S += (h - A[i])
    h = max(h, A[i-1])
  else:
    h = A[i]

print(S)

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

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

print(ans)

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

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

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]:
        cnt += A[i] - A[i+1]
        A[i+1] = A[i]
print(cnt)

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

tmp = 0
ans = 0
for i in range(len(A)):
    tmp = max(tmp, A[i])
    ans += tmp - A[i]
print(ans)

Solution (Python):
def INT():
    return int(input())

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

def LI():
    return list(map(int, input().split()))

N = INT()
A = LI()

max_now = A[0]
ans = 0

for Ai in A:
    max_now = max(max_now, Ai)
    ans += max_now - Ai
    
print(ans)

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    max_height = A[0]
    ans = 0
    for a in A:
        if a > max_height:
            max_height = a
        ans += max_height - a
    print(ans)


if __name__ == '__main__':
    main()

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


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

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    m = 0
    ans = 0
    for i in range(N) :
        m = max(m,A[i])
        if(A[i] < m):
            ans += m - A[i]
    print(ans)


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
def solve(n, a_list):
    cum_max = 0
    res = 0
    for i in range(n):
        a = a_list[i]
        if a < cum_max:
            res += cum_max - a
        cum_max = max(a, cum_max)
    return res


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    res = solve(n, a_list)
    print(res)


def test():
    assert solve(5, [2, 1, 5, 4, 3]) == 4
    assert solve(5, [3, 3, 3, 3, 3]) == 0


if __name__ == "__main__":
    test()
    main()

Solution (Python):




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

    arr = A[0]
    ans = 0
    for i in A:
        ans += abs(arr - i) if i <= arr else 0
        arr = max(arr, i)
    print(ans)


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

Solution (Python):
n=int(input())
l=map(int,input().split())
ans=0
maxt=0
for i in l:
  if i<maxt:
    ans+=maxt-i
  maxt=max(i,maxt)
print(ans)

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]
print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
ans=0
maxt=0
for i in l:
  if i<maxt:
    ans+=maxt-i
  maxt=max(i,maxt)
print(ans)

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

print(b)

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

Solution (Python):
_ = int(input())
s = h = 0
for a in map(int, input().split()):
    h = max(h, a)
    s += h - a
print(s)

Solution (Python):
a = int(input())
b = input().split()

now_max = 0
score = 0
for i in list(b):
  now = int(i)
  if now > now_max :
    now_max = now
  else:
    score += now_max - now

print(score)

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

hight_now = a[0]
ans = 0

for i in range(n):
    if hight_now > a[i]:
        ans += hight_now - a[i]
    else:
        hight_now = a[i]

print(ans)

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

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

print(ans)