Problem:
 Score : 300 points    Problem Statement N persons are standing in a row. The height of the i -th person from the front is A_i .  We want to have each person stand on a stool of some heights - at least zero - so that the following condition is satisfied for every person:  Condition: Nobody in front of the person is taller than the person. Here, the height of a person includes the stool.  Find the minimum total height of the stools needed to meet this goal.      Constraints  1 \leq N \leq 2\times 10^5  1 \leq A_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A_1  \ldots  A_N       Output Print the minimum total height of the stools needed to meet the goal.        Sample Input 1 5
2 1 5 4 3      Sample Output 1 4  If the persons stand on stools of heights 0 , 1 , 0 , 1 , and 2 , respectively, their heights will be 2 , 2 , 5 , 5 , and 5 , satisfying the condition.  We cannot meet the goal with a smaller total height of the stools.       Sample Input 2 5
3 3 3 3 3      Sample Output 2 0  Giving a stool of height 0 to everyone will work.

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

ans = 0

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

print(ans)

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

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

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

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

print(ans)

Solution (Python):
import sys

number_of_line = int(sys.stdin.readline())

sum = 0
height = 0
line = sys.stdin.readline()
humans = line.split(' ')
steps = 0
for human in humans:
    human_height = int(human)
    if human_height < height:
        steps += (height - human_height)
    height = max(human_height, height)

print(steps)

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

li = list(map(int, input().split()))
sum_diff = 0

for i in range(len(li)):
    if i > 0:
        diff = li[i] - li[i - 1]
        if li[i] - li[i - 1] < 0:
            li[i] = li[i] + abs(diff)
            sum_diff += abs(diff)
print(sum_diff)

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

maximum = 0
step_sum = 0
for i in a:
    maximum = max(maximum, i)
    step_sum += maximum - i
print(step_sum)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ma = 0
ans = 0
for i in a:

    if i < ma:
        ans += ma-i
    else :
        ma = i
print(ans)

Solution (Python):
N = int(input())
An = [int(val) for val in input().split()]


Fn = [0 for _ in range(N)]

an_1 = An[0]
fn_1 = 0

for i in range(1, N):
    an = An[i]
    fn =  max(0, an_1 + fn_1 - an)

    Fn[i] = fn
    fn_1 = fn
    an_1 = an

sum = 0
for fi in Fn:
    sum += fi

print(sum)

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

for i in range(N-1):
  large = max(large,A[i+1])
  if large != A[i+1]:
    sum += (large-A[i+1])

print(sum)

Solution (Python):
n=int(input())
listA=list(map(int,input().split()))
listB=[0]

for i in range(len(listA)-1):
  if listA[i+1]<listA[i]:
    listB.append(listA[i]-listA[i+1])
    listA[i+1]=listA[i]
  else:
    listB.append(0)
    
print(sum(listB))

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

Solution (Python):

n = raw_input()
ais = map(int, raw_input().split())
m = ais[0]
q = 0
for i in range(1, len(ais)):
	q += max(0,m - ais[i])
	m = max(m, ais[i])
print q

Solution (Python):
n = int(input())
ary = [int(v) for v in input().split()]

max_tmp = -1
steps = 0

for a in ary:
    if max_tmp > a:
        steps += max_tmp - a
    else:
        max_tmp = a

print(steps)

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

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

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

print(ans)

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

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

tall = 0
ans = 0
for i in A:
    if i < tall:
        ans += (tall - i)

    if i > tall:
        tall = 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())

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

humi = [0,]

for i in range(1, n):
    if (hight[i] >= hight[i-1]):
        humi.append(0)
    else:
        dif = hight[i-1] - hight[i]
        humi.append(dif)
        hight[i] = hight[i] + dif

ans = sum(humi)
print(ans)

Solution (Python):
n = input()
heights = [int(cur) for cur in input().split()]
curr_max = heights[0]
total = 0
for height in heights:
    if height > curr_max:
        curr_max = height
    else:
        total += curr_max - height

print(total)

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

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

print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(1,n):
    if a[i-1] > a[i]:
        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()))
b = [0]*n
for i in range(1,n):
    if a[i] < a[i-1]:
        b[i] = a[i-1] - a[i]
        a[i] = a[i-1]
print(sum(b))

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

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

    ans = 0

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

if __name__ == "__main__":
    solve()

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

ans = 0
max_a = a[0]
for i in range(len(a)-1):
    if max_a<a[i+1]:
        max_a = a[i+1]
    if max_a>a[i+1]:
        ans += max_a - a[i+1]

print(ans)

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

count = 0
for n,a in enumerate(A):
  if(n != 0):
    if(A[n] <= A[n-1]):
      count = count + (A[n-1] - A[n])
      A[n] = A[n-1]
      
print(count)

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

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

highest=L[0]
val=0
for A in L[1:]:
    if A<highest:
        val+=(highest-A)
    elif A>highest:
        highest=A
print(val)

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

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

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

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

    answer = solve(N, A)

    print(answer)


def solve(N, A):
    result = 0
    max_height = A[0]
    for i in range(1, N):
        max_height = max(max_height, A[i])
        if max_height > A[i]:
            result += max_height - A[i]

    return result


if __name__ == "__main__":
    main()

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

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

max = 0
humidai_count = 0

for i in a:
    if max <= i:
        max = i
    else:
        humidai_count += max-i

print(humidai_count)

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

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

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

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

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

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

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

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

ans=0
l2=[l[0]]


for i in range(1,n):
    if l2[i-1] <= l[i]:
        l2+=[l[i]]
        continue
    else:
        l2+=[l2[i-1]]
        ans+=(l2[i]-l[i])

print(ans)

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

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

data = input().split()

sum = 0

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

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

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

Solution (Python):
n = int(input())
a_s = list(map(int, input().split()))
highest = 0
ans = 0

for i in range(n):
    if highest < a_s[i]:
        highest = a_s[i]
    elif highest > a_s[i]:
        ans += (highest - a_s[i])

print(ans)

Solution (Python):













from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
import numpy as np
import random
import bisect
import copy



MOD = int(1e9+7)
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

def keta(kazu): 
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): 
    return reduce(math.gcd, numbers)

def combination(m,n): 
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def fact(n): 
    arr = {}
    temp = n
    for i in range(2,int(n**0.5)+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt
    if temp != 1:
        arr[temp] = 1
    if arr == {}:
        arr[n] = 1
    return arr
    
def main():
    N = int(stdin.readline().rstrip())
    A = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
    ans = 0
    
    for i in range(1,N):
        if A[i] < A[i-1]:
            ans += abs(A[i-1]-A[i])
            A[i] = A[i-1]
        
    print(ans)
            
main()

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

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

print(ans)

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]:
        dif = a[i-1] - a[i]
        count += dif
        a[i] += dif

print(int(count))

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

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

print(S)

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

ki = 0
s = 0
for i in range(0,n):
    if ki < li[i]:
        ki = li[i]
    else:
        s += ki-li[i]
print(s)

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

max = A[0]

for length in A[1:]:
    if length < max:
        length_sum += (max - length)

    elif length > max:
        max = length

print(length_sum)

Solution (Python):
n=input()
A = input().split()
top_sum=0
top=int(A[0])
for i in range(1,int(n)):
    if int(A[i])>top:
        top = int(A[i])
    else:
        top_sum += top - int(A[i])

print(top_sum)

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

Solution (Python):
def step(arr):
    res = 0
    for i in range(1,len(arr)):
        if arr[i] >= arr[i-1]:
            continue
        else:
            res += abs(arr[i-1] - arr[i])
            arr[i] = arr[i - 1]
    return res

def start():
    n = int(input())
    arr = [int(x) for x in input().split(" ")]
    print(step(arr))
start()

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

Solution (Python):
_ = input()
a_n = list(map(int, input().split()))

h = a_n[0]
s = 0
for a in a_n[1:]:
    if h > a:
        s += h -a
    if a > h:
        h = a
print(s)

Solution (Python):
N =int(input())
li = list(map(int,input().split()))
min = li[0]
h = 0

for a in li:
  if min > a:
    h += min - a
  elif min < a:
    min = a

print(h)

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

max_height = heights[0]
total_step = 0

for h in range(1, len(heights)):
    if max_height >= heights[h]:
        total_step = total_step + max_height - heights[h]
    else:
        max_height = heights[h]

print(total_step)

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

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

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

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


current_max_height = 0


need_height = 0
for i in range(N):

    if As[i] > current_max_height:
        current_max_height = As[i]

    need_height += current_max_height - As[i]

print(need_height)

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

num = int(input())
tool = list(map(int, input().split()))

def tool_set(num, tool):
  count = 0
  for i in range(num - 1):
    if tool[i] > tool[i + 1]:
      count += tool[i] - tool[i + 1]
      tool[i + 1] = tool[i]
  print(count)

tool_set(num, tool)

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

sum = 0

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

print(sum)

Solution (Python):
n=int(input())
arr=[int (x) for x in input().split()]
q=arr[:]
i,j=0,1
ans=0
while i<n and j<n:
    if arr[i]>arr[j]:
        arr[j]=arr[i]
    i+=1
    j+=1
print(sum(arr)-sum(q))

Solution (Python):
N = int(input())
line = input()
A = line.split()
hei = 0
for i in range(N-1):
    d = int(A[i+1]) - int(A[i])
    if d < 0:
        hei = hei - d
        A[i+1] = str(int(A[i+1]) - d)
print(hei)

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

result = 0

for idx in range(len(A)):

    if idx == 0:
        continue
    elif A[idx-1] > A[idx]:
        result += A[idx-1] - A[idx]
        A[idx] = A[idx-1]

print(result)

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

for i in range(1, n):
  if lst[i] < num:
    res += lst[i - 1] - lst[i]
    lst[i] = lst[i - 1]
  else:
    num = lst[i]
    
print(res)

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

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

print(count)

Solution (Python):

N = int(input())
B = list(map(int,input().split()))
D = [0] * N

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

print(sum(D))

Solution (Python):
_ = input()
As = map(int, input().strip().split())

total_steps_height = 0
current_height = 0

for Ai in As:
	if current_height > Ai:
		total_steps_height += (current_height - Ai)
	if current_height < Ai:
		current_height = Ai

print(total_steps_height)

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

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

a = row[0]
b =0
fumi = 0
for i in row:
  b = i
  if a <= b:
    a = b
    next
  else:
    fumi += (a - b)
    a = max(a, b)
print(fumi)

Solution (Python):

import sys


def main():
    N = int( sys.stdin.readline() )
    A_list = list(map(int, sys.stdin.readline().split()))

    prev = A_list[0]
    sum_diff = 0

    for i in range(1, len(A_list)):
        if prev > A_list[i]:
            sum_diff += (prev - A_list[i])
        else:
            prev = A_list[i]
    
    print(sum_diff)


if __name__ == "__main__":
    main()

Solution (Python):
if __name__ == "__main__":
  n = int(input())
  ai = list(map(int, input().split()))
  li = []
  for i in range(n - 1):
    if ai[i] > ai[i + 1]:
      li.append(ai[i] - ai[i + 1])
      ai[i+1]=ai[i]
    else:
      pass
  print(sum(li))

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
ma = max(L)
mi = L.index(ma)
S = 0
nma = L[0]

for i in range(1, len(L)):
  if mi <= i:
    S += ma - L[i]
  else:
    nma = max(nma, L[i])
    S += nma - L[i]
print(S)

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

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

for num in range(1,n):
  if lis[num] < lis[num - 1]:
    sum += lis[num - 1] - lis[num]
    lis[num] = lis[num - 1]
    
print(sum)

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

answer = 0
m = a[0]

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

Solution (Python):
n,*a=map(int,open(0).read().split())
count,max_n=0,a[0]
for i in range(1,n):
    if max_n>a[i]:
        count+=max_n-a[i]
    else:
        max_n=max(max_n,a[i])
print(count)

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

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

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
l = 0
count = 0
max_h = 0
for i in range(1,N):
    if A[i-1]>max_h:
        max_h = A[i-1]
    if A[i]<max_h:
        k = max_h - A[i]
        count += k
        if k>l: l=k
        A[i] = max_h
print(count)

Solution (Python):
N, S, *A = map(int,open(0).read().split())
ans = 0

for i in A:
    if i > S:
        S = i
    else:
        ans += S -i
print(ans)

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

Solution (Python):
n = int(input())
a = [int(x.strip()) for x in input().split()]
b = [0]
for i in range(n):
  if i == n-1:
    break
  elif a[i]+b[i] > a[i+1]:
    b.append(a[i]+b[i]-a[i+1])
  else:
    b.append(0)
print(sum(b))

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

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

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

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

Solution (Python):

N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(len(A)-1):
    a_before = A[i]
    a_after = A[i+1]

    
    diff = a_before-a_after
    
    if diff > 0:
        A[i+1] = a_before
        ans += diff

print(ans)

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

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

for i in range(1, N):
    before = A_list[i - 1]
    after = A_list[i]
    if before > after:
        res += before - after
        A_list[i] += before - after

print(res)

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

c = 0

for i in range(n):
  if i == 0:
    continue
  if m[i-1]>m[i]:
    res = m[i-1] - m[i]
    c += res
    m[i] = res+m[i]
  
print(c)

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

total = 0

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

Solution (Python):
N = int(input())
l = map(int,input().split())
two, count = 0, 0
for i in l:
  one = i
  if one < two:
    count += two - one
  else:
    two = one
    continue
    
print(count)

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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")


def solve():
    n = II()
    a = LI()
    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)
    return



if __name__ == '__main__':
    solve()

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
c = 0
S = [0 for i in range(N)]

for i in range(N):
    if A[i] < c :
        S[i] = c - A[i]
    elif A[i] > c :
        c = A[i]
  
ans = sum(S)

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

Solution (Python):


n = int(input())

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

l_max = max(lengths)
i_l_max = lengths.index(l_max)

ans = 0
_max = lengths[0]

for i in range(len(lengths)):
    if i < i_l_max:
        if _max > lengths[i]:
            ans += _max - lengths[i]
        else:
            _max = lengths[i]
    else:
        ans += l_max - lengths[i]

print(ans)

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

Solution (Python):
import sys

input = sys.stdin.readline

def main(n, lst):
	cnt = 0
	for i in range(n-1):
		if lst[i] > lst[i+1]:
			cnt += lst[i] - lst[i+1]
			lst[i+1] = lst[i]
	return cnt

n = int(input())
lst = list(map(int, input().split()))
print(main(n, lst))

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

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

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

print(count)

Solution (Python):
N = int(input())
numbers = input().split()
numbers = list(map(int,numbers))
m = 0
gross_count = 0
for n in numbers:
    if m > n:
        gross_count += (m-n)
    else:
        m = n
print(gross_count)

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

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

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

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

    return ans

print(main())

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

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

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
stool = 0
for i in range(N):
    for j in range(i+1,i+2):
        if j >= N:
            break
        if A[i] > A[j]:
            stool += A[i] - A[j]
            A[j] += A[i] - A[j]

print(stool)

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


pre_max_cnt = arr[0]
total_cnt = 0
for i in arr:
    if pre_max_cnt > i:
        total_cnt += abs(pre_max_cnt - i)
    else:
        pre_max_cnt = i
print(total_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]):
        x=a[i]-a[i+1]
        count+=x
        a[i+1]+=x
print(count)

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

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())
Alist = list(map(int, input().split()))
Sum = 0
for n in range(1, N):
  if Alist[n] < Alist[n-1]:
    Sum += Alist[n-1] - Alist[n]
    Alist[n] = Alist[n-1]
print(Sum)

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n = I()
    a = LI()
    ans = 0
    for i in range(1, n):
        temp = max(a[i-1]-a[i], 0)
        ans += temp
        a[i] += temp
    print(ans)

if __name__ == '__main__':
    main()

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

print(D)

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

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

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

Solution (Python):
import sys
from math import sqrt, gcd, ceil, log, floor
from bisect import bisect, bisect_left
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
input = sys.stdin.readline
read = lambda: list(map(int, input().strip().split()))

MOD = 10**9 + 7


def main():
	
	
	
	n = int(input()); arr = read()
	s = 0
	for i in range(1, n):
		s += max(0, arr[i-1]-arr[i])
		arr[i] += max(0, arr[i-1]-arr[i])

	print(s)






if __name__ == "__main__":
	main()