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()))

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

print(ans)

Solution (Python):
def A():
    n, x, t = map(int, input().split())
    print(t * ((n+x-1)//x))

def B():
    n = list(input())
    s = 0
    for e in n:
        s += int(e)
    print("Yes" if s % 9 == 0 else "No")

def C():
    int(input())
    a = list(map(int, input().split()))
    l = 0
    ans = 0
    for e in a:
        if e < l: ans += l - e
        l = max(l, e)
    print(ans)

C()

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

S = []
X = 0
for i in range(N):
    if X < A[i]:
        X = A[i]
    else:
        pass
    S.append( X - A[i] )

print(sum(S))

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
tlest, total = As[0], 0
for i in range(1, N):
	tlest = max(tlest, As[i])
	if tlest - As[i]:
		total += tlest - As[i]
print(total)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
a=0
s=[]
for i in A:
  if i<a:
    s.append(a-i)
    
  else:
    a=i
print(sum(s))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
total = 0
maximum = A[0]
for a in A[1:]:
  geta = max(0, maximum - a)
  total += geta
  maximum = a + geta

print(total)

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

highest = 0
total = 0

for A in As:
  highest = max(int(A), highest)
  total += highest - int(A)

print(total)

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

max_n = A[0]
ans = 0
for i in range(N):
    tmp = max_n - A[i]
    if tmp > 0:
        ans += tmp
    if max_n < A[i]:
        max_n = A[i]
print(ans)

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

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

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()))

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):
import sys
read = sys.stdin.read

def main():
    n, *a = map(int, read().split())
    r = 0
    maxtall = a[0]
    for i1 in range(1, n):
        if a[i1] > maxtall:
            maxtall =  a[i1]
        else:
            r += maxtall - a[i1]
    print(r)

if __name__ == '__main__':
    main()

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
m = A[0]
ans = 0
for a in A[1:]:
    m = max(m,a)
    if m > a:
        ans += m-a
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())
lis=list(map(int,input().split()))
lis_s=lis.copy()

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

print(sum(lis)-sum(lis_s))

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

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

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

print(ans)

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

s = 0

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

print(s)

Solution (Python):


from typing import *
import collections
import functools as fts
import itertools as its
import math
import sys

INF = float('inf')


def solve(N: int, A: "List[int]"):
    ans = 0
    m = A[0]
    for a in A[1:]:
        ans += max(0, m - a)
        m = max(m, a)
    return ans


def main():

    sys.setrecursionlimit(10 ** 6)

    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)]  
    print(f'{solve(N, A)}')


if __name__ == '__main__':
    main()

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

result = 0
p = arr[0]

for i in range(1, len(arr)):
  if p >= arr[i]:
    result += p - arr[i]
  else:
    p = arr[i]

print(result)

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

for a in A[1:]:
  if a < max:
    ans += max - a
  else:
    max = a

print(ans)

Solution (Python):
N = int(input())
str_in = input()
num = [int(n) for n in str_in.split()]
num = list(map(int, str_in.strip().split()))

hight = num[0]
stools = 0

for x in range(1,len(num)):
    if hight>num[x]:
        stools+=hight-num[x]
    elif num[x] > hight:
        hight = num[x]       
print (stools)

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

sum = 0
k = a[0]

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

print(sum)

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

ans = 0

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

print(ans)

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

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

print(step_sum)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    
    for i in range(1, len(a)):
        humidai = max(0, a[i-1] - a[i])
        ans +=  humidai
        a[i] += humidai
    print(ans)
        
        
if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
ls=[int(s) for s in input().split()]
m=ls[0]
ans=0
for i in range(1,N):
  if ls[i]>m:
    m=ls[i]
  else:
    ans+=(m-ls[i])
print(ans)

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

ans = 0

for i in range(n):
    if i == 0:
        continue

    if a[i-1] > a[i]:
        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()))

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()))
ans = 0
M = -1

for i in range(N):
  M = max([M, A[i]])
  if A[i] < M:
    ans += M - A[i]
print(ans)

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

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

print(ans)

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

h = a[0]
step = 0
for i in range(1,n):
    if a[i] > h:
        h = a[i]
    elif a[i] < h:
        d = h - a[i]
        step += d

print(step)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split(" ")))
    top = a[0]
    ans = 0

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

    print(ans)


main()

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

print(ans)

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

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

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

Solution (Python):
n = int(input())
L = list(map(int, input().split()))
m = 0
ans = 0
for l in L:
    m = max(m, l)
    ans += m - l
print(ans)

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

for l in L:
  if m == l: 
    continue
  elif m > l: 
    ans += (m - l)
  elif m < l:
    m = l
print(ans)

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

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

Solution (Python):

n = int(input())

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

l = []

t = li[0]
for i in range(1,len(li)):
    if t >= li[i]:
        l.append(t - li[i])
    else:
        l.append(0)
        t =li[i]

print(sum(l))

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

total, large = 0,0

for i in A:
  
  if large > i:
    total += (large - i)
  
  if i > large: large = i
    
print(total)

Solution (Python):

def inputlist(): return [int(j) for j in input().split()]

N = int(input())
A = inputlist()
maxa = A[0]
ans= 0
for i in range(1,N):
    if A[i] < maxa:
        ans += maxa - A[i]
    else:
        maxa = A[i]
print(ans)

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

n = int(input())
a = list(map(int,input().split()))
ma = -1
res = 0
for i in a:
    res += max(ma - i,0)
    ma = max(ma,i)
print(res)

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

print (total)

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

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

count = 0
for i in range(N) :
    if i==0 :
        continue
    if A[i]<A[i-1] :
        count += abs(A[i]-A[i-1])
        A[i] += abs(A[i]-A[i-1])

print(count)

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

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

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

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

print(ans)

Solution (Python):
N = int(input())
people = [int(i) for i in input().split()]
step = 0
front = people[0]
for i in range(N):
    if front > people[i]:
        step += front - people[i]
    else:
        front = people[i]
print(step)

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

A = (int(x) for x in input().split())

A = list(A)

step = 0

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



print(step)

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

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

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

print(ans)

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

for i in range(0, 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()))
amax = a[0]
asum = 0
acount = 0
result = 0

for i in range(0,n):
  if amax > a[i]:
    acount+=1
    asum+=a[i]
    if i == n-1:
      result += amax*acount-asum
  else:
    result += amax*acount-asum
    amax = a[i]
    asum = 0
    acount = 0

    
print(result)

Solution (Python):
def solver(arr):
    stool = 0
    for i in range(1, len(arr)):
        
        if arr[i]<arr[i-1]:
            stool += -arr[i]+arr[i-1]
            arr[i] = arr[i-1]
    return stool


if __name__=='__main__':
    n = int(input())
    arr = list(map(int,input().split()))
    print(solver(arr))

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

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

answer = 0
maximum = arr[0]
for index in range(1, len(arr)):
    maximum = max(maximum, arr[index])
    answer += abs(maximum - arr[index])

print(answer)

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

Solution (Python):
import sys
n=int(input())
a=[int(i) for i in sys.stdin.readline().split()]
m=0
sums=0
for i in a:
    m=max(m,i)
    if i<m:
        sums+=m-i
    
print(sums)

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

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


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

Solution (Python):
N = int(input())
A = list(map(int, input().strip().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
for i in range(n):
    if i != 0:
        if a[i] < a[i-1]:
            ans += a[i-1]
            ans -= a[i]
            a[i] = a[i-1]

print(ans)

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

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

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]
    s = 0
    for i in range(1, N):
        pre = A[i-1] 
        now = A[i] 
        if pre > now:
            step = pre-now
            s += step
            A[i] += step
    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+1] - a[i] >= 0 :
        continue
    else :
        ans += abs(a[i+1] - a[i])
        a[i+1] += abs(a[i+1] - a[i])
print(ans)

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

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

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

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
ans = 0 
t = 0
for e,(i , j) in enumerate(zip(A,A[1:])):
    if i + t > j : 
        ans += t + i-j
        t= t + i-j
    else:t = 0
    
print(ans)

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

Solution (Python):
n = int(input())
tall = input().split(" ")

step = 0
for i in range(n-1):
  c = int(tall[i+1]) - int(tall[i])
  if c < 0:
    step -= c
    tall[i+1] = int(tall[i+1]) - c
print(step)

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())
input_line = input().split()
member = [int(input_line[i]) for i in range(n)]

stands = 0
for i in range(1,n): 
  stand = member[i-1] - member[i]
  if stand > 0:
    stands += stand
    member[i] += stand
print(stands)

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

humidai = [0]*N
s = A[0]

for i in range(1, N):
    if A[i]<s:
        humidai[i]=s-A[i]
    else:
        s = A[i]

print(sum(humidai))

Solution (Python):
n = int(input())
a_list = [int(x) for x in input().split()]
ans = h = 0
for a in a_list:
    if a > h:
        h = a
    else:
        ans += h - a
print(ans)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]
ma = A[0]
total = 0
for a in A:
  if a < ma:
    total += ma - a
  ma = max(ma, a)
print(total)

Solution (Python):

n = int(input())

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

ans = []

for i in range(n-1):
    if a[i] >= a[i+1]:
        ans.append(a[i] - a[i + 1])
        a[i+1] += a[i]-a[i+1]

print(sum(ans))

Solution (Python):


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

sum = 0

for i in range(0, N):
  if i == 0:
    continue
  elif (shincho[i] - shincho[i-1]) < 0:
    sum += shincho[i-1] - shincho[i]
    shincho[i] = shincho[i-1]

print(sum)

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N-1):
    next = i + 1
    if A[i] > A[next]:
        a =A[i] - A[next]
        ans += a
        A[next] += a
    else:
        pass
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 = [int(x) for x in input().split()]
maxv = 0
ans = 0
for a in A:
    if maxv > a:
        ans += maxv - a
    else:
        maxv = a
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:
        t = A[i]
    elif A[i] == t:
        pass
    else:
        ans += t - A[i]
print(ans)

Solution (Python):




N = int(input())
cur_max = 0
ans = 0

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

for i in A:
    if cur_max > i:
        ans += cur_max - i

    cur_max = max(i, cur_max)
print(ans)

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

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

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

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

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

Solution (Python):

def main():
    n = int(input())
    
    a = list(map(int, input().split()))
    
    mx = 0
    result = 0
    for num in a:
        mx = max(mx, num)
        if mx > num:
            result += mx - num
    print(result)


if __name__ == '__main__':
    main()

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

ans = 0
for i in range(n-1):
    if al[i] >= al[i+1]:
        ans += al[i]-al[i+1]
        al[i+1] += (al[i]-al[i+1])

print(ans)

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

x = 0
m = a[0]

for i in range(n):
    if a[i] < m:
        x += m - a[i]
    else:
        m = a[i]
    
print(x)

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

ans = A[0]
a = A[0]

for i in range(1, N):
    ans += max(A[i], a)
    a = max(A[i], a)

print(ans - sum(A))

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

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

print(s)

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

b = [0] * n

for i in range(n-1):
    if a[i] + b[i] > a[i+1]:
        b[i+1] += (a[i] + b[i]) - a[i+1]
ans = sum(b)
print(ans)

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

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

Solution (Python):



N = int(input())


N_i = [0] * N


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


Sum = 0


for i in range(1, N):
    
    Sum_i = 0

    
    if N_i[i-1] > N_i[i]:
        
        Sum_i = N_i[i-1] - N_i[i]
        
        N_i[i] = N_i[i] + Sum_i
        
        Sum = Sum + Sum_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] < a[i - 1]:
        ans += a[i - 1] - a[i]
        a[i] = a[i - 1]

print(ans)

Solution (Python):
from sys import stdin
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]


n = int(input())
A = ip()
k = A[0]
ans = 0
for i in range(len(A)):
    k = max(k, A[i])
    ans += (k - A[i])
print(ans)

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

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


def ans176(N:int, A:str):
    A=list(map(int,A.split()))
    step_count=0
    max=A[0]
    for i in range(1,N):
        if A[i]<max:
            step_count=step_count+max-A[i]
        else:
            max=A[i]
    return(step_count)

print(ans176(N,A))

Solution (Python):




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

key = A[0]
ans = 0

for a in A:
    if  a < key:
        ans += key - a
        
    else:
        key = a

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

max_len = A[0]
sum = 0

for ai in A:
    if ai < max_len:
        sum += max_len - ai
    else:
        max_len = ai

print(sum)

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

ans = 0
for i in range(N-1):
  add = max(S[i] - S[i+1],0)
  S[i+1] += add
  ans += add
  
print(ans)

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

ans = 0
h = A[0]

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

print(ans)

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

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

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

print(str(s))

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

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

print(str(s))

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

height_list = [0] * N

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

print(sum(height_list))

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

sum=0;
max=a[0];
for aa in a:
    if aa<max:
        sum=sum+max-aa
    else:
        max=aa

print(sum)

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

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

ans = 0
maxa = 0
for aa in a:
    maxa = max(maxa, aa)
    ans += maxa - aa
print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
ans = 0
now = A[0]
for a in A:
    if now > a:
        ans += now -a
    if now < a:
        now = a
print(ans)

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

for i in range(n):
    b = max(b,a[i])
    c += (b-a[i])

print(c)

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

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

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

ans = 0
max = A[0]

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

print(ans)

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

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

print(Sum)

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

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

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

print(sum)

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

now = 0
cost = 0
for A in As:
    if A > now:
        now = A
    else:
        cost += now-A
print(cost)

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

max_list = []
m = 0
ans = 0
for a in A:
    ans += max(m - a, 0)
    max_list.append(m)
    m = max(m, a)

print(ans)

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets = "abcdefghijklmnopqrstuvwxyz"
ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD = 1000000007
INF = float("inf")

sys.setrecursionlimit(1000000)

def main():
    N = I()
    A = LI()

    s = 0
    high = 0
    for ai in A:
        if ai < high:
            s += high - ai
        else:
            high = ai

    print(s)

if __name__ == '__main__':
    main()

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

def judge(x, y):
  if x > y:
    return(x-y)
  else:
    return(0)
ans = 0
for i in range(n - 1):
  xy = judge(a[i], a[i+1])
  a[i+1] += xy
  ans += xy
  
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
height = 0
res = 0
for i in A:
    if i > height:
        height = i
    elif i < height:
        res += height - i
print(res)

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

if len(A) == 1:
    print(0)
    
else:
    for i in A:
        if A[con] > A[con+1]:
            A[con+1] =  A[con]
        
        if A[con+1] == A[-1]:
            break

        con += 1

    ans = sum(A) - raw
    print(ans)

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

print(fumidai)

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

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

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