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

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

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

level = 0
step = 0
for i in range(n):
  if level < a[i]:
    level = a[i]
  else:
    step += level - a[i]
print(step)

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

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

Solution (Python):
from sys import exit, stdin




input = stdin.readline


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

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

ans = 0
lmax = 0
for n in A:
    if n < lmax:
        ans += lmax - n
    else:
        lmax = n

print(ans)

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

now_max = A[0]
ans = 0

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

Solution (Python):
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N = int(input())
    A = list(map(int, input().split()))
    A = collections.deque(A)

    count = 0
    dai = 0
    mae = 0
    while len(A) > 0:
        a = A.popleft()
        if a >= mae + dai:
            mae = a
            dai = 0
        else:
            dai = mae + dai - a
            mae = a
            count += dai
    print(count)





if __name__ == '__main__':
    solve()

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

Solution (Python):

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

hh=a[0]
hsum=0
for i in range(n):
    if a[i]<hh:
        dh=hh-a[i]
        hsum+=dh
    else:
        hh=a[i]
    
print(hsum)

Solution (Python):
n = int(input())
lst = [ int(i) for i in input().split() ]
count = 0
mn = lst[0]
for i in lst:
  sa = mn - i
  if sa >= 0:
    count += sa
  elif sa < 0:
    mn = i

print(count)

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

for i in range(n):
    if a[i] >= m:
        m = a[i]
    b[i] = m - a[i]

print(sum(b))

Solution (Python):
import sys
input = sys.stdin.readline
I = lambda : list(map(int,input().split()))

n,=I()
arr=I()
sumi=0
for i in range(1,n):
    if arr[i-1]>arr[i]:
        sumi+=arr[i-1]-arr[i]
        arr[i]=arr[i-1]

print(sumi)

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

print(step)

Solution (Python):
N=int(input())
A=[]

kore=0
A=list(map(int,input().split()))
ma=A[0]
ans=0
for i in range(1,N):
    if(A[i]<=ma):
        ans=ans+ma-A[i]
       

    else:
        ma=A[i]

print(ans)

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

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

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

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

b = [0]
for i in range(n-1):
    s = a[i+1] - (a[i] + b[i])
    if s < 0:
        b.append(abs(s))
    else:
        b.append(0)

c = sum(b)
print(c)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
count=0
B=[A[0]]
for i in range(1,N) :
  if B[i-1]<A[i] :
    B.append(A[i])
  else :
    B.append(B[i-1])
ans=0
for i in range(N) :
  ans+=B[i]-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]:
        diff = A[i] - A[i + 1]
        sum_ += diff
        A[i + 1] += diff
print(sum_)

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

hight = 0
ans = 0

for i in range(len(list_A)):
    if i == 0:
        hight = list_A[i]
    elif list_A[i] > hight:
        hight = list_A[i]
    elif list_A[i] < hight:
        ans += hight - list_A[i]

print(ans)

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

a = 0
b = A[0]
for i in range(N - 1):
    if b > A[i + 1]:
        a += b - A[i + 1]
    else:
        b = A[i + 1]
        continue
print(a)

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

ans = 0
for i in range(1,n):
	if height[i] < height[i-1]:
		ans += height[i-1] - height[i]
		height[i] = height[i-1]

print(ans)

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

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

def solve(n,a):
    counter = 0
    temp = a[0]
    for i in range(1,n):
        if a[i] < temp:
            counter += temp - a[i]
        temp = max(temp,a[i])
    return counter


if __name__ == '__main__':
    main()

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

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


su = 0
for i in range(n-1):
  if int(A[i]) > int(A[i+1]):
    su += (int(A[i]) - int(A[i+1]))
    A[i+1] = A[i]
  else:
    continue
    
print(su)

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

for a in A:
  if a < maxH:
    cnt += maxH - a
  else:
    maxH = a
    
print(cnt)

Solution (Python):
import numpy as np
import math
import scipy as sp
import queue
import collections

MOD = 10 ** 9 + 7
INF = 10 ** 9

def main(kwargs):
    n = kwargs["n"]
    A = kwargs["A"]
    B = [0 for i in range(n)]
    for i in range(n - 1):
        B[i+1] = max(0, A[i] + B[i] - A[i+1])
    if n > 1:
        B[-1] = max(0, A[-2] + B[-2] - A[-1])
    res = sum(B)
    return res

if __name__ == "__main__":
    kwargs = {}

    cin = input().split()
    kwargs["n"] = [int(i) for i in cin][0]

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

    cout = main(kwargs)
    print(cout)

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

max_tall = 0
humidai = 0

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

print(humidai)

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

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

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

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

item_bef = 0
sum = 0
for item in A:
  if item < item_bef:
    sum += (item_bef - item)
    item_bef = item_bef
  else:
  	item_bef = item
print(sum)

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

maxLength = 0
dif = 0

for a in A:
    if(maxLength > a):
        dif += maxLength - a
    if(maxLength < a):
        maxLength = a
        
print(dif)

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

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()))

TOP = A[0]
Ans = 0

for i in range(1, len(A)):
    if TOP > A[i]:
        Ans += TOP - A[i]
    else:
        TOP = A[i]
print(Ans)

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

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

print(sum)

Solution (Python):
n = int(input())
aa = list(map(int, input().split()))
amax = aa[0]
total = 0
for a in aa:
    amax = max(amax,a)
    total += (amax - a)
print(total)

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

temp_max = A[0]
ans = 0
for i in range(1,len(A)):
    if A[i] < temp_max:
        temp_ans = temp_max - A[i]
        ans += temp_ans
    
    else:
        temp_max = A[i]

print(ans)

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

count = 0
highest = a[0]
for i in a:
    if highest < i:
        highest = i
    if i < highest:
        count += highest - i
print(count)

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

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

print(S)

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

ans = 0


a.append(99999999999999999)
tmp = 0
for i in range(n):
  
  if a[i] <= a[i+1]:
    tmp = 0
    continue
  else:
    tmp = a[i] - a[i+1]
    a[i+1] += tmp
    ans += tmp
    
print(ans)

Solution (Python):

n = int(input())
a = list(map(int, input().split()))
i = 1
ans = 0
before = a[0]
while i < len(a):
    if before > a[i]:
        ans += before - a[i]
    before = max(before,a[i])
    i+=1
print(ans)

Solution (Python):
n = int(input())
heights = list(map(int, input().split()))
step = 0

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

print(step)

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

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

print(ans)

Solution (Python):

def main():
    n = int(input())
    ai = list(map(int, input().split()))
    cnt = 0
    
    for i in range(n):
        if i != n-1:
            if ai[i] > ai[i+1]:
                cnt += ai[i]-ai[i+1]
                ai[i+1] = ai[i]
    print(cnt)
    
if __name__ == "__main__":
    main()

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
assert 1 <= N and N <= 2*1e5
for a in A:
    assert a >= 1 and a <= 1e9
x = []
for i in range(len(A)-1):
    if A[i] > A[i+1]:
        d = A[i]-A[i+1]
        x.append(d)
        A[i+1] = A[i]
print(sum(x))

Solution (Python):
import math
t = int(input())
a = list(map(int,input().split()))
highest = a.index(max(a))
ans=0
for i in range(1,highest):
    if a[i]<a[i-1]:

        ans+= (a[i-1]-a[i])
        a[i] = a[i-1]
for i in range(highest+1,len(a)):
    if a[i]<a[i-1]:
        ans+=(a[i-1]-a[i])
        a[i]=a[i-1]
print(ans)

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

for i in range(len(x)):

    if i==0:
        continue

    if x[i]<x[i-1]:
        size+=abs(x[i]-x[i-1])
        x[i]=x[i-1]


print(size)

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

a=list()
d=input().split()

for i in d:
    a.append(int(i))
d=0
for i in range(1,n):
    if a[i]<a[i-1]:
        d+=a[i-1]-a[i]
        a[i]+=a[i-1]-a[i]
print(d)

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

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

print(t)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]
humidai = 0
prev = A[0]
for i in range(1, N):
    if prev <= A[i]:
        prev = A[i]
    else:
        humidai += prev - A[i]
print(humidai)

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
ans = 0
pre = A[0]
for a in A:
    if pre <= a:
        pre = a
        continue
    ans += pre - a
print(ans)

Solution (Python):
import sys
N = sys.stdin.readline()
Alst = list(map(int, sys.stdin.readline().split()))
now = 1
x = 0
for i in Alst:
    if now < i:
        now = i
    else:
        x += (now - i)

print(x)

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

ans = 0
se = 0
for i in A:
  se = max(se,i)
  ans += se-i
print(ans)

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

for i in range(N):
    if A[i]>k:
        k=A[i]
    else:
        B[i]=k-A[i]
        

print(sum(B))

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

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

L = 0
M = 0

for j in range(N):
	if A[j] >= M:
		M = A[j]
	else:
		L += M-A[j]

print(L)

Solution (Python):








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

maxVal = a[0]

ans = [0]*n

for i in range(1,n):
    if maxVal > a[i]:
        ans[i] = maxVal - a[i]
    elif maxVal < a[i]:
        maxVal = a[i]

print(sum(ans))

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

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

sum = 0

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

Solution (Python):
import sys

n = int(input())
r = list(map(int,input().split()))
ans = 0

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

print(ans)

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

ans = 0
mx = lst[0]
for _ in range(n):
    mx = max(lst[_], mx)
    ans += mx - lst[_]
    
print(ans)

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

Solution (Python):
def actual(N, A):
    height_amount = 0

    for i in range(N - 1):
        left = A[i]
        right = A[i + 1]

        if left > right:
            diff = left - right
            height_amount += diff

            A[i + 1] += diff

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

print(actual(N, A))

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

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

ans = 0
tmp_max = input_list[0]
for i in range(1, n):
    if input_list[i] < tmp_max:
        ans +=  tmp_max - input_list[i]
        
    if input_list[i] > tmp_max:
        tmp_max = input_list[i]
        
        
print(ans)

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

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

    step = [0] * (N + 1)
    i = 0
    a_old = 0
    for a in A_list :
        if i == 0 :
            step[i] = 0
            a_old = a
        else :
            
            
            if a >= (step[i] + a_old) :
                step[i+1] = 0
            else :
                step[i+1] = step[i] + a_old - a
            a_old = a
        i += 1
        


    ans = sum(step)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
heights = list(map(lambda x: int(x), input().split()))
sum_boards = 0

for i in range(len(heights) - 1):
    gap = heights[i+1] - heights[i]
    
    if gap >= 0:
        continue
    sum_boards += abs(gap)
    heights[i+1] = heights[i]
    
print(sum_boards)

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

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

n, *a = map(int, read().split())
h = a[0]
ans = 0
for i in range(1, n):
    if a[i] < h:
        ans += h - a[i]
    elif a[i] > h:
        h = a[i]
print(ans)

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


print(ans)

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

d = 0
a0 = an[0]
for i in range(1, len(an)):
    a1 = an[i]
    if a0 >= a1:
        d += a0 - a1
    else:
        a0 = a1
    
print(d)

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]
    else:
        pass
print(cnt)

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

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

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

ans = 0

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

print(ans)

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

cnt = 0
for i in range(len(A)):
    if i==0:
        continue

    if A[i-1]-A[i]>0:
        cnt +=A[i-1]-A[i]
        A[i]=A[i-1]

print(cnt)

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

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

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

num_before = A[0]
dai_sum = 0
for num in A:
    if num_before > num:
        dai_sum += num_before - num
    else:
        num_before = num
print(dai_sum)

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

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

print(ans)

Solution (Python):
"""
AtCoder :: Beginner Contest 176 :: C - Step 
https://atcoder.jp/contests/abc176/tasks/abc176_c
"""
import sys


def main():
    """Main program."""
    N = int(sys.stdin.readline())
    A = [int(i) for i in sys.stdin.readline().split()]
    max_a = A[0]
    stools_height = 0
    for a in A:
        
        
        
        if a < max_a:
            stools_height += (max_a - a)
        
        
        elif a > max_a:
            max_a = a
    print(stools_height)


if __name__ == '__main__':
    main()

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

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

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

print(sum)

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

A=list(map(int,input().split()))
co=A[0]
for i in range(N-1):
  s=A[i+1]-co
  if s<0:
    re=re-s
  else:
    co=A[i+1]
print(re)

Solution (Python):
import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N = II()
    A = LI()
    hmax = A[0]
    hmae = A[0]
    ans = 0

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

        


    
    print(ans)

if __name__ == "__main__":
	main()

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

b = a
ans = 0
for i, item in enumerate(a):
  if i == 0:
    continue
  if b[i-1] <= item:
    continue
  else:
    b[i] += a[i-1] - item
    ans += a[i-1] - item
print(ans)

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

max_height = array[0]
fumidai_sum = 0

for h in array:
    if h <= max_height:
        fumidai_sum += max_height - h
    else :
        max_height = h

print(fumidai_sum)

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

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

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

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

print (ans)

Solution (Python):
N = int(input())
persons = list(map(int,input().split()))
stool_height = 0
for i in range(1,N):
    if persons[i] < persons[i-1]:
        height_req = abs(persons[i] - persons[i-1])
        persons[i] = persons[i]+height_req
        stool_height += height_req
print(stool_height)

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

    if mi>a[i]:
        ans+=mi-a[i]
        mi=max(mi,a[i])
        
    else:
        mi=a[i]
print(ans)

Solution (Python):
def main(N, A):
    b = A.pop(0)

    l = 0
    for a in A:
        if a > b:
            b = a
        else:
            l += b - a
    print(l)

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    main(N, A)

Solution (Python):
import numpy as np

n = int(input())
a = input().split()
ai=np.array(a, dtype=int)

fumi=0
amax=ai[0]
for i in range(1,n):
  if (ai[i] >= amax):
    amax=ai[i]
  else:
    fumidai1=amax-ai[i]
    fumi+=fumidai1

print (fumi)

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

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

B = [0]*N

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

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

    ans = sum(ans)
    print(ans)


resolve()

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

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

b = sum(B)
print(b - a)

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

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

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

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

print(str(step))

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

Max = 0
daiCount = 0
for a in A:
    if a <= Max:
        daiCount += (Max - a)
    if a > Max:
        Max = a
print(daiCount)

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

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

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

minPivot=A[0]

stepSum=0

for a in A:
  if minPivot > a:
    stepSum+=minPivot-a
  else:
    minPivot = a

print(stepSum)

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

Solution (Python):

N = int(input())

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

tmp = A[0]

res = 0
for ai in A:
    
    if ai < tmp:
        res += (tmp - ai)
    else:
        tmp = ai

print(res)

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]:
        diff = a[i - 1] - a[i]
        ans += diff
        a[i] += diff
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()))
sum = 0
mae = a[0]
for ai in a[1:]:
  if mae > ai:
    sum = sum + mae - ai
    continue
  mae = ai
print(sum)

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

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

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

now=li[0]
sum=0
for i in li[1:]:
  if now>i:
    sum+=now-i
  elif i>now:
    now=i

print(sum)

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

print(sum(B))

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

bef =0

count=0

for i in A:
    if i<bef:
        count +=(bef-i)
        bef = i+(bef-i)
    else:
        bef=i

print(count)

Solution (Python):
n=int(input())
lst=list(map(int,input().split()))
_max=lst[0]
st=0
for i in lst:
  if i>=_max:
    _max=i
  else:
    st=st+(_max-i)
print(st)

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

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

tmp_max = 0
springboards = []

for i in a:
    if tmp_max < i:
        tmp_max = i
        springboard = 0
    else:
        springboard = (tmp_max - i)

    springboards.append(springboard)
    

print(sum(springboards))

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

Solution (Python):
n = int(input())
a_max = 0
t = 0
for x in map(int, input().split(' ')):
    a_max = x if x > a_max else a_max
    t += a_max - x

print(t)

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

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

amax = 0
ans = 0

for i in a:
    if(amax<=i):
        amax= i
    ans +=amax-i

print(ans)

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

Solution (Python):
n = int(input())
a=list(map(int,input().split()))
b = -1
c = 0

for i in a:
    if i < b:
        c += b - i
    else:
        b = i
        
print(c)

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

total = 0
for i in range(len(an)-1):
    if an[i+1] - an[i] < 0:
        add = (an[i+1] - an[i]) * -1
        an[i+1] += add
        total += add
print(total)

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

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

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

Solution (Python):
import sys
import math


sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()

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()))

cnt = 0

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