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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
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):
import sys
input = sys.stdin.buffer.readline

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

res = 0

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

print(res)

Solution (Python):
def main():
  x = 0
  n = int(input())
  a = list(map(int,(input().split())))
  for i in range(n-1):
    a1 = a[i:i+1]
    a2 = a[i+1:i+2]

    if a1 > a2:

      sa = a1[0]-a2[0]
      x +=(sa)
      a[i+1]+=sa
  if a[n-2] > a[n-1]:
    x +=a[n-2][0]-a[n-1][0]
  print(x)  

if __name__ == '__main__':
  main()

Solution (Python):


def main():
    p_cnt = int(input())
    all_height = list(map(lambda x: int(x), input().split(' ')))
    max_height = 0
    sum_height = 0

    for i in range(p_cnt):
        if i == 0:
            max_height = all_height[i]
        else:
            if max_height > all_height[i]:
                sum_height += max_height - all_height[i]
            else:
                max_height = all_height[i]

    print(sum_height)

if __name__ == '__main__':
    main()

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

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

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

print(hight)

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

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    continue

  if an >= max_height:
    max_height = an
    continue

  result += (max_height - an)

print(result)

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
S=None
T=A[0]
D=0

for i in range(0,N):
    S=T-A[i]
    if S>=0:
        D=D+S
    else:
        T=A[i]

print(D)

Solution (Python):
n = int(input())
array = input().split()
s = 0
suma = 0
for i in range(len(array)):
    array[i] = int(array[i])

for i in range(len(array)):
    if i >= 1:
        if array[i-1] > array[i]:
            s = array[i - 1] - array[i]
            array[i] += s
            suma += s
print(suma)

Solution (Python):
import numpy as np
from heapq import heapify, heappop
def main():
    N = int(input())
    A = np.array(list(map(int, input().split())))
    Hi = [(-height, i) for i, height in enumerate(A)]

    heapify(Hi)

    count = 0
    current = N
    while True:
        (_h, i) = heappop(Hi)
        if i > current:
            pass
        else:
            height = -_h
            count += np.sum(height - A[i:current])
            current = i
        if current == 0:
            break
    print(count)
if __name__ == '__main__':
    main()

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

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

Solution (Python):




n = input()
s = input()

a = s.split()

max = int(a[0])
total = 0

for i in range(int(n)):
    if max>int(a[i]):
        total += max-int(a[i])
    else:
        max = int(a[i])

print(str(total))

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

sum = 0
key = 0
para = A[0]
for i in range(N):
    if i == 0:
        continue
    else:
        if para > A[i]:
            key = para - A[i]
            sum = sum + key
        else:
            key = 0
            sum = sum + key
            para = A[i]

print(sum)

Solution (Python):
import sys
import numba
import numpy as np
from numba import jit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

@jit
def main(a):
  ans = 0
  for i, j in enumerate(a):
    if i == 0 or j >= a[i-1]:
      continue
    else:
      ans += a[i-1] - j
      a[i] = a[i-1]
  return ans

n= int(readline().strip())
a = np.array(readline().split(), np.int64)
print(main(a))

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

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    max_height_idx = 0
    continue

  if an >= max_height:
    max_height = an
    max_height_idx = i
    continue

  result += (max_height - an)

print(result)

Solution (Python):
N = int(input())
tall = list(map(int, input().split()))
step = [0]*N
sum_step = 0

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

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

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

Solution (Python):
def main() :
	n = int(input().strip())
	num = list(int(j) for j in input().strip().split())
	l = num[0]
	s = 0
	for j in num :
		if j < l : s = s + (l - j)
		if j > l : l = j
	print(s)

if __name__ == '__main__' :
	main()

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

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

print(total)

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

sum = 0
b = a[1:]
x = 0

for i, j in zip(a, b):
    i = i + abs(x)
    x = j - i
    if  x < 0:
        sum += abs(x)
    else:
        x = 0

print(sum)

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

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

for i in range(0,n-1):
    if m>A[i+1]:
        x=x+(m-A[i+1])
    else:
        m=A[i+1]

print(x)

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

a=list(map(int,input().split()))
  
b=[]
if n>1:
  for j in range (n-1):
    c=a[j]-a[j+1]
    if c>-1:
      b.append(c)
      a[j+1]=a[j+1]+c
    else:
      b.append(0)
      
   
  if max(b)>-1:
    print(sum(b))
  else:
    print(0)
    
else:
  print(0)

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

Solution (Python):
import math

if __name__ == "__main__":
    n = int(input())
    A = list(map(int,input().split()))

    zantei = A[0]
    goukei = 0
    for a in A[1:]:
        if zantei>a:
            goukei += zantei - a
        elif zantei<a:
            zantei = a
    print(goukei)

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))
ans = 0
for i in range(1,n):

    if arr[i]<arr[i-1]:
        ans += arr[i-1]-arr[i]
        arr[i] += arr[i-1]-arr[i]
    
print(ans)

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

output = 0

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


print(abs(output))

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N = I()
A = LI()

ans = 0

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

print(ans)

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

ans = 0

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

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
step = [0] * N
for i, a in enumerate(A[1:], 1):
    step[i] = max(0, A[i - 1] + step[i - 1] - a)
print(sum(step))

Solution (Python):
input_n = input()
N = str(input_n)

input_a = input()
A = str(input_a)

listN = A.split(' ')

listA = []

for value in range(len(listN)):
    listA.append(int(listN[value]))

result = 0

for value in range(1, len(listA)):
    if listA[value] < listA[value - 1]:
        result += (int(listA[value - 1]) - int(listA[value]))
        listA[value] += (int(listA[value - 1]) - int(listA[value]))

print(str(result))

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

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

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

B=[]
for i in range(int(N)):
  if i==0:
    continue
  else:
    if A[i-1]>A[i]:
      B.append(A[i-1]-A[i])
      A[i]=A[i]+B[i-1]
    else:
      B.append(0)
      
print(sum(B))

Solution (Python):
N = int(input())
height = list(map(int, input().split()))
total = 0
max_number = height[0]

for i in range(1, height.index(max(height))):
	if height[i] > max_number:
		max_number = height[i]
	else:
		total += (max_number - height[i])

total += (max(height)*(N-1-height.index(max(height)))-sum(height[height.index(max(height))+1:N]))
    
print(total)

Solution (Python):
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))


cnt = 0
B = 0
for i in range(len(A)):
    if B < A[i]:
        B = A[i]
    elif B > A[i]:
        cnt += (B - A[i])

print(cnt)

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

print(res)

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

for i in range(n):
  if num_list[i] < a:
    p = p + (a - num_list[i])
  elif num_list[i] > a:
    a = num_list[i]
  
print(p)

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

if __name__ == '__main__':
    main()

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












max = a_list[0]
count = 0

for a in a_list:
    if max > a:
        d = max - a
        count += d
        a += d
    max = a

print(count)

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

print(c)

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

Solution (Python):

N = int(input())

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

boder = 0
ans = 0


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


print(ans)

Solution (Python):
N = int(input())
x = list(map(int, input().split()))
box = 0
for num in range(N-1):
    if x[num] - x[num + 1] > 0:
        box += x[num] - x[num + 1]
        x[num +1] = x[num]
    else:
        box = box
print(box)

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

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

Solution (Python):
import numpy as np
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)

N = int(input())
A_str = input()

A_lst = A_str.split(" ")
A_lst = [int(A_lst[i]) for i in range(N)]

dai_lst = []

tmp = A_lst[0]

for a in A_lst:
  if a<tmp:
    dai_lst.append(tmp-a)
  elif a>tmp:
    tmp = a

out = sum(dai_lst)
print(out)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
result = 0
for index in range(len(a)):
    diff = 0
    if index != 0:
        diff = a[index-1] - a[index]
        if diff > 0:
            result += diff
            a[index] = a[index-1]

print(result)

Solution (Python):
import sys
def main(lines):
  n = int(lines[0])
  a = []
  hei = lines[1].split()
  for i in range(n):
    a.append(int(hei[i]))
  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)

if __name__ == '__main__':
    lines = []
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    main(lines)

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

Solution (Python):


N = int(input())
heights = [int(x) for x in input().split(" ")]

step = 0 

for i in range(len(heights)-1):

    if(heights[i] >= heights[i+1]):
        diff = heights[i] - heights[i+1]
        heights[i+1] = heights[i+1] + diff
        step += diff

print(step)

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

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

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

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

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

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

cnt = 0
MAX = A[0]
for a in A:
    if a < MAX:
        cnt += MAX - a
    MAX = max(MAX, a)

print(cnt)

Solution (Python):
N=input()
A=list(map(int, input().split()))
taller=0
count=0
for i in A:
    if i>=taller:
        taller=i
    else:
        count+=taller-i

print(count)

Solution (Python):
N=int(input())
M=list(map(int,input().split()))
TotalN=sum(M)
for i in range(len(M)-1):
    if M[i]>M[i+1]:
        M[i+1]=M[i]
TotalM=sum(M)
print(int(TotalM-TotalN))

Solution (Python):
if __name__ == "__main__":
    
    n = int(input())
    a = list(map(int, input().split()))

    target = 0
    ans = 0
    for h in a:
        if target <= h:
            target = h
        else:
            ans += target - h

    print(ans)

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

sup = 0
ans = 0

for i in range(N):
  if sup < int(A[i]):
    sup = int(A[i])
  else:
    ans += sup - int(A[i])

print(ans)

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


tmp=0
spring_high=0
for i in range(N):
  if(i==0):
    tmp=A[0]
  if(tmp<=A[i]):
    
    tmp=A[i]
  else:
   	spring_high+=tmp-A[i]

print(spring_high)

Solution (Python):
n = input()

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

answer = 0
tmp = 0
isFirst = True

for i in lst:
    if isFirst:
        isFirst = False
        tmp = i
    else:
        if tmp > i:
            answer += tmp - i
        else:
            tmp = i
print(answer)

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

Ans = 0
Amax = 0

for i in range(len(A)):
    A[i] = int(A[i])
    if Amax < A[i]:
        Amax = A[i]
    Ans = Ans + (Amax - A[i])

print(Ans)

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

print(sum)

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

ans = 0

for i in range(0,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()))

cnt = len(A)
box = []

for i in range(cnt-1):
    if A[i] < A[i+1]:
        box.append(0)
    else:
        num = A[i]-A[i+1]
        A[i+1] = A[i+1] + num
        box.append(num)

print(sum(box))

Solution (Python):
n = input()
a_lst = list(map(int, input().split()))
check = 0
tmpcnt = 0
anscnt = 0
lens = len(a_lst)
for i in range(lens):
    
    if check > a_lst[i]:
        
        anscnt += check - a_lst[i]
        tmpcnt = check - a_lst[i]

        
        
        a_lst[i] += tmpcnt
    check = a_lst[i]
    
print(anscnt)

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

sum = 0
cur = 0
for i in a:
    if i > cur:
        cur = i
    elif i < cur:
        sum += (cur - i)

print(sum)

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

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

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

Solution (Python):
N=int(input())
height=list(map(int,input().split()))
humidai_list=[0]
total_height=[height[0]]
del height[0]

for i in height:
    if total_height[-1] > i :
        humidai=total_height[-1]-i
        humidai_list.append(humidai)
        total_height.append(i+humidai)
    else:
        total_height.append(i)
print(sum(humidai_list))

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

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

if  all(a[k] >= b[k] for k in range(n + 1)):
    print(0)
    exit()

s = [0] * n
for i in range(1, n):
    if a[i - 1] - a[i] >= 1:
        s[i] = a[i - 1] - a[i]
        a[i] = a[i - 1]
print(sum(s))

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

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

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

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

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

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

ans = 0

for i in range(n-1):
  if a[i+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()))
hsum = 0
for i in range(n-1):
    if a[i] > a[i+1]:
        hsum += a[i]-a[i+1]
        a[i+1] = a[i]
print(hsum)

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

prevHeight = 0
fumidaiSum = 0
for aa in A:
  if aa<prevHeight:
    fumidaiSum += prevHeight - aa
  prevHeight = max(aa, prevHeight)
print(fumidaiSum)

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




def main():
	n=II()
	A=LI()

	now=0
	ans=0
	for i in A:
		if i<=now:
			ans+=now-i
		else:
			now=i
	print(ans)




if __name__ == "__main__":
	main()

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

tmp = 0
sums = 0
for h in a:
    if h < tmp:
        sums += tmp - h
    else:
        tmp = h
print(sums)

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

player_input2 = input()

lst_of_heights = player_input2.split(" ")

tallest = int(lst_of_heights[0])

step_value = 0



for i in range(1, ammount):
    if int(lst_of_heights[i]) >= tallest:
        tallest = int(lst_of_heights[i])
    else:

        step_value += (tallest - int(lst_of_heights[i]))

print(step_value)

Solution (Python):
N=int(input())
A_list=list(map(int,input().split()))
diff=0
step=[]
for a in range(1,len(A_list)):
    diff=A_list[a]-A_list[a-1]
    if diff<0:
        A_list[a]=A_list[a]-diff
        step.append(-diff)

print(sum(step))

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


S = A[0]
sum = 0

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

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]:

        
        dai=A[i-1]-A[i]


        ans+=dai

        A[i] += dai

    

print(ans)

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

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

Solution (Python):
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

A = np.array(read().split(), np.int64)[1:]

B = np.maximum.accumulate(A)
print(np.sum(B - A))

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

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

height = 0
current_max = A[0]
for i in range(N-1):
    required_height = max(0, A[i] - A[i+1])
    A[i + 1] += required_height
    height += required_height

print(height)

Solution (Python):
def run(x, a_list):
    '''

    '''
    if x == 1:
        print(0)
    else:
        out = 0
        prev = 0
        for i,v in enumerate(a_list):
            if i == 0:
                continue
            prev_height = prev + a_list[i-1]
            if v > prev_height:
                out += 0
                prev = 0
            else:
                out += prev_height - v
                prev = prev_height - v
            
        print(out)
            


    



if __name__ == '__main__':
    x = int(input())
    a_list = list(map(int, input().split()))
    run(x, a_list)

Solution (Python):
n = int(input())
al = list(map(int, input().split()))
r = range(len(al)-1)

c = 0
for i in r:
    a_prev = al[i]
    a_next = al[i+1]
    if a_prev > a_next:
        diff = a_prev - a_next
        c += a_prev - a_next
        al[i+1] += diff

print(c)

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

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

Solution (Python):
n = input("")
a = input("").split()
mae_height = 0
ans = 0
for height in a:
    height = int(height)
    if mae_height > height:
        ans += mae_height - height
        mae_height = height + (mae_height - height)
    else:
        mae_height = height
print(ans)

Solution (Python):
n = int(input())
num = [int(x)for x in input().split()]
j = 0
k = 0
for i in num:
    if j == 0:j = i
    if j > i:
        k += j-i
    elif j < i:
        j = i

print(k)

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

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

for i in range(1,n):
  if a[i-1] > a[i]:
    Sum.append(a[i-1] - a[i])
    a[i] = a[i-1]
    
print(sum(Sum))

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

A_list = list(map(float, A))
sa = 0
for i in range(N-1):
    
    if A_list[i] > A_list[i+1]:
        sa += A_list[i] - A_list[i+1]
        A_list[i+1] = A_list[i]
print(int(sa))

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

pre = A[0]

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

print(FH)

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

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

stair = 0

for i in range(N):
    if i != 0:
        if A[i] < A[i-1]:
            stair = stair + A[i-1] - A[i]
            A[i] = A[i-1]
            

print(stair)

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

h_max = 0
step_h = 0
for i in range(n):
  if a[i] < h_max:
    step_h += h_max - a[i]
  if a[i] > h_max:
    h_max = a[i]

print(step_h)

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

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

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

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

max_value = max(A)
max_index = A.index(max_value)

c=0

for i in range(max_index,n):
    c+=(max_value-A[i])

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

print(c)

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

tmp = [0]*N

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

ans = sum(tmp)

print(ans)

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

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

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

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

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

num = 0
for i in range(len(a_list) - 1):
    if a_list[i] > a_list[i+1]:
        num += a_list[i] - a_list[i+1]
        a_list[i+1] = a_list[i]
    else:
        continue

print(num)

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

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

print(count)

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

Solution (Python):
input()
a_list = [int(x) for x in input().split()]
n = len(a_list)
i=1
mae_height = a_list[0]
fumidai_sum = 0
sa = 0
while i<n:
  if a_list[i] < mae_height:
    sa = mae_height - a_list[i]
    fumidai_sum += sa
    mae_height = a_list[i] + sa
  elif a_list[i] >= mae_height:
    mae_height = a_list[i]
  i+=1
print(fumidai_sum)

Solution (Python):
from sys import stdin, stdout
def gc(): return map(int, stdin.readline().strip().split())
def gar(): return list(map(int, stdin.readline().strip().split()))
def gs(): return stdin.readline().strip()
n = int(stdin.readline())
ar = gar()
ans = 0
p = ar[0]
for i in range(n):
    if ar[i] <= p:
        ans += p-ar[i]
        ar[i] = p
    else:
        p = ar[i]
print(ans)

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

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

print(sum)

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

Solution (Python):
length = input()
lst = input()
lst = lst.split()
q = 0
for i in range(len(lst)):
    lst[i] = int(lst[i])
for i in range(int(length)-1):
    if lst[i] >= lst[i+1]:
        q += (lst[i]-lst[i+1])
        lst[i + 1]= lst[i]
print(q)

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

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

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

len = len(A)
i = 1
dai = 0

while(i < len):
    if(A[i-1] > A[i]):
        dai += (A[i-1] - A[i])
        A[i] = A[i-1]
    i += 1

print(dai)

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

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