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
diff = 0

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

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

ans_list = [0]
for i in range(1, N):
    d = a_list[i-1] + ans_list[i-1] - a_list[i]
    if d > 0:
        ans_list.append(d)
    else:
        ans_list.append(0)
print(sum(ans_list))

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
s = 0
if N==1:
  print(0)
  exit()

for i,a in enumerate(A):
  if i==0:
    continue
  
  tmp = A[i-1] - a
  if tmp>0:
    A[i] = A[i-1]
    s += tmp

print(s)

Solution (Python):
N=input()
list=[int(x) for x in input().split()]
i=0
kei=0
k=0
while(i!=len(list)-1):
    if(list[i]>list[i+1]):
        kei+=list[i]-list[i+1]
        list[i+1]=list[i]
    i+=1
print(kei)

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

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

Solution (Python):




import sys

readline = sys.stdin.buffer.readline



iimr = lambda: map(int, sys.stdin.readline().rstrip().split())


def debug(*x):
    print(*x, file=sys.stderr)


class atcoder():
    def __init__(self):
        pass

    def çözmek(self):
        N = int(input())
        A = list(map(int, sys.stdin.readline().split()))
        res = []
        res.append(self.関数(N, A))
        print(*res, "")	
    
    def 関数(self, n, Aliste):
        res = 0
        for i in range(1, n):
            if Aliste[i - 1] > Aliste[i]:
                res += Aliste[i - 1] - Aliste[i]
                Aliste[i] = Aliste[i - 1]

        return res

if __name__ == "__main__":
    ima = atcoder()
    ima.çözmek()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

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

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

list_ans = []

for j in range(1,n):
    stepping_stone = a[j-1] - a[j]
    if stepping_stone > 0:
        list_ans.append(stepping_stone)
        a[j] += stepping_stone
    else:
        list_ans.append(0)

ans = sum(list_ans)
print(ans)

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

maximum=A[0]
dai=0
for a in A:
    if a<maximum:
        dai+=maximum-a
    if a>maximum:
        maximum=a

print(dai)

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

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
B=A
M=max(A)
k=0
Fumi=0

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

print(Fumi)

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

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

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

for i in range(N-1):
    if A[i] > A[i+1]:
        youso = A[i] - A[i+1]
        A[i+1] += youso
        ans.append(youso)
    else:
        pass

print(sum(ans))

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

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

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

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

Sum = 0

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

print(Sum)

Solution (Python):

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline


def array(size, init=0):
    return [[init for j in range(size[1])] for i in range(size[0])]


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


def II():
    return int(input())


N = II()
a = acinput()

x = 0

h = a[0]
for i in range(1, N):
    x += max(0, h-a[i])
    h = max(h, a[i])

print(x)

Solution (Python):
num = int(input()) - 1
list = list(map(int,input().split()))
len = len(list) 
total = 0
i = 0

while i < num:
    if list[i] > list[i+1]:
        total= total + (list[i+1] -list[i])
        list[i+1] = list[i]
        i = i + 1
    else:
        i = i + 1
        
print(abs(total))

Solution (Python):

n = int(input())

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

p = 0

q = 0

for i in range(n-1):
  
  e = a[i+1]-a[i]
  if e > 0:
    if e > q:
      q = 0
    else:
      q = q-e
  else:
    q = q-e
  p += q
  
print(p)

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

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

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

for i in range(1, N):
  if s[i] < s[i-1]:
    h += s[i-1] - s[i]
    s[i] = s[i-1]
  else:
    continue

print(h)

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

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

tmp = 0
ans = 0

for i in a:
    if tmp > i:
        ans += tmp - i
    tmp = max(i, tmp)

print(ans)

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

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

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


if __name__ == "__main__":
    solve()

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

Solution (Python):
def main():

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

    sum = 0

    max_value = 0

    for i in range(0, len(heights)):
        if max_value < heights[i]:
            max_value = heights[i]
        else:
            sum = sum + max_value - heights[i]


    print(sum)
main()

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

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

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

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

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

import numpy as np

B_list = A_list.copy()

for i in range(1, len(A_list)):
    if A_list[i] < A_list[i-1]:
        A_list[i] = A_list[i-1]
        
A_array = np.array(A_list)
B_array = np.array(B_list)

print(np.sum(A_array - B_array))

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

Solution (Python):
N = int(input())
members = list(map(int, input().strip().split()))
stands = []
stands.append(0)

for i, member in enumerate(members):
  if i>0:
    if members[i] > members[i-1] + stands[i-1]:
      stands.append(0)
    else:
      stands.append(members[i-1] + stands[i-1] - members[i])
      
print(sum(stands))

Solution (Python):

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

high = [0]*n
for i in range(1,len(a)):
    if a[i] <= a[i-1]:
        dai = a[i-1] - a[i]
        a[i] += dai
        high[i] = dai


print(sum(high))

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

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

print(sum)

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

max=0
total=0

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

print(total)

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

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

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

max = 0
step = 0

for a in h:
    if a > max:
        max = a
    if a < max:
        step += max - a

print(step)

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

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

    for i in range(1, len(a)):
        if i == 1:
            most_h = max(a[i - 1], a[i])
            if a[i - 1] > a[i]:
                ans += a[i - 1] - a[i]
        else:
            if most_h > a[i]:
                ans += most_h - a[i]

        most_h = max(most_h, a[i])

    print(ans)

if __name__ == '__main__':
    abc176_c()

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

f = l[0]
t = l[1:]
s = 0
max = f
for i in t:
    diff = max - i
    if diff > 0:
        s += diff
        
    if(max < i):
        max = i 
        
print(s)

Solution (Python):
"""
自分が前より小さいとき前の人に合わせる
大きいときそのまま
"""
n = int(input())
a = list(map(int, input().split()))
cnt = 0
m = int(a[0])
for i in range(1, n):
    if m > int(a[i]):
        cnt += m - int(a[i])
    else:
        m = int(a[i])
print(cnt)

Solution (Python):
N = int(input())
data = [int(i) for i in input().split()]
MAX = data[0]
res = 0
for i in range(1, len(data)):
    if data[i] > MAX:
        MAX = data[i]
    else:
        res += (MAX - data[i])

print(res)

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

t=0
x=0
z=a[0]

while t<n:
  if a[t]<z:
    x=x+(z-a[t])
  else:  
    z=a[t]
  t=t+1
print(x)

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

print(total)

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

    c += 1

print(r)

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

keep = 0
cnt =0
x=0


for a in A:
    if keep > a:
        cnt += keep -a
        x = keep -a
        keep = a + x
    else:
      keep = a
    
    
print(cnt)

Solution (Python):

"""
Created on Sat Aug 22 21:53:54 2020

@author: naoki
"""

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

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

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

total = 0
amax = A[0]

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

print(total)

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

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

total = 0
ext = 0
prev = an[0]

for a in an:
    if prev+ext > a:
        ext = prev+ext - a
        total += ext
    else:
        ext=0
    prev = a

print(total)

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

for i in range(len(A)-1):
    if A[i] >= A[i+1]:
        gap = 0
        gap = A[i]-A[i+1]
        A[i+1] = A[i+1] + gap
        sum_gap += gap
print(sum_gap)

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

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

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

F = 0
H = A[0]

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

print(F)

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

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

print(sum(stand))

Solution (Python):
a = [input().split() for l in range(2)]
count = 0
N = int(a[0][0])
A = a[1]
ans = 0
for i in range(N-1):
    if int(A[i+1]) < int(A[i]):
        ans = ans + int(A[i]) - int(A[i+1])
        A[i+1] = A[i]
print(ans)

Solution (Python):
n = input()
m = input().split()
m = [int(i) for i in m]
mx = max(m)
ind = m.index(mx)
 
c = 0
j = len(m)
for i in reversed(m[ind:]):
    if i < mx:
        d = mx - i
        c += d
        
ind_i = 0
for i in m[1:ind]:
    
    if m[ind_i] > i:
        num = m[ind_i] - i
        m[ind_i+1] += num
        c+= num
    
    ind_i += 1
    
print(c)

Solution (Python):
num = input()
height = list(map(int,input().split()))
count = 0

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

print(count)

Solution (Python):
N = map(int, input().split())
l = list(map(int, input().split()))
from collections import deque


l = deque(l)

count=0
for i in range(len(l)-1):
    if l[i] >l[i+1]:
        c=l[i] -l[i+1]
        l[i+1]+=c
        count+=c

print(count)

Solution (Python):
import copy
N = input()
A = []
for val in map(int, input().split()):
    A.append(int(val))


def asd(index):
    x = A[index]
    y = A[index+1]
    inheight = 0
    
    
    if( x > y) :
        
        inheight +=  x - y
        A[index+1] += inheight
    return inheight

height = 0
for index in range(len(A) - 1):
    
    height += asd(index)
print(height)

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

x = 0
y = l[0]
for i in range(1, n):
    z = l[i] - y
    if z >= 0:
        y = l[i]
        x += 0
    else:
        x += abs(z)

print(x)

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

std = 0
base = int(A[0])
ans = 0

for n in range(1,N):
    std = int(A[n]-1)

    if std < base:
        ans += A[n-1] - A[n]
        std += base - std
        A[n] = std

    base = A[n]

print(ans)

Solution (Python):
n = int(input())
aN = list(map(int, input().split()))
sum_board = 0
aN_max = aN[0]
for i in range(1,n):
    if aN[i] < aN_max:
        sum_board +=  aN_max - aN[i]
    else:
        aN_max = aN[i]

print(sum_board)

Solution (Python):
n = int(input())
an = list(map(int, input().split()))
mx = -1
ht = 0

for i in range(n):
  mx = max(mx,an[i])
  if mx - an[i] > 0:
    ht += mx - an[i]
print(ht)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


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

A.insert(0, A[0])

stand_h = 0

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

print(stand_h)

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

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

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

Solution (Python):
from sys import stdin, setrecursionlimit,stdout
from collections import deque, defaultdict,Counter
from math import log2, log, ceil,sqrt
from operator import *
input = stdin.readline
setrecursionlimit(int(2e5))
def getstr(): return input()[:-1]
def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint1(): return list(map(lambda x : int(x) - 1, input().split()))
jn = lambda x,l: x.join(map(str,l))

def swaparr(arr, a,b):
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp
 

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

def nCr(n, k): 
    if(k > n - k): 
        k = n - k 
    res = 1
    for i in range(k): 
        res = res * (n - i) 
        res = res / (i + 1) 
    return res 
 

def upper_bound(a, x, lo=0):
    hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x:
            lo = mid+1
        else:
            hi = mid
    return lo
 



def maxSlidingWindow(nums,k):
    windowSum,maxSum = 0,0
    windowSum = sum(nums[:k])
    for end in range(k,len(nums)):
        windowSum += nums[end]-nums[end - k]
        maxSum = max(maxSum,windowSum)
    return maxSum



def twoSum(nums, target):
        left = 0
        right = len(nums)-1
        while left <= right:
            if nums[left]+nums[right] == target:
                return [left,right]
            elif nums[left]+nums[right] < target:
                left +=  1
            else:
                right -= 1
        return 



def prefix(arr):
    a = map(add,arr.insert(0,0))
    return a











def primefs(n):
    
    primes = {}
    while(n%2 == 0):
        primes[2] = primes.get(2, 0) + 1
        n = n//2
    for i in range(3, int(n**0.5)+2, 2):
        while(n%i == 0):
            primes[i] = primes.get(i, 0) + 1
            n = n//i
    if n > 2:
        primes[n] = primes.get(n, 0) + 1
    
    
    return primes
 

def power(x, y, p): 
    res = 1
    x = x % p  
    if (x == 0) : 
        return 0
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
        y = y >> 1      
        x = (x * x) % p 
    return res 
 

def swap(a,b):
    temp = a
    a = b
    b = temp
    return a,b
 






 

def find(x, link):
    p = x;
    while( p != link[p]):
        p = link[p];
    
    while( x != p):
        nex = link[x];
        link[x] = p;
        x = nex;
    return p;
 
 


def union(x, y, link, size):
    x = find(x, link)
    y = find(y, link)
    if size[x] < size[y]:
        x,y = swap(x,y)
    if x != y:
        size[x] += size[y]
        link[y] = x
 

def sieve(n): 
    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
    return prime



def binsearch(a, l, r, x):
    while l <= r:
        mid = l + (r-1)//2
        if a[mid]:
            return mid
        elif a[mid] > x:
            l = mid-1
        else:
            r = mid+1
    return -1






graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000
 
 
def bfs(d, v):
    q = []
    q.append(v)
    visited[v] = 1
    while len(q) != 0:
        x = q[0]
        q.pop(0)
        for i in d[x]:
            if visited[i] != 1:
                visited[i] = 1
                q.append(i)
        print(x)
 
 
def make_graph(e):
    d = {}
    for i in range(e):
        x, y = mi()
        if x not in d:
            d[x] = [y]
        else:
            d[x].append(y)
        if y not in d:
            d[y] = [x]
        else:
            d[y].append(x)
    return d
 
 
def gr2(n):
    d = defaultdict(list)
    for i in range(n):
        x, y = mi()
        d[x].append(y)
    return d
 
 
def connected_components(graph):
    seen = set()
 
    def dfs(v):
        vs = set([v])
        component = []
        while vs:
            v = vs.pop()
            seen.add(v)
            vs |= set(graph[v]) - seen
            component.append(v)
        return component
 
    ans = []
    for v in graph:
        if v not in seen:
            d = dfs(v)
            ans.append(d)
    return ans


 

MAXN = int(1e6 + 5)
def spf_sieve():
    spf[1] = 1;
    for i in range(2, MAXN):
        spf[i] = i;
    for i in range(4, MAXN, 2):
        spf[i] = 2;
    for i in range(3, ceil(MAXN ** 0.5), 2):
        if spf[i] == i:
            for j in range(i*i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i;
    
 

spf = [0 for i in range(MAXN)] 
spf_sieve() 
def factoriazation(x):
    ret = {};
    while x != 1:
        ret[spf[x]] = ret.get(spf[x], 0) + 1;
        x = x//spf[x]
    return ret
    
    
 




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

MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');
y, n = "Yes", "No"
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12,
       'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24,
       'z': 25}






def solve():
    x = getints()
    count = 0
    for i in range(1,len(x)):
        if x[i-1]<= x[i]:
            count += 0
        else:
            count+=(x[i-1]-x[i])
            x[i] = x[i-1]
    print(count)
if __name__ == "__main__":
    
    
    
    
    getint()
    solve()

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

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

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

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

print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
u=max(l)
d=0
i=0
while l[i]!=u:
  if l[i]>l[i+1]:
    d=d+abs(l[i+1]-l[i])
    l[i+1]=l[i] 
  i=i+1
r=i
for b in range(r,n):
    d=d+(u-l[b])
print(d)

Solution (Python):
def resolve():
	N = int(input())
	H = 0
	res = 0
	for A in input().split():
		A = int(A)
		res += max(0, H - A)
		H = max(H, A)
	print(res)
resolve()

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
 
sum_humidai = 0
max_front_person = 0
  
for person_idx, person_height in enumerate(As):
  
  if person_idx == 0:
    continue
    
  max_front_person = max(max_front_person, As[person_idx - 1])
  if max_front_person > person_height:
    humidai = max_front_person - person_height
    sum_humidai += humidai
 
print(sum_humidai)

Solution (Python):
a=[input() for i in range(2)]
a1=int(a[0])
a2=[int(i) for i in a[1].split()]

n=0

for i in range(len(a2)-1):
	sa=a2[i]-a2[i+1]
	if sa>0:
		a2[i+1]+=sa
		n+=sa

print(n)

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

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

print(ans)

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

rem = A[0]
count = 0
for i in range(1,n):
  if rem >= A[i]:
    sa = rem - A[i]
    A[i] += sa
    count += sa
  else:
    rem = A[i]
    
print(count)

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

print(sum)

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

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

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

Amax = 0
FD = 0

for Ai in AL:

  if Amax > Ai:

    FD = FD + (Amax - Ai)
  elif Amax < Ai:

    Amax = Ai

print(FD)

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

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 = map(int, input().split())
l = list(a)
nl = [0]*n

for i in range(n-1):
    if l[i] > l[i+1]:
        nl[i+1] += l[i] - l[i+1]
        l[i+1] = l[i]
    else:
        nl[i+1] += 0
print(sum(nl))

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

ans = 0
for i in range(N):
  if i == 0:
    tmp = A[i]
  else:
    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())) 
klists = []
B = [A[0]]
for i in range(N-1):
    if B[i] > A[i+1]:
        k = B[i]-A[i+1]
        klists.append(k)
        A[i+1] = A[i+1] + k
    B.append(A[i+1])
        
print(sum(klists))

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

Solution (Python):
ｎ = int(input())
a = list(map(int,input().split()))
cnt = 0
mx = a[0]

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

print(cnt)

Solution (Python):

_INPUT = """\
5
3 3 3 3 3
"""

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    Springboard = []
    max = A[0]
    for i in range(1,len(A)):
    
        if max < A[i]:
            max = A[i]
            Springboard.append(0)
     
        else:
            Springboard.append(max - A[i])

    print(sum(Springboard))

if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools

    
    main()

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

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

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

except:
    pass
print(fumidai)

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

maxi, res = 0, 0
for num in nums:
  maxi = max(maxi, num)
  res += maxi - num
  
print(res)

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

tmpMax=0
sumHeight=0
for Ai in A:
  if tmpMax>Ai:
    sumHeight+=tmpMax-Ai
  else:
    tmpMax=Ai
print(sumHeight)

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

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

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

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


highest = 0
ans = 0

for i in a:
    if i > highest:
        highest = i
    ans += highest - i

print(ans)

Solution (Python):
N = input()
A = [int(p) for p in input().split(' ')]
stools = list()
stools.append(0)
for k in range(1,len(A)):
    if(A[k-1]>A[k]):
        stools.append(A[k-1]-A[k])
        A[k] = A[k-1]
    else:
        stools.append(0)
print(sum(stools))

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

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

S = [0]*N
i = 0

while i != N-1 and i == 0 and i < N-1:
    for j in range(i+1,N):
        if A[i] >= A[j]:
            S[j] = A[i] - A[j]
        else:
            i = j
            continue
    i = j

print(sum(S))

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