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

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

Solution (Python):
def main():
    n = map(int, list(input()))
    a = list(map(int, input().split()))
    height = 0
    max_height = a[0]
    for _a in a[1:]:
        if _a == max_height:
            continue
        elif _a > max_height:
            max_height = _a
        else:
            height += max_height - _a
    print(height)



if __name__ == "__main__":
    main()

Solution (Python):
import sys

N = int(sys.stdin.readline().rstrip())
A = [int(x) for x in sys.stdin.readline().rstrip().split()]

sumf = 0
max_ = 0
for a in A:
    if max_ < a:
        max_ = a
        continue
    else:
        sumf += max_ - a

print(sumf)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy







mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
al = inpl()
ans = 0
tmp = 0
pre = al[0]
for a in al:
    if(pre > a):
        ans += pre - a
    else:
        pre = a
print(ans)

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n = ri()
ans = 0
a = rl()
tmp = 0
for i in a:
  ans += max(0,tmp-i)
  tmp = max(tmp, i)
print(ans)

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

ans = 0
h = a[0]
for i in a[1:]:
    if i <= h:
        ans += h - i
    else:
        h = i

print(ans)

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

tmax = A[0]
ans = 0
for p in range(1, N):
    if A[p] < tmax:
        ans += tmax - A[p]
    elif A[p] > tmax:
        tmax = A[p]

print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1  
MOD = 10 ** 9 + 7


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


def solve(N, AS):
    ret = 0
    prevStep = 0
    for i in range(1, N):
        if prevStep + AS[i - 1] > AS[i]:
            prevStep = prevStep + AS[i - 1] - AS[i]
            ret += prevStep
        else:
            prevStep = 0
    return ret


def main():
    N = int(input())
    AS = list(map(int, input().split()))
    print(solve(N, AS))



T1 = """
5
2 1 5 4 3
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
4
"""

T2 = """
5
3 3 3 3 3
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
0
"""


def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g)


def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii")


input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

if sys.argv[-1] == "-t":
    print("testing")
    _test()
    sys.exit()

main()

Solution (Python):
import sys



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


def main():
    _ = int(input())
    A = [int(i) for i in input().split()]

    ans = 0
    cur = A[0]
    for a in A:
        if a >= cur:
            cur = a
        else:
            ans += cur - a

    print(ans)
    return


if __name__ == "__main__":
    main()

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

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


if __name__ == '__main__':
    resolve()

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

c = INF
B = []

tmp = A[0]

for i, a in enumerate(A):
    if i == 0:
        tmp = A[0]
        B.append(tmp)
    else:
        if tmp >= A[i]:
            B.append(tmp)
        else:
            tmp = A[i]
            B.append(tmp)

print(sum(B) - sum(A))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
A_max = 0
for a in A:
    A_max = max(A_max, a)
    ans += abs(A_max - a)
print(ans)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
A = inpl()

ans = 0
mini = 0
for i in range(n):
    if A[i] < mini:
        ans += mini - A[i]
    mini = max(A[i],mini)

print(ans)

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

ans = 0
prev = a[0]
for e in a:
  if prev - e > 0:
    ans += prev - e
  else:
    prev = e

print(ans)

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

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

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

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

tmp_top = A[0]
ans = 0

for A_i in A[1:]:
    if A_i > tmp_top:
        tmp_top = A_i
    else:
        ans += tmp_top - A_i

print(ans)

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

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

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

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

ans = 0
highest = a[0]

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

print(ans)

Solution (Python):
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations

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

    res = 0

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

if __name__ == '__main__':
    main()

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

maxh=A[0]
stepsum=0

for i in range(1, N):
	step = max(maxh - A[i],0)
	maxh = step + A[i]
	stepsum += step
print(stepsum)

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

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

Solution (Python):


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

    total = 0 
    pre_a = As[0]
    for i in range(1, N):
        diff = pre_a - As[i]
        if diff > 0:
            
            total += diff
            As[i] += diff
        else:
            
            pass
        pre_a = As[i]

    print(total)


if __name__ == "__main__":
    solve()

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


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    ans = 0
    h = A[0]
    for a in A:
        if h > a:
            ans += h - a
        h = max(h, a)
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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

max_a = 0
ans = 0
for i in range(n):
    if max_a < a[i]:
        max_a = a[i]
    else:
        ans += max_a - a[i]

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)


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


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Solution (Python):
N,*A = map(int,open(0).read().split())
a_max = A[0]
ans = 0
for a in A:
  if a_max > a:
    ans += a_max - a
  elif a_max < a:
    a_max = a
print(ans)

Solution (Python):


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

ans=0

a0=A[0]
for a in A[1:]:
    if a < a0:
        ans+=a0-a
    else:
        a0=a

print(ans)

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

if __name__ == '__main__':
    main()

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

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

print(sum_step)

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

    ans = 0
    for i in range(N-1):
        if A[i] <= A[i+1]:
            continue
        else:
            diff = A[i] - A[i+1]
            ans += diff
            A[i+1] = A[i]
    
    print(ans)
    
if __name__ == "__main__":
    main()

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip.split())
def main():
    n = I()
    a = LI()
    bemax = a[0]
    ans = 0
    for i in a[1:]:
        if i > bemax:
            bemax = i
        elif i < bemax:
            ans += bemax - i
    print(ans)
main()

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

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

if __name__ == "__main__":
    main()

Solution (Python):

MOD = 10 ** 9 + 7
INFI = 10**10

import math
from collections import deque
import itertools
import heapq

from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint




def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default




def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    
    n=int(input())
    a=list(map(int,input().split()))
    maxheight=0
    total=0
    for i in range(n):
        if a[i]<maxheight:
            total+=maxheight-a[i]
        maxheight=max(a[i],maxheight)
    print(total)




if __name__ == "__main__":

    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

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









ans = 0
max = 0

for a in A:
    if a >= max:
        max = a
    else:
        ans += max-a

print(ans)

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

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

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

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

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

X = [0]*N
maxtall = 0
total = 0
for i in range(N):
    if A[i] > maxtall:
        maxtall = A[i]
    else:
        total += maxtall - A[i]
print(total)

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

Solution (Python):
import math
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors


n = ini()
a = inl()
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()))

step = 0

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

print(step)

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

s = 0
hi = a[0]
for ai in a:
    if hi > ai:
        s += (hi - ai)
    else:
        hi = ai
print(s)

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

ans = 0
before = A[0]

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

print(ans)

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

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

Solution (Python):
from math import ceil
def mlt(): return map(int, input().split())


x = int(input())
s = list(mlt())

res = 0

for n in range(1, x):
    if s[n] < s[n-1]:
        d = s[n-1]-s[n]
        res += d
        s[n] += d

print(res)

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

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
som = 0
m = l[0]
for i in range(1,len(l)):
    if l[i] >= m:
        m = l[i]

    else:
        som = som+(m-l[i])

print(som)

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

Solution (Python):
n = int(input())
*a, = map(int, input().split())
newa=[]
latest=0
for i in range(n):
    if a[i]>latest:
        latest=a[i]
    newa.append(latest)
ans=[newa[i]-a[i] for i in range(n)]
print(sum(ans))

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

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

print(ans)

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

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

Solution (Python):

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

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

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

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

current_max = 0

answer = 0
for a in A:
    if a < current_max:
        answer += current_max - a
    current_max = max(current_max, a)

print(answer)

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

print(ans)

Solution (Python):
def main():
    n = int(input())
    arr = list(map(int,input().split()))
    total = 0
    max_val = 0
    for i in arr:
        if i < max_val:
            total += max_val-i

        max_val = max(max_val,i)

    print(total)

main()

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

for i in range(N-1):
  if a[i] <= a[int(i+1)]:
    pass
  else:
    b = b + (a[i] - a[int(i+1)])
    a[int(i+1)] = a[i]

print(b)

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

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

before = A[0]
total = 0
for i in range(1,N):
    dif = A[i] - before
    if dif>=0:
        before = A[i]
    else:
        total -= dif
print(total)

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

t = a[0]
ans = 0

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

print(ans)

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

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

print(ans)

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

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

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

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

before=0
ans=[]
for i in A:
  ans.append(max(0,before-i))
  before=i+ans[-1]
print(sum(ans))

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

Solution (Python):

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

Solution (Python):
import sys
import math
import itertools
import collections
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()


NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

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

    highest = 0

    step_sum = 0

    for a in A:
        if highest < a:
            highest = a
        else:
            step_sum += highest - a
    print(step_sum)


if __name__ == '__main__':
    main()

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

temp = alist[0]
count = 0
for a in alist[1:]:
    if a < temp:
        count += temp - a
    elif a > temp:
        temp = a

print(count)

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

print(ans)

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

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

print(s)

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

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

M = A[0]
ans = 0

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

print(ans)

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

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

print(p)

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

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

print(fumifumi)

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]:
        diff = A[i] - A[i+1]
        total += diff
        A[i+1] += diff

print(total)

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

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
aaa = list(map(int, input().split()))
now = aaa[0]
acc = 0
for a in aaa:
    if a >= now:
        now = a
    else:
        acc += now - a
print(acc)

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

Solution (Python):
"""
    こんにちは。これはクシャルです
    
    Author : thekushalghosh (クシャル)
    Team   : CodeDiggers
 
    I prefer Python language over the C++ language :p :D
        
    Visit my website : thekushalghosh.github.io
"""
import sys,math,cmath,time
start_time = time.time()


    

    
def solve():
    n = inp()
    a = inlt()
    q = 0
    w = 0
    for i in range(len(a)):
        if a[i] < q:
            w = w + q - a[i]
        else:
            q = a[i]
    print(w)
    


 
def main():
    global tt
    if not ONLINE_JUDGE:
        sys.stdin = open("input.txt","r")
        sys.stdout = open("output.txt","w")
    t = 1
    for tt in range(t):
        solve()
    if not ONLINE_JUDGE:
        print("Time Elapsed :",time.time() - start_time,"seconds")
        sys.stdout.close()
    

def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    return(input().strip())
def invr():
    return(map(int,input().split()))
 

def counter(a):
    q = [0] * max(a)
    for i in range(len(a)):
        q[a[i] - 1] = q[a[i] - 1] + 1
    return(q)

def counter_elements(a):
    q = dict()
    for i in range(len(a)):
        if a[i] not in q:
            q[a[i]] = 0
        q[a[i]] = q[a[i]] + 1
    return(q)

def string_counter(a):
    q = [0] * 26
    for i in range(len(a)):
        q[ord(a[i]) - 97] = q[ord(a[i]) - 97],1
    return(q)
 
def factors(n):
    q = []
    for i in range(1,int(n ** 0.5) + 1):
        if n % i == 0: q.append(i); q.append(n // i)
    return(list(sorted(list(set(q)))))
 
def prime_factors(n):
    q = []
    while n % 2 == 0: q.append(2); n = n // 2
    for i in range(3,int(n ** 0.5) + 1,2):
        while n % i == 0: q.append(i); n = n // i
    if n > 2: q.append(n)
    return(list(sorted(q)))

def transpose(a):
    n,m = len(a),len(a[0])
    b = [[0] * n for i in range(m)]
    for i in range(m): 
        for j in range(n): 
            b[i][j] = a[j][i]
    return(b)


ONLINE_JUDGE = __debug__
if ONLINE_JUDGE:
    input = sys.stdin.readline
main()

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

stand = 0
minimum = A[0]
for a in A[1:]:
    if a < minimum:
        stand += minimum - a
    else:
        minimum = a
print(stand)