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

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

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

prev_height = A_list[0]
result = 0

for i in range(1, N):
    cur_height = A_list[i]
    if prev_height > cur_height:
        result += prev_height - cur_height
    else:
        prev_height = cur_height

print(result)

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

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

dai = 0

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

        
    else:
        continue

print(dai)

Solution (Python):
def II(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

N=II()
A=LI()
L=[0]*N
TM=A[0]
for i in range(N):
  TM=max(TM,A[i])
  L[i]=TM-A[i]
  
  
ans=sum(L)
print(ans)

Solution (Python):
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
a = lmiinps()

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 = input()
a = list(map(int,input().split()))
m = a[0]
ans = 0

for i in a:
  if i <=m:
    ans += m-i
  else:
    m = i

print(ans)

Solution (Python):

n =  int(input())
a = [int(x) for x in 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()))

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

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

Solution (Python):
from collections import defaultdict, deque
import sys, heapq, bisect, math, itertools, string, queue, copy, time
from fractions import gcd
import numpy as np
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
EPS = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

n = int(input())
a_list = np.array(list(map(int, input().split())))

a_mx = np.maximum.accumulate(a_list)

ans = (a_mx - a_list).sum()

print(ans)

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

h = 0
r = 0
for v in A:
	need = 0 if v >= h else h-v
	r += need
	h = need + v
print(r)

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

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

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

steps = 0
steps_list = []
for i in range(1,N):
	if A[i] < A[i-1]:
		steps_list.append(A[i-1] - A[i])
		A[i] += A[i-1] - A[i]
print(sum(steps_list))

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

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

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

Solution (Python):
a=input()
b=input()
c=b.split()
d=map(int,c)
main=list(d)
sum=0
for i in range(len(main)-2):
    stool=main[i]-main[i+1]
    if stool>0:
        main[i+1]+=stool
        sum+=stool

stool=main[len(main)-2]-main[len(main)-1]
if stool>0:
    sum+=stool
    main[len(main)-1]+=stool

print(sum)

Solution (Python):

N = int(input())
As = list(map(int, input().split()))
ans = 0

tall = int(As[0])

for A in As:
	if(tall > A):
		ans += tall - A
	else:
		tall = A

print(ans)

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

print(ans)

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

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

i = 0
step = []
while i <= len(A) - 2:
    if A[i] > A[i+1]:
        step.append(A[i] - A[i+1])
        A[i+1] = A[i]
        i += 1
    else:
        i += 1

print(sum(step))

Solution (Python):
from sys import stdin
input = stdin.readline
N = int(input())
A = list(map(int, input().split()))

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

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

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

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

t=a[0]
total=0

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

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

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

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

    print('{}'.format(ans))

if __name__ == '__main__':
    main()

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

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

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

print(x)

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

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

print(ans)

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

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

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

toki=a[0]
shigi=0

for i in range(1,n):
  if toki>a[i]:
    shigi += toki-a[i]
  else:toki=a[i]

print(shigi)

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

for x in range(1 ,N):
    if(height[x] >= height[x-1] + box[x-1]):
        box.append(0)
    if(height[x] < height[x-1] + box[x-1]):
        box.append(- height[x] + height[x-1] + box[x-1])

sum = 0
for t in box:
    sum += t
print(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())
l = list(map(int,input().split()))

h = 0
sum = 0
for i in range(n):
    if l[i] <= h:
        sum += h - l[i]
    else:
        h = l[i]

print(sum)

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


main()

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

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

ans = 0
max = 0

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

print(ans)

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

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

tmp = 0
sum = 0
for i in a:
    if tmp > i:
        dif = tmp - i
        sum += dif
    else:
        tmp = i

print(sum)

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

hight_sum = 0
for i in range(n-1):
    hight = 0
    diff = a[i+1] - a[i]
    if diff < 0:
        hight -= diff
        a[i+1] -= diff
        hight_sum += hight
print(hight_sum)

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

print(res)

Solution (Python):

N = int(input())
A=list(map(int,input().split()))
dansa = []
top = 0

for i in range(N):
    dansa.append(max(0,top-A[i]))
    top = max(top,A[i])

print(sum(dansa))

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

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

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

    for a in A:
        if a > s:
            s = a
        else:
            ans += s - a

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


from itertools import accumulate
import operator

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

b = accumulate(a, max)
s = sum(map(operator.sub, b, a))
print(s)

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

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n = int(input())
    A = list(map(int,input().split()))
    high = 0
    ans = 0
    for a in A:
        if high > a:
            ans += high - a
        elif high < a:
            high = a
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n = int(input())
    A = list(map(int,input().split()))
    high = 0
    ans = 0
    for a in A:
        if high > a:
            ans += high - a
        elif high < a:
            high = a
    print(ans)

if __name__=='__main__':
    main()

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

count = 0
temp = a[0]

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
base = 0
diff = 0
for i in range(N):
    if A[i] > base:
        base = A[i]
    diff += base - A[i]
print(diff)

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

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

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

print(ret)

Solution (Python):

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

max_num = 0
sum_num = 0
for i in A:
    max_num =  max(i, max_num)
    calcu_num = max_num
    sum_num += (calcu_num - i)

print(sum_num)

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

a=0
result=0

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

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

MAX = 0
ANS = 0

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

print(ANS)

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

standard = height[0]
ans = 0
for i in range(n):
  if height[i] > standard:
    standard = height[i]
  elif height[i] < standard:
    ans += standard - height[i]

print(ans)

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

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

Solution (Python):

def input_array():
	return list(map(int,input().split()))
  
n=int(input())
A=input_array()

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

print(ans)

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

height = 1
step = 0

for i in range(N):
  if As[i] < height:
    step += height - As[i]
  else:
    height = As[i]
    
print(step)

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

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

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

print(ans)

Solution (Python):
from sys import stdin
import math
inp = lambda : stdin.readline().strip()

n = int(inp())
a = [int(x) for x in inp().split()]
ans = 0
for i in range(1, n):
	if a[i] < a[i-1]:
		ans += a[i-1] - a[i]
		a[i] += a[i-1] - a[i]
print(ans)

Solution (Python):
from sys import stdin
from math import ceil
inp = lambda : stdin.readline().strip()

n = int(inp())
a = [int(x) for x in inp().split()]

curr = 0
ans = 0
for i in a:
    curr = max(curr, i)
    if i < curr:
        ans += curr - i
print(ans)

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

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

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

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

ans = 0

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


print(ans)

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

max_tmp = 0
ans = 0

for i in range(n):
    max_tmp = max(max_tmp, lst[i])
    ans = ans + (max_tmp - lst[i])

print(ans)

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

h = 0
ans = 0

for i in range(N):
    h = max(h, As[i])
    ans += h - As[i]

print(ans)

Solution (Python):
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') 
inf = float('inf') ;mod = 10**9+7 
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

Solution (Python):
N = int(input())
nums = list(map(int, input().split(' ')))
mx = float('-inf')
ans = 0

for val in nums:
    if val < mx:
        ans += mx - val
    
    mx = max(val, mx)

print(ans)

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

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

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

for x in A:
    if x < maxBefore:
        steps += maxBefore - x
    else:
        maxBefore = x
print(steps)

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

r = 0
m = 0
for i in a:
    if m <= i:
        m = i
    else:
        r += m-i
print(r)

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

print(sum(A)-B)

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

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

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

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

for j in range(N):
    sum += A[j]-Ad[j]

print(sum)

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

Solution (Python):
def func(A):
  result = 0
  min = 0
  for a in A:
    if a < min:
      result += min - a
    else:
      min = a
  return result

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

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

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

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

print(ans)

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

Solution (Python):
def resolve():
    N = input()
    heights = [int(x) for x in input().split(" ")]

    max_heights = 0
    s = 0

    for h in heights:
        if h < max_heights:
            s += max_heights - h
        else:
            max_heights = h
    print(s)
    
resolve()

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


print(h)

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

a =[]
for i in range(1, N):
  if A[i-1] -A[i] > 0:
    add = A[i-1] -A[i]
    a.append(add)
    A[i] += add
print(sum(a))

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

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

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

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

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

ma = vals[0]
res = [0 for _ in range(n)]
for i, val in enumerate(vals):
    if val < ma: 
        res[i] = ma - val
    if ma < val:
        ma = val

print(sum(res))

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

M = 0
S = 0

for a in A:
    if M < a:
        M = a
    if a < M:
        S += M - a

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

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

print(sum(ans))

Solution (Python):
n = int(input())
total = 0
a = [int(v) for v in input().split()]
highest = a[0]
for i in range(1, len(a)):
    first = a[i - 1]
    second = a[i]
    if second < first:
        diff = first - second
        a[i] = second + diff
        total += diff
print(total)

Solution (Python):
def sol(lis):
    m = lis[0]
    ans = []
    for i in range(len(lis)):
        if lis[i]<m:
            ans.append(m-lis[i])
            lis[i] = m
        elif  lis[i]!=m:
            m = max(m,lis[i])
    print(sum(ans))
input()
lis = list(map(int,input().split(' ')))
sol(lis)

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

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

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

Solution (Python):
import sys

n=int(input())
li=[int(x) for x in input().split()]

h=0
k=0

for i in range(n):
  if h<=li[i]:
    h=max(h,li[i])
  else:
    k=k+(h-li[i])
    h=max(h,li[i])
    
print(k)

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

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

needs = 0
thred = int(strings[0])
for s in strings[1:N]:
  now = int(s)
  if now < thred:
    needs = needs + thred - now
  else:
    thred = now
    
print(needs)

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

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


if __name__ == "__main__":
    main()

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

print(sum_step)

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

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

print(ans)

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

ans = 0
for i in range(len(l)-1):
    if l[i] <= l[i+1]:
        pass
    else:
        ans += l[i] - l[i+1] 
        l[i+1] += l[i] - l[i+1] 
print(ans)

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

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

Solution (Python):
import math
from itertools import combinations
from itertools import combinations_with_replacement
from itertools import product
from collections import defaultdict
from collections import deque
from collections import Counter
import heapq


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


N = int(input())
A_LI = input_li()
max_h = A_LI[0]
ans = 0
for i in range(1, N):
    if A_LI[i] < max_h:
        ans += max_h - A_LI[i]
    else:
        max_h = max(max_h, A_LI[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]:
        diff    = A[i] - A[i+1]
        ans    += diff
        A[i+1] += diff

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

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
answer = 0
prev = 0
for a in A:
    step = max(0, prev - a)
    answer += step
    prev = a + step
print(answer)

Solution (Python):
'''
Problem Link - https://atcoder.jp/contests/abc176/tasks/abc176_c
'''
from sys import stdin,stdout
from math import *
from collections import *

mod = 1000000007

def gcd(a,b):
	while b:
		a,b = b,a%b
	return a

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

def set_bits(X):
	c = 0
	while X:
		X &= (X-1);c += 1
	return c

def compute_MOD(N,M): return (N%M + M) % M

def get_array(): return list(map(int, stdin.readline().split()))
 
def get_ints(): return map(int, stdin.readline().split())

def get_int(): return int(stdin.readline())

def get_input(): return stdin.readline().strip()

def main():
    tc = get_int()
    arr = get_array()
    ans = 0
    Max = arr[0]
    for i in arr[1:]:
    	Max = max(Max,i)
    	ans += Max-i
    stdout.write(str(ans)+'\n')
if __name__ == "__main__":
    main()

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

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

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


for i in range(n-1):
    if a[i] >= a[i+1]:
        x = a[i] - a[i+1]
        total += x
        a[i+1] = a[i]
        i = i+1
    else:
        total += 0

print(total)

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



stool = A[0]
ans = 0
for i, h in enumerate(A):
    if h < stool:
        ans += stool - h
    stool = max(stool, h)

print(ans)

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

Solution (Python):


N = int(input().split()[0])
a_list = list(map(int, input().split()))
before_a = 0
total = 0
for i, a in enumerate(a_list):
    if i == 0:
        before_a = a
        continue
    total += max(before_a - a, 0)
    before_a = a + max(before_a - a, 0)
ans = total
print(ans)

Solution (Python):

import numpy as np

N = int(input())
A = np.array([int(a) for a in input().split()])
Amax = np.maximum.accumulate(A)

print(sum(Amax-A))

Solution (Python):


N = int(input())
A = list(int(a) for a in input().split())

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

print(ans)

Solution (Python):
n = int(input())
s = list(map(int, input().strip().split()))
befmax=0
sumlong=0
for i in range(0,n):
    if(befmax>s[i]):
        sumlong += befmax-s[i]
    else:
        befmax=s[i]

print(sumlong)

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

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

    current = a_list[0]
    answer = 0
    for a in a_list:
        if a < current:
            answer += current - a
        current = max(a, current)
    print(answer)


if __name__ == '__main__':
    main()