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

Solution (Python):

import functools
n = int(input())
A = list(map(int, input().split(" ")))

res = 0
hist = []
for i in range(n-1):
    if A[i] > A[i+1]:
        sa = A[i] - A[i+1]
        res += sa
        A[i+1] += sa

print(res)

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

res = val = 0
for i in a:
    res += val - min(val, i)
    val = max(val, i)
print(res)

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

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

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

mae_max = []
max_tall = 0
for i in range(N):
    max_tall = max(max_tall, A[i])
    mae_max.append(max_tall)

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

print(ans)

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

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

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

Solution (Python):









import os
import sys




def main():
    n = int(input())
    A = list(map(int, input().split()))
    base = A[0]
    ans = 0
    for a in A:
        if base > a:
            ans += base-a
        if base < a:
            base = a
    print(ans)
    


if __name__ == "__main__":
    main()

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

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

ma = 0
ans = 0
for a in A:
    ans -= max(0, ma-a)
    ma = max(ma, a)

print(-ans)

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

minval = 0
previous = 0
for i in range(len(A)):
    if i == 0:
        previous = A[0]
        continue

    diff = previous - A[i]

    if diff > 0:
        minval += diff
    previous = A[i] + diff if diff > 0 else A[i]

print(minval)

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

print(ans)

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

prev_A = As[0]
total_step_hight = 0
for i in range(1,len(As)):
    if  As[i-1] > As[i]:
        total_step_hight += As[i-1] - As[i]
        As[i] = As[i] + (As[i-1] - As[i])

print(total_step_hight)

Solution (Python):












N=int(input())

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

highest = A[0]
dai = 0

for Ai in A[1:]:
    if Ai <= highest:
        dai += highest-Ai
    else:
        highest = Ai

ans = dai
print(ans)

Solution (Python):
N=int(input())
AA=list(map(int,input().split()))
s=0
key=0
for i in range(N):
    key=max(key,AA[i])
    s+=key
print(s-sum(AA))

Solution (Python):
def main():
    N = int(input())
    As = list(map(int, input().split()))
    ans = 0
    for i in range(len(As)-1):
        if As[i+1] < As[i]:
            ans += As[i]-As[i+1]
            As[i+1] = As[i]


    return ans

if __name__ == '__main__':
    print(main())

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

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

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

print(ans)

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

    print(ans)

resolve()

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

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

tmp = A[0]
ans = 0

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

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

now = A[0]
ans = 0
for a in A:
    if a < now:
        ans += now - a
        continue
    now = a

print(ans)

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

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

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

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

print(ans)

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

prev = A[0]
total = 0
for a in A[1:]:
  if prev > a:
    total += prev - a
  else:
    prev = a
    
print(total)

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
step = 0
high = A[0]
for i in range(N):
    high = max(high,A[i])
    step += max(0,high-A[i])
print(step)

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

current = a[0]
steps = 0

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

print(steps)

Solution (Python):
import math
import re



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

tempmax=0
retnum=0

tempmax=a[0]

for i in range(len(a)):
    if tempmax>a[i]:
        retnum+=tempmax-a[i]
    else:
        tempmax=a[i]
print(retnum)

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

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

print (tot)

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

top = A[0]
ans = 0

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

Solution (Python):
n = int(input())
As = [int(i) for i in input().split()]
maxs = [0]
for a in As:
    maxs.append(max(a, maxs[-1]))
maxs = maxs[1:]
print(sum(m-a for a, m in zip(As, maxs)))

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

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

if __name__ == "__main__":
    resolve()

Solution (Python):

import os
import sys
from io import BytesIO, IOBase


 
 
def main():
    n=int(input())
    a=list(map(int,input().split(" ")))
    s=0
    for x in range(1,n):
        if a[x]<a[x-1]:
            s=s+a[x-1]-a[x]
            a[x]=a[x-1]
    print(s)





BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

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

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

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

ans = 0
max_a = A[0]

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

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

Solution (Python):
import sys
read = sys.stdin.readline
import time
from heapq import heappush, heappop, heapify
import math
import itertools as it
def inp():
    return int(input())
def inpl():
    return list(map(int, input().split()))
start_time = time.perf_counter()


N = inp()
A = inpl()

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)


end_time = time.perf_counter()
print('time:', end_time-start_time, file=sys.stderr)

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

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

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

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

cnt_step = 0

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

print(cnt_step)

Solution (Python):

"""
Created on Sat Aug 15 17:00:46 2020

@author: saito
"""






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


ans = 0
f = 0
for ai in a:
    if f <= ai:
        f = ai
    else:
        ans += f - ai



print(ans)

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

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

print(s)

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

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

print(ans)

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

base = 0
before_a = ai[0]

for a in ai[1:]:
    if before_a < a:
        before_a = a

    else:
        base += (before_a - a)

print(base)

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

Solution (Python):
n, *A = map(int, open(0).read().split())
a0 = A[0]
s = 0
for i in range(1, n):
    a1 = A[i]
    if a0 <= a1:
        a0 = a1
    else:
        s += a0 - a1
        a1 = a0
print(s)

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

ans=0

for i in range (1,N):
    if Als[i]>=Als[i-1]:
        pass
    else :
        step=abs(Als[i]-Als[i-1])
        ans+=step
        Als[i]+=step
print(ans)

Solution (Python):
import math
import sys,collections
from sys import stdin, stdout  


def get_ints(): return map(int, sys.stdin.readline().strip().split())
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_string(): return sys.stdin.readline().strip() 
def input(): return sys.stdin.readline().strip()

def file():
	sys.stdin = open('input.py', 'r')
	sys.stdout = open('output.py', 'w') 
def is_subsequence(x, y):
    """Test whether x is a subsequence of y"""
    x = list(x)
    for letter in y:
        if x and x[0] == letter:
            x.pop(0)

    return not x    
def sieve(): 
	spf[1] = 1
	for i in range(2, MAXN): 
		spf[i] = i 
	for i in range(4, MAXN, 2): 
		spf[i] = 2

	for i in range(3, mt.ceil(mt.sqrt(MAXN))): 
		if (spf[i] == i): 
			for j in range(i * i, MAXN, i): 
				if (spf[j] == j): 
					spf[j] = i 
def getFactorization(x): 
	ret = list() 
	while (x != 1): 
		ret.append(spf[x]) 
		x = x // spf[x] 

	return ret 
def getFloor(A, x):

    (left, right) = (0, len(A) - 1)

    ind,floor = -1,-1
    while left <= right:
        mid = (left + right) // 2
        '''if A[mid] == x:
            return mid'''
        if x < A[mid]:
            right = mid - 1
        else:
            floor = A[mid]
            ind=mid
            left = mid + 1
            
    return ind
def check(st) : 
  
    
    n = len(st) 
    digitSum = 0
      
    for i in range(0,n) : 
        digitSum = digitSum + (int)(st[i]) 
  
    
    
    return (digitSum % 9 == 0)     


mod=(10**9)+7

def main():
	n=int(input())
	l=get_array()
	c=0
	for i in range(1,n):
		if(l[i]>=l[i-1]):
			continue
		else:
			diff=l[i-1]-l[i]
			c+=diff
			l[i]+=diff
	print(c)		

	





















	




















		
			


		

			

		





			

	























				

	















	

	










if __name__ == '__main__':
    main()

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

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

print(ans)

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

maxheight = a[0]
addon = 0

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

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

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

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

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

print(ans)

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

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

m = a[0]
f = 0

for i in range(1, len(a)):
  if m > a[i]:
    f += m - a[i]
  else:
    m = a[i]

print(f)

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

hmax=A[0]
hsum=0

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

print(hsum)

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

for i in range(1,n+1):
    if a[i-1] > a[i]:
        apls = a[i-1] - a[i]
        a[i] += apls
        pl += apls
print(pl)

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

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

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

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

Solution (Python):



import collections
import sys
import copy
import re
import math
import itertools


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


def main():
    N = I()
    AArray = LI()
    ans = 0
    max_sintyou = 0

    for A in AArray:
        if max_sintyou > A:
            sa = max_sintyou-A
            ans += sa
            A += sa

        max_sintyou = max(A, max_sintyou)
    print(ans)


if __name__ == '__main__':
    main()

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

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

for a in A:
    if preMax < a:
        preMax = a
        m = a 
    if a < m:
        ans += abs(a-m)
    preMax = max(a, preMax)
print(ans)

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

prev = A[0]
sum_ = 0
for a in A[1:]:
    if a >= prev:
        d = 0
    else:
        d = prev - a

    sum_ += d
    prev = a + d

print(sum_)

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

ans = 0
cmax = A[0]
for a in A[1:]:
    if a < cmax:
        ans += cmax - a
    else:
        cmax = a
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    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)


if __name__ == '__main__':
    main()

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

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

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

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

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

ans = 0
anchor = A[0]
for i in range(N):
    if anchor < A[i]:
        anchor = A[i]
    else:
        ans += abs(anchor - 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+1]-a[i]<0:
        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
high=A[0]

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

print(ans)

Solution (Python):
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()

ans = 0

if n==1:
	print(ans)
	exit()

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

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
tmpmax = A[0]
for a in A:
    if a < tmpmax:
        ans += tmpmax - a
    elif a > tmpmax:
        tmpmax = a

print(ans)

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

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

ans = 0
prior = A[0]
for a in A:
  ans += max(prior - a, 0)
  prior = max(prior,a)
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]

print(ans)

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

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

Solution (Python):

N = int(input().strip())


A = [int(x) for x in input().strip().split(" ")]

cnt = 0

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

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools


sys.setrecursionlimit(10**7)






N = int(input())
A = [int(i) for i in input().split()]
ans = 0
temp = A[0]

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

print(ans)

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
before = sum(A)
former = A[0]
for i in range(n):
    if A[i] < former:
        A[i] = former
    else:
        former= A[i]
print(sum(A) - before)

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

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    N = readint()
    A = list(readnums())
    m = A[0]
    ans = 0
    for i in range(1, N):
        if m < A[i]:
            m = A[i]
        else:
            ans += m - A[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
a=[int(x) for x in input().split()]
sum=0
for i in range(len(a)-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()))

cnt = 0
m = A[0]
for i in range(1, N):
    sub = max(A[i-1], m) - A[i]
    if sub > 0:
        cnt += sub
    else:
        m = A[i]
print(cnt)

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,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 = [int(i) for i in input().split()]
ans = 0
h_max = A[0]
for i in range(1, N):
    if h_max > A[i]:
        ans += h_max - A[i]
    h_max = max(h_max, A[i])
print(ans)

Solution (Python):

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

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

    print(ans)


if __name__ == "__main__":
    resolve()

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

a_tmp = a_list[0]

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

print(sum)

Solution (Python):
n = int(input())
lis = list(map(int,input().split()))
maxNum = 0
lis2 = []
for i in range(n):
    a = lis[i]
    maxNum = max(maxNum,a)
    lis2.append(maxNum)
print(sum(lis2)-sum(lis))

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

Solution (Python):




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


hmax = 0
fsum = 0
for i in range(alen):
    if hmax < a[i]:
        hmax = a[i]
    fsum = fsum + (hmax - a[i])


print(fsum)

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

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

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

s = 0
x = 0
for i in A:
    if i < x:
        s += x - i
    else:
        x = i
print(s)

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

tallest= 0
ans = 0
for person in a_s:
	if person>tallest:
		tallest= person
	else:
		ans+= tallest-person
print(ans)

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

ans = 0

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

Solution (Python):
N=int(input())
A=[int(x) for x in input().split()]
ans=0
maxheight=0
for i in range(N):
    
    ans+=max(0,maxheight-A[i])
    maxheight=max(maxheight,A[i])

print(ans)

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

n = 0
count = 0

for a in A:
  if n > a:
    count += n-a
  else:
    n = a
print(count)

Solution (Python):

def main():
    _ = int(input())
    A = [int(x) for x in input().split()]

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


if __name__ == '__main__':
    main()

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

ans_list=[]
a_max=0

for a_idx,a in enumerate(A):
    if a > a_max:
        a_max = a
        ans_list.append(0)
    else:
        ans_list.append(a_max-a)
        
print(sum(ans_list))

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):
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = LI()
ans = 0
if n == 1:
    print(0)
    exit()

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

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

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
data = list(map(int, stdin.readline().rstrip().split()))

min = data[0]
count = 0
for i in data:
    if i < min :
        count += min - i
    else:
        min = i
print(count)

Solution (Python):
n=int(input())
A=[int(_) for _ in input().split()]
c=A[0]
ans=0
for a in A:
    if a<c:
        ans+=c-a
    else:
        c=a

print(ans)

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

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

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

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

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

print(cnt)

Solution (Python):
n=int(input(""))
lisa=input("").split(" ")
t=0
s=int(lisa[0])
for i in lisa:
    if(s<int(i)):
        s=int(i)
    else:
        t+=s-int(i)
print(t)

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)