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

total = 0
th = 0

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

print(total)

Solution (Python):
def resolve():
    '''
    code here
    '''
    N = int(input())
    As = [int(item) for item in input().split()]

    res = 0
    if N >= 2:
        prev = As[0]
        for item in As[1:]:
            if item < prev:
                res += prev - item
            else:
                prev = item

    else:
        res = 0

    print(res)

if __name__ == "__main__":
    resolve()

Solution (Python):

import sys

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


n = I()
A = list(MI())

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

print(ans)

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

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

out = 0
t_h = A[0]

for i in A:
    a = max(t_h - i, 0)
    out += a
    t_h = i + a

print(out)

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

ans = 0

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

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

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

res =0
temp = al[0]

for i in range(n):
    x = temp - al[i]
    if x >0:
        res += x
    else:
        temp = al[i]

print(res)

Solution (Python):

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

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

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



def calculate(n, arr):
    currentHeight = arr[0]
    result = 0
    for i in range(1,n):
        if arr[i] < currentHeight:
            result += (currentHeight - arr[i])
        else:
            currentHeight = arr[i]

    return result

res = calculate(N, ARR)

print(res)

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

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

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

print(ans)

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

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

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]


def main():
    n = int(input())
    a = list(map(int,input().split()))
    ans = 0
    mae = 0
    for i in a:
        if mae > i:
            ans += (mae - i)
            i = mae
        mae = i
    print(ans)



    
if __name__ == "__main__":
    main()

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

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

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

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

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

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    summation = 0
    tallest = A[0]
    for a in A[1:]:
        if a > tallest:
            tallest = a
        else:
            diff = tallest - a
            summation += diff

    print(summation)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
arr = [int(n) for n in input().split()]
l, res, prev = len(arr), 0, arr[0]
for i in range(1, l):
  now = arr[i]
  if now > prev:
    prev = now
  else:
    res += prev - now
print(res)

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

goukei=0

saidai=a[0]

for i in range(1,len(a)):
    if a[i]<saidai:
        goukei+=saidai-a[i]
    else:
        saidai=a[i]

print(goukei)

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

m = a[0]
res = 0

for v in a:
    if m < v:
        m = v
    else:
        res += m-v
print(res)

Solution (Python):


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

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

print(ans)

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

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

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

now_h = A[0]
ans = 0

for a_i in A:
  if now_h>a_i:
    ans += now_h-a_i
  else:
    now_h = a_i
    
print(ans)

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

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

Solution (Python):
N,*A = map(int, open(0).read().split())
ans = 0
temp = A[0]
for x in A:
    if x < temp:
        ans += temp - x
    else:
        temp = x
print(ans)

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

ans = 0

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

print(ans)

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

ans = 0
prev_height = A[0]
for current_height in A[1:]:
    if prev_height>current_height:
        ans+=prev_height-current_height
    else:
        prev_height=current_height


print(ans)

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

ans = 0
prev = 0
for a in A:
    now = a
    if prev<=now:
        prev = now
        continue
    ans += (prev-now)

print(ans)

Solution (Python):
import sys

if sys.subversion[0] == "PyPy":
    import io, atexit

    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))

    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()
RS = raw_input
RA = lambda x=int: map(x, RS().split())
RN = lambda x=int: x(RS())


def solve():
    max = 0
    ans = 0
    for i in range(n):
        if max > a[i]:
            ans += max - a[i]
        else:
            max = a[i]
    print(ans)
    return






n = RN()
a = RA()
solve()

Solution (Python):
import os
import sys
from io import BytesIO, IOBase


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



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

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

h_sum = 0

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

print(h_sum)

Solution (Python):



import sys


def get_ints():
    return map(int, sys.stdin.readline().strip().split())


def std_in():
    return sys.stdin.readline().strip()


def main():
    n = int(std_in())
    a = list(get_ints())

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


if __name__ == "__main__":
    main()

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

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


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

print(humidai)

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

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

print(ans)

Solution (Python):
import sys
N=sys.stdin.readline().strip()
A=map(int, sys.stdin.readline().split())
a_max=None
ans=0
for i,a in enumerate(A):
    if i==0:
        a_max=a
    else:
        if a<a_max:
            ans+=(a_max-a)
        elif a_max<a:
            a_max=a
print ans

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

Solution (Python):





def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return x

def str_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(input()))
    return x

def int_map():
    return map(int,input().split())

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

def print_space(l):
    return print(" ".join([str(x) for x in l]))


import numpy as np




N = int(input())
A = int_list()

ans = 0

if N == 1:
    print(0)
    exit()

now = A[0]

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

print(ans)

Solution (Python):


from pprint import pprint
from collections import deque, defaultdict
import itertools
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

ans = 0
prev = None
for a in A:
    if prev is None:
        prev = a
        continue
    if prev > a:
        diff = prev - a
    else:
        diff = 0
    ans += diff
    prev = a + diff

print(ans)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
ans = sum(a)

for i in range(N-1):
  if a[i]>a[i+1]:
    a[i+1]=a[i]
ans_1 = sum(a)
print(ans_1-ans)

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

mae = a[0]
ans = 0

for i in a:
    if mae > i:
        ans += mae - i
    else:
        mae = i

print(ans)

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

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

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

for i in range(len(a_list) - 1):
  if a_list[i] > a_list[i+1]:
    score += a_list[i] - a_list[i+1]
    a_list[i+1] = a_list[i]
    
print(score)

Solution (Python):

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

max_height = 0
ans = 0
for i in range(N):
    if A_list[i] > max_height:
        max_height = A_list[i]
    else:
        ans += max_height - A_list[i]
print(ans)

Solution (Python):
import sys

N = int(sys.stdin.readline())
A = map(int, sys.stdin.readline().split())

ans = 0
pre = 0
for a_i in A:
    
    if a_i < pre:
        diff = pre - a_i
        a_i += diff
        ans += diff
    pre = a_i

print(ans)

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

Solution (Python):
import sys
import numpy as np
from numba import vectorize

readline = sys.stdin.readline
read = sys.stdin.read

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

@vectorize("i8(i8,i8)")
def larger (a,b):
    return max(a,b)

b = larger.accumulate(a)
ans = sum(b-a)
print(ans)

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

print(r)

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

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

ans = 0
chk = a[0]
for x in a[1:]:
    if chk > x:
        ans += chk - x
    else:
        chk = x
print(ans)

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

humidai = 0

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

Solution (Python):
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    ans = 0
    for i in range(1, N):
        if A[i] < A[i-1]:
            sh = A[i-1] - A[i]
            A[i] += sh
            ans += sh
    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_i = list(map(int, input().split()))
maxval = 0
ans = 0
for a in a_i:
    maxval = max(maxval, a)
    if maxval > a: ans += maxval - a
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]
print(ans)

Solution (Python):
n = int(input())
al = list(map(int, input().split()))
max = 0
r = 0
for x in al :
    if max < x :
        max = x
    else :
        r += max - x
print(r)

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

    print(sum)

    
if __name__ == '__main__':
    main()

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

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

ans = 0

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

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

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

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

print(ans)

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

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
from math import ceil

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    n = II()
    a_s = LI()
    big = 0
    sum_ = 0
    for i in a_s:
        if big > i:
            sum_ += big - i
        else:
            big = i
    print(sum_)

if __name__ == '__main__':
    main()

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

for i in range(1,N):
  ans+=max(0,A[i-1]-A[i])
  A[i]=max(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):
    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()))
now = a[0]
res = 0
for i in range(1, n):
    res += max(0, now - a[i])
    now = max(a[i], now)

print(res)

Solution (Python):
import sys
from io import StringIO
import unittest
import os


sys.setrecursionlimit(999999999)



def resolve(test_def_name=""):
    
    n = int(input())
    a_s = list(map(int, input().split()))

    if n == 1:
        print(0)
        return

    ans = 0
    work = a_s[0]
    for a in a_s[1:]:
        if work - a > 0:
            ans += work - a
        else:
            work = a

    print(ans)



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """5
2 1 5 4 3"""
        output = """4"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """5
3 3 3 3 3"""
        output = """0"""
        self.assertIO(test_input, output)

    
    def test_original(self):
        pass



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

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

fumidai=0

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

print(fumidai)

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

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

sum = 0

for i in range(1, len(A)):
    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()))

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

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

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

print(count)

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

print(count)

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

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

print(ans)

Solution (Python):
import sys

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

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

sum = 0
amax = A[0]

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

print(sum)

Solution (Python):

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

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


print(ans)

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

Solution (Python):

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):
  h = a[i-1] - a[i]
  if h < 0:
    h = 0
  ans += h
  a[i] += h
  

print(ans)

Solution (Python):
N = int(input())
A = [int(x) for x in 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
now = a[0]
for i in range(n-1):
    if now > a[i+1]:
        ans += now - a[i+1]
    else:
        now = a[i+1]
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

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

ans = 0
v = A[0]
for a in A:
    if a < v:
        ans += v - a
    v = max(v, a)
print(ans)

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

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

print(cnt)

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

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

Solution (Python):
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    sum = 0
    now = 0
    for a in a_list:
        if now <= a:
            now = a
        else:
            sum += now-a
    print(sum)
if __name__ == '__main__':
    main()

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

sum = 0

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

print(sum)

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

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

Solution (Python):

import sys

sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

from collections import defaultdict
sys.setrecursionlimit(10**7)
import math








INF = 1 << 50



def run():
    N, *A = map(int, read().split())
    ans = 0
    cache = A[0]
    for a in A:
        if cache > a:
            ans += cache - a
        else:
            cache = a

    print(ans)


if __name__ == "__main__":
    run()

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


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








count = 0

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

print(count)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    count = 0
    for n in range(1, N):
        if A[n - 1] <= A[n]:
            continue
        need = A[n - 1] - A[n]
        count += need
        A[n] += need 
    print(count)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
l = [int(x) for x in input().split()]
m = l[0]
ans = 0
for i in l:
    if m>i:
        
        ans+=m-i

    m = max(m,i)
print(ans)

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

Solution (Python):
n=int(input(""))
lisa=input("").split(" ")
t=0
s=int(lisa[0])
for i in lisa:
    if(s<int(i)):
        s=int(i)
    else:
        t+=s-int(i)
print(t)

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

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

total = 0

max_height = 0

for height in heights:
  if height > max_height:
    max_height = height
  else:
    total += max_height - height

print(total)

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

Solution (Python):
N = int(input())
A_list = [int(e) for e in input().split()]

before_max = A_list[0]
ans = 0

for i in range(N):
    
    if A_list[i] < before_max:
        ans += before_max - A_list[i]
    elif A_list[i] > before_max:
        before_max = A_list[i]
        
print(ans)

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

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

print(ans)

Solution (Python):
N = int(input())
A = tuple(map(int, input().split()))
now_max = 0
ans = 0
for i in A:
    if now_max < i:
        now_max = i
    elif now_max > i:
        ans += (now_max - i)
print(ans)

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

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

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

Solution (Python):


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

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

print(total)