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

def change_height(list, n):
    for i in range(n-1):
        if list[i] > list[i+1]:
            diff = list[i] - list[i+1]
            list[i+1] += diff
    return list

c_num_list = []
for i in range(N):
    c_num_list.append(num_list[i])
changed_list = change_height(num_list, N)
total_step = 0
for j in range(N):
    total_step += changed_list[j] - c_num_list[j]

print(total_step)

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

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



max_l = l[0]
count = 0
for i in range(1,len(l)):
    if max_l >= l[i]:
        count =count + (max_l - l[i])
    else:
        max_l = l[i]
print(count)

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

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

Solution (Python):
elm_cont = int(input())
array = [int(i) for i in input().split()]

current = array[0]
sum = 0

for i in range(1, elm_cont):
    if array[i] < current:
        sum += current - array[i]
    else:
        current = array[i]

print(sum)

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

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

prev_max = 0
ans = 0
for a in A:
    if a > prev_max:
        prev_max = a
    else:
        ans += (prev_max - 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] > 0:
        Sum += A[i] - A[i+1]
        A[i+1] += A[i] - A[i+1]
        
print(Sum)

Solution (Python):
def solve(string):
    _, *a = map(int, string.split())
    ans = step = 0
    for a0, a1 in zip(a, a[1:]):
        step = max(0, a0 + step - a1)
        ans += step
    return str(ans)


if __name__ == '__main__':
    import sys
    print(solve(sys.stdin.read().strip()))

Solution (Python):
N = input()

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

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

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

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

prev = 0
ans = 0
for a in A:
    if a < prev:
       ans += prev - a

    prev = max(prev, a)

print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):

    if a[i] >= max_height:
        max_height = a[i]
        continue

    result += (max_height - a[i])

print(result)

Solution (Python):
N=int(input())
A= list(map(int, input().split()))
takai=A[0]
kot=0
for a in range(N):
    if takai<=A[a]:
        takai=A[a]
    else:
        kot+=takai-A[a]

print(kot)

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

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

answer = 0

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

print(answer)

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
mx=l[0];total=0
for it in l:
  if it>mx:mx=it
  total+=mx-it
print(total)

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

ans = 0

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

Solution (Python):


N = int(input())

A = [int(y) for y in 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("{}".format(ans))

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

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

print(ans)

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

ans = 0

for i in range(N - 1):
    if As[i] >As[i+1]:
        diff = As[i] - As[i+1]
        As[i +1] = As[i]
        ans += diff
    else:
        pass
    
print(ans)

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

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

print(answer)

Solution (Python):
from math import ceil
def main():
    n=int(input())
    aa=list(map(int,input().split()))
    m=aa[0]
    d=0
    for a in aa:
        if a<m:
            d+=m-a
        elif a>=m:
            m=a
    print(d)
main()

Solution (Python):
max_height = 0
step_sum = 0

input()
people_height_array = map(int, input().split())

for height in people_height_array:
    if max_height <= height:
        max_height = height
    else:
        step_sum += max_height - height

print(step_sum)

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

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

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

max = 0
sum = 0
for a in A:
  if a < max:
    sum += max - a
  if a > max:
    max = a

print(sum)

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

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

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

for i,s in enumerate(a) :
    if i > n-2:
      break
    if a[i] > a[i+1] :
        ans = 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
mx_a = a[0]
for i in range(n):
    if mx_a > a[i]:
        ans += mx_a - a[i]
    
    mx_a = max(mx_a, a[i])

print(ans)

Solution (Python):
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from functools import lru_cache
import math


rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')
MOD = 10**9 + 7

def main():
	n = int(rl())
	H = list(rli())
	maxx = H[0]
	ans = 0
	for i in range(1, n):
		if H[i] < maxx:
			ans += maxx - H[i]
		maxx = max(H[i], maxx)
	print(ans)
	stdout.close()

if __name__ == "__main__":
	main()

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

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

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

tall = list(map(int, input().split()))
front = tall[0]
standard = front
result = 0


for i in range(1, loop):
  back = tall[i]
  
  if back-standard > 0:
    standard = back
  elif back-standard < 0:
    result += standard - back
  front = back

print(result)

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())
list1 = list(map(int, input().split()))
h,c = 0,0
for i in list1:
    if i < h:
        c += h - i
    else:
        h = i
print(c)

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
for i in range(1, N):
    if A[i-1] > A[i]:
        d = A[i-1] - A[i]
        A[i] += d
        ans += d
print(ans)

Solution (Python):
N = int(input())
array = list(map(int,input().split()))
d,f = 0,array[0]
ans = [0]
for i in range(1,N):
  n = array[i]
  d = f - n
  if d >= 0:
    ans.append(d)
  else:
    ans.append(0)
  if n > f:
    f = n
print( sum(ans) )

Solution (Python):
N = int(input())
array = list(map(int,input().split()))
d,f = 0,0
ans = []
for i in range(N):
  if i == 0:
    ans.append(0)
    f = array[0]
    continue
  n = array[i]
  d = f - n
  if d >= 0:
    ans.append(d)
  else:
    ans.append(0)
  if n > f:  
    f = n
print( sum(ans) )

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

for a in A:
    s += max(m-a,0)
    m = max(m, a)
print(s)

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

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

temp = 0
result = 0
for i in range(n):
  if(temp<li[i]):
    temp = li[i]
  else:
    result += temp - li[i]
print(result)

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

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

    for height in A:
        if max_height < height:
            max_height = height
        else:
            count += max_height - height
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    A = list(map(int,input().split()))
    front = 0
    ans = 0
    for a in A:
        if front < a:
            front = a
        else:
            ans += front - a
    print(ans)
    return

if __name__ == '__main__':
    main()

Solution (Python):
N=input()
A=input().split()
ans=0
j=0
for i in A:
    if j>int(i):
        ans+=j-int(i)
    else:
        j=int(i)
print(ans)

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

Solution (Python):

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

m = ans = 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()))
asum = sum(a)
want = 0
prev = 0
for i in range(0,n):
    if i == 0:
        want += a[i]
        prev = a[i]
    else:
        if a[i] > prev:
            prev = a[i]
            want += a[i]
        else:
            want += prev
print(want-asum)

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())
input = [int(s) for s in input().split()]
ans = 0

for i in range(N-1):
    if input[i+1] < input[i]:
        ans = ans+(input[i]-input[i+1])
        input[i+1] = input[i]

print(ans)

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

result = 0
current = A[0]

for i in range(1,N):
    if current <= A[i]:
        current = A[i]
        continue
    else:
        result += current - A[i]

print(result)

Solution (Python):
try:
    k = int(input())
    l = list(map(int,input().split(' ')))
    c = 0
    for i in range(len(l)-1):
      x = l[i+1]-l[i]
      if x<0:
        l[i+1]+=abs(x)
        c+=abs(x)
    print(c)
except:
    pass

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

Solution (Python):

N = int(input())
A = map(int, input().split())
h = []

max_h = 0
for a in A:
    if max_h <= a:
        max_h = a
        h.append(0)
    else:
        h.append(max_h - a)

print(sum(h))

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

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

cnt = 0
for i in range(0,len(A)-1):
  if A[i] > A[i+1]:
    d = (A[i]-A[i+1])
    cnt = cnt + d
    A[i+1] = A[i+1] + d

print(cnt)

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

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


if __name__ == "__main__":
    main()

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

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

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

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

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


def main():
    peopleNum, peopleList = problemInput()

    maxHeight = 0
    totalStepHeight = 0

    for ind in range(peopleNum):
        lookingPerson = peopleList[ind]
        if lookingPerson <= maxHeight:
            totalStepHeight += maxHeight-lookingPerson
        else:
            maxHeight = lookingPerson
    
    print(totalStepHeight)


if __name__ == "__main__":
    main()

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

    mi = A[0]
    s = 0

    for a in A[1:]:
        if a <= mi:
            s += mi - a
        else:
            mi = 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):
    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(" ")))

dai = 0

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

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

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

Solution (Python):
N = int(input().rstrip())
a = [int(v) for v in input().rstrip().split()]
r = 0
pre = a[0]
for i in range(1, N):
	d = a[i] - pre
	if d >= 0:
		pre = a[i]
		pass
	else:
		d2 = abs(d)
		r += d2

print(r)

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

ans=mx=0
for x in a:
    mx=max(mx,x)
    ans+=mx-x

print(ans)

Solution (Python):
num = int(input())
line = input().split(" ")
border = 0
step = 0
for i in line:
  if int(i) >= border:
    border = int(i)
  else:
    step += border - int(i)
print(step)

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

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

print(ans)

Solution (Python):
def solve(n, arr):
    ans_arr = [0]
    for i in range(0, n-1):
        if arr[i] > arr[i + 1]:
            diff = arr[i] - arr[i + 1]
            ans_arr.append(diff)
            arr[i + 1] += diff
        else:
            ans_arr.append(0)
    return sum(ans_arr)

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

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

Solution (Python):
N = int(input())
heights = list(map(int, input().split()))
before = 0
answer = 0

for h in heights:
  if h < before:
    answer += before - h
  else:
    before = h
print(answer)

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

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

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

ans = 0

for i in range(1,len(L)):
    if L[i-1] > L[i]:
        ans += L[i-1] - L[i]
        L[i] = L[i-1]

print(ans)

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

print(ans)

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

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

print(ans)

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

sum = 0

for i in range(len(li)-1):
    if li[i] > li[i+1]:
        sum += li[i] - li[i+1]
        li[i+1] = li[i]

print(sum)

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

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

sum = 0
for n in range(N-1):
	if a[n]>a[n+1]:
		dif=a[n]-a[n+1]
		a[n+1]=a[n+1]+dif
		sum=sum+dif
print(sum)

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

temp = a[0]
count = 0
for i in range(1,n):
    count += max(0,temp-a[i])
    temp = max(temp,a[i])

print(count)

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))
 
h_list = [0]*(n+10)
 
for i in range(1,len(a_list)):
    if a_list[i]  < a_list[i-1] + h_list[i-1]:
        h_list[i] = a_list[i-1] + h_list[i-1] - a_list[i] 
    elif a_list[i] >= a_list[i-1] + h_list[i-1]:
        h_list[i] = 0
        
print(sum(h_list))

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

c=0
ans=0
for i in range(n):
	if c<a[i]:
		c=a[i]
	elif c>=a[i]:
		ans+=c-a[i]
print(ans)

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

Solution (Python):

people_count = int(input())
people = list(map(int, input().split()))

stools, previous = 0, -1
for index in range(people_count):
    if index == 0:
        previous = people[index]
    else:
        current = people[index]

        if previous > current:
            stools += abs(current - previous)
            people[index] = previous
        else:
            previous = current

print(stools)

Solution (Python):
def main():
    _ = input()
    a = list(map(int, input().split()))
    max_height = 0
    ans = 0
    for height in a:
        if max_height <= height:
            max_height = height
        else:
            ans += max_height - height
    print(ans)

if __name__ == "__main__":
    main()

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

Solution (Python):
N=int(input())
A = list(map(int, input().split()))
i = 0
sum = 0
while i<N-1:
  if A[i]<=A[i+1]:
    f=0
  else:
    f=A[i]-A[i+1]
  A[i+1]+=f
  sum+=f
  i+=1
print(sum)

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

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 = [int(x) for x in input().split()]

total = 0
for i in range(1, N):
    d = max(A[i-1] - A[i], 0)
    A[i] += d
    total += d
print(total)

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

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

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

high = 0;
con = 0;

for i in range(n):
    if a[i] > high:
        high = a[i]
    else:
        con += (high - a[i]);
print(con);

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

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

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

Solution (Python):
N=int(input())
M=map(int,input().split())
Mx=0
Total=0
for n in M:
  Mx=max([Mx,n])
  Total+=Mx-n
print (Total)

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

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

ans=0
flag=a[0]

for i in range(len(a)):
    if a[i] < flag:
        ans+=flag-a[i]
    else:
        flag = 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())
heights = list(map(int, input().split()))
steps = [0] * N
for i in range(1, N):
    if heights[i-1] + steps[i-1] > heights[i]:
        steps[i] = (heights[i-1] + steps[i-1]) - heights[i]
    else:
        steps[i] = 0
        
print (sum(steps))

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

dailist = [0]*N
Alist.append(0)
dailist.append(0)
max = 0
for i in range(N):
  if Alist[i]>max:
    max=Alist[i]
    
    
  
  if max>Alist[i+1]:
    dailist[i+1]=max-Alist[i+1]
    



del dailist[-1]
print (sum(dailist))

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

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

total = 0

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

print(total)

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

cnt = 0
step = []
step_sum = 0

for i in range(0, N):
    if i == 0:
        step.append(0)
        continue
    diff = (An[i - 1] + step[i - 1]) - An[i]
    if diff <= 0:
        step.append(0)
    else:
        step.append(diff)

for num in step:
    step_sum += num

print(step_sum)

Solution (Python):
from __future__ import division, print_function






from sys import stdin, stdout
import bisect            
import math
import heapq
i_m=9223372036854775807
def inin():
    return int(input())
def stin():
    return input()
def spin():
    return map(int,stin().split())
def lin():                           
    return list(map(int,stin().split()))
def matrix(n):
    
    return [list(map(int,input().split()))for i in range(n)]


def calculate_sum(a, N): 
    
    m = N / a 
    
    sum = m * (m + 1) / 2
    
    ans = a * sum
    return ans
def series(N):
    return (N*(N+1))//2

def count2Dmatrix(i,list):
    return sum(c.count(i) for c in list)

def modinv(n,p):
    return pow(n,p-2,p)

def GCD(x, y): 
    x=abs(x)
    y=abs(y)
    if(min(x,y)==0):
        return max(x,y)
    while(y): 
        x, y = y, x % y 
    return x
def LCM (x, y):
    return (x * y) // GCD(x, y)
    
def Divisors(n) : 
    l = []  
    for i in range(1, int(math.sqrt(n) + 1)) :
        if (n % i == 0) : 
            if (n // i == i) : 
                l.append(i) 
            else : 
                l.append(i)
                l.append(n//i)
    return l
def isprime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
prime=[]
def SieveOfEratosthenes(n): 
    global prime
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p): 
                prime[i] = False
        p += 1
    f=[]
    for p in range(2, n): 
        if prime[p]: 
            f.append(p)
    return f
q=[]       
def dfs(n,d,v,c):
    global q
    v[n]=1
    x=d[n]
    q.append(n)
    j=c
    for i in x:
        if i not in v:
            f=dfs(i,d,v,c+1)
            j=max(j,f)
            
    return j

 
"""*******************************************************"""
n = inin()
a = lin()


c, d = 0, 0

for i in range(n-1):
    if a[i]>a[i+1]:
        d = a[i] - a[i+1]
        c += d
        a[i+1] = a[i]

print(c)

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

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
import sys
def S(): return sys.stdin.readline().rstrip()
def M(): return map(int,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
A = LI()
ans = 0
max_n = 0
for i in A:
    if i > max_n:
        max_n = i
    else:
        ans = ans + (max_n - i)
print(ans)

Solution (Python):
from sys import exit
n = int(input())
a = list(map(int, input().split()))
if n==1: print(0); exit()
al = [0 for _ in range(n)]
for index in range(n-1):
	person = a[index]
	person_behind = a[index+1]
	if person_behind < person:
		k = person - person_behind
		a[index+1] += k
		al[index+1] = k
print(sum(al))

Solution (Python):
from sys import stdin
readline = stdin.readline
n = int(readline())
a = list(map(int, readline().split()))
x = 0
ans = 0
for i in a:
    if x > i:
        ans += (x-i)
    else:
        x = i
print(ans)

Solution (Python):


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

p = 0
t = 0
for i in range(len(a)):
    if p > a[i]:
        add = p - a[i]
        t += add
        p = a[i] + add
    else:
        p = a[i]

print(t)

Solution (Python):
import math
import sys

def dict_sort(ans):
    ans=sorted(ans.items(),reverse=True,key=lambda kv:(kv[1]))

def is_prime(n):
    if n==1:
        return 0
    for i in range(2,int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True

def seive_primes(n):
    flag=[0]*(n+10)
    flag[1]=flag[0]=1
    i=2
    while i*i<=n+1:
        if flag[i]==0:
            j=i*i
            while j<=n+1:
                flag[j]=1
                j+=i
        i+=1
    return flag

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

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

def alphabates():
    return "abcdefghijklmnopqrstuvwxyz"

def binary_search(ls,n,flag):
    low=0
    hi=n-1
    while(low<=hi):
        mid=(low+hi)//2
        if ls[mid]==flag:
            return mid
        elif ls[mid]>flag:
            hi=mid-1
        else:
            low=mid+1
    return -1

def Binary(n):
    if n==0:
        return "00000000"
    binary = "" 
    i = 0
    while n > 0 and i<=8: 
        s1 = str(int(n%2)) 
        binary = binary + s1 
        n /= 2
        i = i+1
        d = binary[::-1] 
    return d

def qdrt(x,a,b,c):
    return a*x*x+b*x+c

def inp():
    ls=list(map(int,input().split()))
    return ls

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

def knapSack(W, wt, val, n): 
    K = [[0 for x in range(W + 1)] for x in range(n + 1)]  
    for i in range(n + 1): 
        for w in range(W + 1): 
            if i == 0 or w == 0: 
                K[i][w] = 0
            elif wt[i-1] <= w: 
                K[i][w] = max(val[i-1]+K[i-1][w-wt[i-1]],  K[i-1][w]) 
            else: 
                K[i][w] = K[i-1][w] 
  
    return K[n][W] 

n=int(input())
ls=inp()
boro=ls[0]
ans=0
for i in range(1,n):
    if ls[i]<boro:
        ans+=boro-ls[i]
    else:
        boro=ls[i]
print(ans)

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

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

answer = 0
prev = 0
for idx, v in enumerate(li_a):
    if idx == 0:
        prev = v

    if prev > v:
        answer += (prev - v)
    else:
        prev = v
print(answer)

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

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

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