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):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readlines
def main():
    lines = input()

    N = int(lines[0].strip())
    A = list(map(int, lines[1].split()))

    max_ = 0
    total = 0
    for i, x in enumerate(A):
        if x <= max_:
            total += (max_ - x)
        else:
            max_ = x
    print(total)

main()

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

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

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

ans = sum(B)
print(ans)

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

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

x = 0

before_a = 0

for a in A:
    if a < before_a:
        x += (before_a - a)
    else:
        before_a = a
    
print(x)

Solution (Python):
from __future__ import print_function

from functools import reduce
from operator import mul
from collections import Counter
from collections import deque
from itertools import accumulate
from queue import Queue
from queue import PriorityQueue as pq
from heapq import heapreplace
from heapq import heapify
from heapq import heappushpop
from heapq import heappop
from heapq import heappush
import heapq
import time
import random
import bisect
import itertools
import collections
from fractions import Fraction
import fractions
import string
import math
import operator
import functools
import copy
import array
import re
import sys
sys.setrecursionlimit(500000)


input = sys.stdin.readline


def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return


















def combinations_count(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom


mod = 1000000007


def combinations_count_mod(n, r):
    r = min(r, n - r)
    numer = reduce(lambda x, y: x*y % mod, range(n, n - r, -1), 1)
    denom = pow(reduce(lambda x, y: x*y % mod, range(1, r + 1), 1), mod-2, mod)
    return numer * denom % mod


def solve():
    pass


def main():
    n = int(input().strip())
    a = list(map(int, input().strip().split()))
    max_x = a[0]
    ans = 0
    for i in range(1,n):
        ans += max(max_x - a[i], 0)
        max_x = max(max_x, a[i])
        
        
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

print(total)

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

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

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

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

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

print(ans)

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

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

print(step)

Solution (Python):
import copy
n=int(input())
a=list(map(int,input().split()))
b=copy.deepcopy(a)
for i in range(1,n):
  if a[i-1] > a[i]:
    a[i] = a[i-1]
print(sum([a[i]-b[i] for i in range(n)]))

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

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

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

print(ans)

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

x = A[0]
y = 0
for a in A:
    if a >= x:
        x = a
    else:
        y += x - a
        
print(y)

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

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

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

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

for i in range(len(A)):
    if i == 0:
        continue
    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()))

mm = 0
ans = 0
for i in range(len(a)):
    mm = max(mm, a[i])
    q = mm - a[i]
    ans += q

print(ans)

Solution (Python):
def main():
    N = map(int, input().split())
    A = list(map(int, input().split()))
    max_list = [0]*(len(A))
    max_list[0] = A[0]
    for i in range(1, len(A)):
        max_list[i] = max(max_list[i - 1], A[i])
    diff_sum = 0
    for i in range(len(A)):
        diff_sum += max_list[i] - A[i]
    return diff_sum

if __name__ == '__main__':
    print(main())

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

Solution (Python):
def solution(data):
    mh = float("-inf")
    res = 0
    for x in data:
        mh = max(mh, x)
        if mh > x:
            res += (mh - x)
    return res

n = input()
data = list(map(int, input().split()))
print(solution(data))

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

Solution (Python):









def main():
    N = int(input())
    A = map(lambda x: int(x), input().split())

    currentMax = 0
    result = 0
    for item in A:
        if item > currentMax:
            currentMax = item
        else:
            result += currentMax - item

    print(result)


if __name__ == "__main__":
    main()

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

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

        else :
            hmax = a[i+1]

    print(hsum)

if __name__ == "__main__":
    solve()

Solution (Python):
N = int(input())
listA = [int(i) for i in input().split()]
maxH = listA[0]
ans = 0
for a in listA[1:]:
    if maxH > a:
        ans = ans + (maxH - a)
    elif maxH < a:
        maxH = a
print(ans)

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

step = 0
dif = 0
maxHeight = A[0]
for n in range(1, N):
    if maxHeight > A[n]:
        step += (maxHeight - A[n])
    elif maxHeight < A[n]:
        maxHeight = A[n]
    else:
        pass

print(step)

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

for i in range(1,len(arr)):
  cmp = max(arr[i],cmp)
  add = cmp - arr[i]
  ans = ans + add

print(ans)

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

print(diff)

Solution (Python):


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

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

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

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

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

l = A[0]
b = 0
for i in A[1:]:
    if l > i:
        b = l - i + b
    elif l < i:
        l = i
print(b)

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

biggest = 0
ans = 0
A_new = []

for i in range(N):
    biggest = max(biggest, A[i])
    A_new.append(biggest)
    ans += A_new[i] - A[i]

print(ans)

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

max_ = 0
result = 0
for i in range(N):
    if max_ < A[i]:
        max_ = A[i]
    elif A[i] < max_:
        result += (max_ - A[i])
print(result)

Solution (Python):
N=input()
A=[int(_) for _ in input().split()]
x = 0
amax = 0
for a in A:
  if amax > a: x += (amax-a)
  else: amax = a
print(x)

Solution (Python):




import sys
input = sys.stdin.readline



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

    most = A[0]
    res = 0

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

    print(res)



main()

Solution (Python):


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


ans = 0
highest = 0
for n in range(N):
    highest = max(highest, A[n])
    ans += max(highest - A[n], 0)


print(ans)

Solution (Python):
import math

def resolve():
    N=int(input())
    A=list(map(int,input().split()))
    tmp=0
    Max=0
    for i in range(1,N):
        Max=max(Max,A[i-1])
        tmp+=max(0, Max - A[i])

    print(tmp)
resolve()

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

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

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

sin = A[0]
dai = 0
ans = 0
for i in range(1,N):
    sin = max(sin,A[i])
    ans += sin-A[i]
print(ans)

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

def main():
    f = 0
    p = s[0]
    for ss in range(1, n):
        if s[ss] <= p:
            f += p - s[ss]
        else:
            p = s[ss]
    
    print(f)
    return
main()

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

sum = 0
max = 0

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

print(sum)

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

Solution (Python):

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

length = 0
step_sum = 0

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

print(step_sum)

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

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

res = 0
max_val = -1
for a in A:
    if a >= max_val:
        max_val = a
    else:
        res += max_val - a

print(res)

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

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

Solution (Python):

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

t = 0 ;
for idx , x in enumerate ( A ) :
	if idx==0 : continue ;

	if A[idx] < A[idx-1] :
		r = A[idx-1] - A[idx] ; 
		t += r ; 
		A[idx] += r ; 

print ( t ) ;

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

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

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

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

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

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

max=0
sum=0

for i in list:
    if i>max:
        max=i
    else:
        sum+=(max-i)

print(sum)

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

t = arr[0]

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

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

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

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

print(sum)

Solution (Python):
import os
import heapq
import sys,threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10**5)
from io import BytesIO, IOBase
def gcd(a,b):
    if b==0:

        return a
    else:
        return gcd(b,a%b)
def power(x, p,m):
    res = 1
    while p:
        if p & 1:
            res = (res * x) % m
        x = (x * x) % m
        p >>= 1
    return res
def inar():
    return [int(k) for k in input().split()]

def lcm(num1,num2):
    return (num1*num2)//gcd(num1,num2)

def main():
    n=int(input())
    arr=inar()
    ms=arr[0]
    ans=0
    for i in range(1,n):
        if arr[i]<ms:
            ans+=ms-arr[i]
        else:
            ms=max(arr[i],ms)
    print(ans)


BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()

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

ans = 0
max = A[0]

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

Solution (Python):
nn = int(input())
aa = list(map(int, input().split()))

mymax = aa[0]
steps = 0
for i in range(1,nn):
    if mymax >= aa[i]:
        steps += mymax - aa[i]
    else:
        mymax = aa[i]

print(steps)

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

step = 0
mx = 0
for i in range(len(A)-1):
    if A[i] > mx:
        mx = A[i]
    if mx > A[i+1]:
        step += mx-A[i+1]

print(step)

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

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

ans = 0
for i in range(len(a)):
    if i == 0:
        height = a[i]
        continue
    if a[i] >= height:
        height = a[i]
        continue
    ans += height - a[i]
print(ans)

Solution (Python):
num_p = int(input())
pre_height = 1
step_sum = 0
li= input().split()
for i in range(num_p):
    height = int(li[i])
    if height > pre_height:
        step_sum += 0
        pre_height = height
    else:
        step_sum += pre_height - height
        pre_height += 0
        
print(step_sum)

Solution (Python):








n = int(input())
a = list(map(int, input().split()))
ans = 0
num = a[0]

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

    else:
        ans += num - a[i+1]

print(ans)

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

Solution (Python):

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

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

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

    else:
        dekai=a[i]

print(humidai)

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

ans = 0
dai = 0

for i in range(1, n):
    dai = min(0, a[i]-a[i-1])*(-1)

    if dai != 0:
        a[i] += dai
        ans += dai

print(ans)

Solution (Python):
import math

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

    count = 0
    currentHeight = A[0]

    for i in range(1, len(A)):
        if A[i] < A[i-1]:
            dai = A[i-1] - A[i]
            A[i] += dai
            count += dai

    print(count)

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

        ans += Ai - An[i]
    Ai = max(Ai, An[i])
print(ans)

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

ans = 0
now = AList[0]
for a in AList:
  if a < now:
    ans += (now - a)
  elif a > now:
    now = a
print(ans)

Solution (Python):


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

goukei=0

bar=S[0]

for i in range(N-1):
    if S[i+1]<bar:
        goukei+=bar-S[i+1]
    elif S[i+1]>bar:
        bar=S[i+1]

print(goukei)

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

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

print(sam)

Solution (Python):

n=int(input())
l=list(map(int,input().split()))
ans=0
for i in range(1,n):
  if 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()))

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):
from sys import stdin
readline = stdin.buffer.readline

def main():
    N = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    ans = 0
    h = A[0]
    for a in A:
        if a > h:
            h = a
        elif a < h:
            ans += h - a
    print(ans)

if __name__ == "__main__":
    main()

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

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

print(ans)

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

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

pre = A[0]
ans = 0

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

print(ans)

Solution (Python):

import glob
import os

bn = os.path.basename(__file__).split('.')[0]
dn = os.path.dirname(__file__).split('/')


REL_PATH = dn[len(dn)-2] + '\\' + dn[len(dn)-1] + '\\' + bn


TOP_PATH = 'C:\\AtCoder'

class Common:

    problem = []
    index = 0

    def __init__(self, rel_path):
        self.rel_path = rel_path

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            self.index += 1
            return self.problem[self.index-1]

        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(TOP_PATH + '\\' + self.rel_path + '/*.txt'):
                print("Test: " + path)
                self.initialize(path)
                self.resolve()
                print("\n\n")
        except NameError:
            self.resolve()


class Solver(Common):

    def resolve(self):

        N = int(self.input_data())
        A = list(map(int, self.input_data().split()))

        a_len = len(A)
        max = A[0]
        result = 0
        for i in range(1, a_len):
            if A[i] < max:
                result += (max - A[i])
            else:
                max = A[i]
        print(str(result))

solver = Solver(REL_PATH)
solver.exec_resolve()

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

before = 0
d = [0] * N
for i in range(N):
    if i == 0:
        before = i
        continue

    if (A[before] + d[before]) > A[i]:
        d[i] = (A[before] + d[before]) - A[i]
        before = i
    else:
        before = i

print(sum(d))

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

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

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
highest = 0
ans = 0
for a in A:
    if a >= highest:
        highest = a
    else:
        ans += highest - a
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):
import sys
sys.setrecursionlimit(10**7)
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(input())
    As = list(map(int, input().split()))

    s = 0
    max_n = As[0]
    for i in range(len(As)):
        if i == 0: continue
        if max_n > As[i]:
            s += max_n - As[i]
        elif max_n < As[i]:
            max_n = As[i]
    print(s)

if __name__ == '__main__':
    main()

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

for i in range(1, n):
    step[i] = max(0, a[i-1] + step[i-1] - a[i])
print(str(sum(step)))

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

daisum = 0
minh = 0
for i in a:
  dai = 0
  if i < minh:
    dai = minh - i
    daisum += dai
  elif i > minh:
    minh = i

print(daisum)

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

    current = A[0]
    answer = 0
    for i in range(N):
        if A[i] < current:
            answer += current - A[i]
        elif A[i] > current:
            current = A[i]

    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict
import sys

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

totalStep = 0

prev = 0

for a in A:
    step = 0
    if a < prev:
        step = prev-a
        a += step

    totalStep += step

    prev = a
    totalStep

print(totalStep)

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

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

Solution (Python):

import math

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


b = [a[0] for _ in range(n)]
tmp = a[0]

for i in range(1, n): 
    if a[i] >= tmp:
        tmp = a[i]
    b[i] = tmp 

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()))
cnt = 0

ma = a[0]
for i in range(1, n):
  if a[i] < ma:
    cnt += ma - a[i]
  elif a[i] == ma:
    pass
  else:
    ma = a[i]
print(cnt)

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

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

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

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

ans = 0
max_a = A[0]

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

print(ans)

Solution (Python):
def abc176c_step():
    n = int(input())
    a = list(map(int, input().split()))
    total = 0
    max_val = a[0]
    for v in a:
        if v <= max_val:
            total += max_val - v
        else:
            max_val = v
    print(total)

abc176c_step()

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

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

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

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

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

cnt = 0
max_h = a[0]
for aa in a:
    if aa > max_h:
        max_h = aa
    cnt += max_h - aa

print(cnt)

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

max_h=a[0]
ans=0
for h in a:
    if h<max_h:
        ans+=(max_h-h)
    else:
        max_h=max(h,max_h)
print(ans)

Solution (Python):
from sys import stdin
n = int(stdin.readline().rstrip())
a =  list(map(int,input().split()))
wk = 0  
tp = 0  
ans = 0

num = 0
while num < n:
  
  wk = a[num]
  if tp < wk:
    tp = wk
  if a[num] < tp:
    ans = ans + (tp - a[num])
  num = num + 1

print(ans)

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

Solution (Python):
N=int(input())
A=[int(_) for _ in input().split()]
height=A[0]
s=0
for a in range(len(A)-1):
    if A[a+1]<height:
        s+=height-A[a+1]
    elif A[a+1]>height:
        height=A[a+1]
print(s)

Solution (Python):
import numpy as np

N=int(input())

A=np.array(input().split(" "),dtype=np.int64)

step=[0]
Max=A[0]
for i in range(1,N):
    if(Max>A[i]):
        step.append(Max-A[i])
    else:
        Max=A[i]
        step.append(0)

print(sum(step))

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

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

c_max = 0
total = 0

for i in range(N):
  if i == 0:
    c_max = A[0]
  elif A[i] < c_max:
    total += c_max - A[i]
  else:
    c_max = A[i]

print(total)

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

step = 0
max_height = 0
for i in range(N):
    if max_height <= A[i]:
        max_height = A[i]
    else:
        step += (max_height - A[i])
print(step)