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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(1,N):
    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()))
s=0
ans=0
for i in range(n):
    if a[i]>s:
        s=a[i]
    else:
        ans+=s-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):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





N = getN()
A = getList()
now = A[0]
ans = 0
for i in range(1, N):
    if now <= A[i]:
        now = A[i]
    ans += now - 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:
        ans+=m-i
    else:
        m=i
print(ans)

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

max = An[0]
count = 0

for i in range(n):
    if An[i]<max:
        count += max - An[i]
    else:
        max = An[i]
print(count)

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

Solution (Python):


import os,sys
from io import BytesIO, IOBase

def main():
    n = int(input())
    arr = list(map(int,input().split()))
    maxi = arr[0]
    ans = 0
    for i in range(1,n):
        if arr[i] < maxi:
            ans += maxi-arr[i]
        maxi = max(maxi,arr[i])
    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
check = A[0]
for i in range(1,N):
    if A[i] < check:
        ans += check-A[i]
        A[i] =check
    check = 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):
"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""


import sys
from functools import lru_cache, cmp_to_key
from collections import defaultdict as dd, deque, Counter as C
from bisect import bisect_left as bl, bisect_right as br, bisect
from heapq import heapify, heappop, heappush
from math import ceil, log, floor, sqrt
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+end)
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def L(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]


n = int(input())
arr = L()
answer = 0
for i in range(1, n):
    if arr[i] < arr[i-1]:
        answer += arr[i-1]-arr[i]
        arr[i] = arr[i-1]
out(answer)

Solution (Python):
N = int(input())
A = [int(T) for T in input().split()]
Step = 0
for TN in range(1,N):
    if A[TN-1]>A[TN]:
        Step += A[TN-1]-A[TN]
        A[TN] = A[TN-1]
print(Step)

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

res = 0
m = 0
for a in arr:
    m = max(m, a)
    res += max(m-a, 0)

print(res)

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=abs(A[i]-A[i+1])
        A[i+1]+=plus
        ans+=plus

print(ans)

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

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

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

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

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())
arr = [int(j) for j in input().split()]

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

print(ans)

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

lmax = a[0]
ans = 0
for i in a:
    ans += max(0, lmax-i)
    lmax = max(lmax, i)
print(ans)

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

before = 0
sum = 0
for a in a_array:
    if before > a:
        sum += before - a
    else:
        before = a

print(sum)

Solution (Python):
import sys
import math

n=int(input())
a=[int(o) for o in input().split()]
s=0
for i in range(n-1):
    if a[i+1]<a[i]:
        s+=(a[i]-a[i+1])
        a[i+1]=a[i]
print(s)

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

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

sum = 0
tallest = 0
for t in l:
    if t < tallest:
        sum += tallest - t
    else:
        tallest = t
print(sum)

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

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

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

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

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

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

print(ans)

Solution (Python):
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
import copy
n = int(input())


a = list(map(int, sys.stdin.readline().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
current_max = 0
for ai in a:
    current_max = max(current_max, ai)
    ans += current_max - ai

print(ans)

Solution (Python):

import sys


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


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)]  
    print(solve(N, A))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    
    main()

Solution (Python):
N = int(input())
A = [int(x) for x 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):
import sys
input = lambda: sys.stdin.readline().rstrip()

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

ans = 0
minstep = a[0]

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

print(ans)

Solution (Python):
def resolve():
    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)
    
resolve()

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

Solution (Python):



import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline




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

prev = p[0]
ans = 0
for p in p[1:]:
    diff = max(prev - p, 0)
    
    ans += diff
    prev = p + diff

print(ans)

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

pre_max = A_list[0]
res = 0
for a in A_list:
    if a > pre_max:
        pre_max = a
    else:
        res += pre_max - a

print(res)

Solution (Python):
import sys

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))

pre = A[0]

res = 0
for a in A[1:]:
    if pre > a:
        res += pre - a
    else:
        pre = a

print(res)

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array





sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    ret, max_h = 0, A[0]
    for n in range(N):
        if max_h > A[n]:
            ret += max_h - A[n]
            max_h = A[n] + (max_h - A[n])
        else:
            max_h = max(max_h, A[n])
    print(ret)


if __name__ == '__main__':
    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]
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]
    else:
        continue
print(ans)

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

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

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

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

print(sm)

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

curmax = 0
ans = 0

for i in range(n):
    if curmax <= a[i]:
        curmax = a[i]
    else:
        ans += (curmax - 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())
arr = list(map(int,input().split()))
cur = arr[0]
ans = 0
for i in arr:
    if i>=cur:
        cur = i
    else:
        ans+=abs(i-cur)
print(ans)

Solution (Python):
import math
import pprint
import fractions
import collections
import itertools
from decimal import *
from collections import deque
from bisect import *
N=int(input())
A=list(map(int,input().split()))
cnt=0
for i in range(1,N):
    if A[i-1]>A[i]:
        cnt=cnt+(A[i-1]-A[i])
        A[i]=A[i-1]
print(cnt)

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
l = A[0]

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

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

c = 0
for i in range(1, N):
    d = A[i] - A[i - 1]

    if d >= 0:
        continue
    else:
        c += abs(d)
        A[i] = A[i - 1]

print(c)

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

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

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=0
_=A[0]
for a in A:
  if _<a:
    _=a
  else:
    ans+=(_-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):
from sys import stdin
rs = stdin.readline
ri = lambda : int(rs())
ril = lambda : list(map(int, rs().split()))

def main():
    n = ri()
    A = ril()
    h = 0
    ans = 0
    for a in A:
        dh = max(h-a, 0)
        h = a + dh
        ans += dh
    print(ans)

if __name__ == '__main__':
    main()

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

ans = 0
height_need = 0

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

print(ans)

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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

mx = None
ans = 0
for a in As:
    if mx is None:
        mx = a
    else:
        if a >= mx:
            mx = a
        else:
            ans += mx - a
print(ans)

Solution (Python):
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 6)

mod = 1000000007
inf = int(1e18)

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]


def inverse(a):
    return pow(a, -1, mod)


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

main()

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

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

print(ans)

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

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

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

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

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

Solution (Python):
import sys


def _i(): return int(sys.stdin.readline().strip())


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


def main():
    n = _i()
    cur, *a = _ia()
    ans = 0
    for ai in a:
        if ai < cur:
            ans += (cur - ai)
        else:
            cur = ai
    return ans


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

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

Solution (Python):


import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    m = 0
    ans = 0
    for ai in a:
        ans += max(0, m - ai)
        m = max(m, ai)
        
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math
import copy


MOD = 10 ** 9 + 7



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] - A[i]

print(s)

Solution (Python):

import os
import sys
from io import BytesIO, IOBase




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


class union_find:
    def __init__(self, n):
        self.n = n
        self.rank = [0]*n
        self.parent = [int(j) for j in range(n)]
    
    def union(self,i,j):
        i = self.find(i)
        j = self.find(j)
        if self.rank[i] == self.rank[j]:
            self.parent[i] = j
            self.rank[j] += 1

        elif self.rank[i] > self.rank[j]:
            self.parent[j] = i
        else:
            self.parent[i] = j

    def find(self, i):
        temp = i
        if self.parent[temp] != temp:
            self.parent[temp] = self.find(self.parent[temp])
        return self.parent[temp]
from math import log2, ceil
from collections import deque, Counter as CC
def main():

    
        
        n = int(input())
        

        l = [int(j) for j in input().split()]
        tmp = l[0]
        ans = 0
        for i in range(1,n):
            if(l[i]<tmp):
                ans+=tmp-l[i]
            else:
                tmp = l[i]
        print(ans)



        

            

        




        
        
        



        



if __name__ == "__main__":
    main()

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

def solve(arr,n):
    if arr==sorted(arr):
        return 0
    ans=0
    m=arr[0]
    for i in range(1,n):
        if arr[i]<m:
            ans+=m-arr[i]
        else:
            m=arr[i]
    return ans
print(solve(arr,n))

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

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

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

INF = float("inf")


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

Solution (Python):
import sys, math
import io, os





def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def out(var) : sys.stdout.write(str(var)+'\n')
from decimal import Decimal


INF = float('inf')
mod = int(1e9)+7

n=int(data())
h=mdata()
m=0
ans=0
for i in h:
    m = max(m, i)
    ans+=m-i
out(ans)

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

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

for i in range(n):
    if s > a[i]:
        ans += s - a[i]
    else:
        s = a[i]
print(ans)

Solution (Python):
import sys


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    forward = 0
    for i in range(n):
        if a[i] < forward:
            ans += forward - a[i]
        else:
            forward = a[i]
    print(ans)


if __name__ == "__main__":
    main()

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

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N=ii()
    A=list(mi())

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

    print(ans)

        
        






if __name__ == "__main__":
    main()

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

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

Solution (Python):
(N,) = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = 0
mn = A[0]
for i in range(N):
    ans += max(0, mn - A[i])
    mn = max(mn, A[i])
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 

from decimal import *



N = INT()
A = LIST()

ans = 0
tmp = A[0]

for a in A[1:]:
	ans += max(0, tmp-a)
	tmp = max(tmp, a)

print(ans)

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

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

if __name__ == '__main__':
    solve()

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

mx = a[0]

ans = 0

for x in a:
    if x < mx:
        ans += (mx - x)
    else:
        mx = x 
print(ans)

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

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

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):
import sys
input = sys.stdin.buffer.readline
N = int(input())
A = list(map(int, input().split()))

v = A[0]
ans = 0
for a in A:
    if v > a:
        ans += v - a
    if v < a:
        v = a
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()))

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

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

ans = 0
for i in range(len(A)):
  if i == 0:
    continue
  else:
    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()))
num=0
ans=0
for i in range(N):
    if A[i]>num:
        num=A[i]
    ans+=num-A[i]
print(ans)