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

Solution (Python):
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter



input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    
    
    
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    
    return X.flatten().tolist()[0]

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        
        
        
        
        

def sosu(n):
    so = [1 for i in range(n+1)]
    so[0], so[1] = 0, 0
    for i in range(2, n+1):
        if so[i]:
            for j in range(2, n//i):
                so[i * j] = 0

    return so

def solve():
    n = getN()
    nums = getList()
    ans = 0
    tmp = 0
    for num in nums:
        if num < tmp:
            ans += tmp - num
        if num > tmp:
            tmp = num

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    
    solve()

Solution (Python):
import collections as cc
import sys
import bisect as bi
I=lambda:list(map(int,input().split()))
n,=I()
l=I()
curmax=l[0]
ans=0
for i in range(1,n):
	if l[i]<curmax:
		ans+=abs(l[i]-curmax)
	else:
		curmax=l[i]
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline


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

    ans = 0
    prev_a = 0
    for a in A:
        if prev_a <= a:
            prev_a = a
        else:
            ans += (prev_a - a)

    print(ans)


if __name__ == "__main__":
    main()

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

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

ans = 0
prev = 0
for x in a:
    if prev > x:
        ans += prev - x
    else:
        prev = x
print(ans)

Solution (Python):
N, *A = map(int, open(0).read().split())
cur_max = 0
ans = 0
for a in A:
    if a < cur_max:
        ans += cur_max - a
    cur_max = max(cur_max, a)
print(ans)

Solution (Python):
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007

inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

def bo(i):
    return ord(i)-ord('a')

file = 1



    
def solve():


    
    

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








        
if __name__ =="__main__":

    if(file):

        if path.exists('input.txt'):
            sys.stdin=open('input.txt', 'r')
            sys.stdout=open('output.txt','w')
        else:
            input=sys.stdin.readline
    solve()

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    m = A[0]
    s = 0
    for i in range(1,len(A)):
        a = A[i]
        if a < m:
            s += m-a
        else:
            m = a
    print(s)
    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):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  

N = I()
A = LI()

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

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

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


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

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

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

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

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

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

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

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

def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

ans  = 0
n = int(ri())
a = Ri()

maxx = -1
for i in a:
    if  i >= maxx:
        maxx = i
    else:
        ans+= (maxx- i)
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

    ans = 0
    current = A[0]
    for a in A[1:]:
        if a >= current:
            current = a
        else:
            ans += (current - a)

    print(ans)


    

if __name__ == '__main__':
    main()

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

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

    elif A[i] > maxtall:
        maxtall = A[i]

print(ans)

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

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

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

print(ans)

Solution (Python):


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

front_height = A[0]

ret = 0

for a in A[1:]:
    if a < front_height:
        ret += front_height - a
    front_height = max(front_height, a)

print(ret)

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

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

print(res)

Solution (Python):

import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

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

if __name__ == "__main__":
    main()

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

print(ans)

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

ret = 0
min_val = 0

for item in a:
    if item >= min_val:
        min_val = item
    else:
        ret += (min_val - item)

print(ret)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

n=II()
aa=MI()
pa=0
ans=0
for a in aa:
    cur=max(0,pa-a)
    ans+=cur
    pa=a+cur
print(ans)

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

max_ = a[0]
sum_ = 0
for i in a[1:]:
    if i >= max_:
        max_ = i
    else:
        sum_ += abs(max_ - i)

print(sum_)

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


def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    ma = 0
    ans = 0
    for i in range(n):
        if i != 0:
            ans += max(0, ma-a[i])
        ma = max(ma, a[i])
    
    print(ans)
    
if __name__ == "__main__":
    main()

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = [int(x) for x in input().strip().split()]
    ans = 0
    p = 0
    for a in A:
        if p <= a:
            p = a
        else:
            ans += (p - a)
    print(ans)

if __name__ == '__main__':
    main()

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
h = 0
c = 0
for ai in a:
    if ai < h:
        c += h - ai
    h = max(ai, h)
print(c)

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

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

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

print(ans)

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

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  N=I()
  A=LI()

  ans=0
  st=A[0]
  for x in A[1:]:
    if st>x:
      ans+=st-x
    else:
      st=x

  return ans


print(main())

Solution (Python):
from math import gcd

from math import factorial as f

from math import ceil, floor, sqrt
import math

import bisect
import re
import heapq

from copy import deepcopy
import itertools
from itertools import permutations

from sys import exit

ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))

yes = "Yes"
no = "No"


def main():
    n = ii()
    a = li()
    ans = 0
    m = a[0]
    for i in range(1, n):
        ans += max(0, m - a[i])
        m = max(m, a[i])
    print(ans)


main()

Solution (Python):
pre = 0
n = int(input())
ans = 0
for i in map(int, input().split()):
    if i < pre:
        ans += pre - i
    pre = max(pre, i)
print(ans)

Solution (Python):
n = int(input())
A = tuple(map(int, input().split()))
ans = 0
cur = A[0]
for a in A[1:]:
    if a < cur:
        ans += cur - a
    else:
        cur = a
print(ans)

Solution (Python):
N = int(input())
A = tuple(map(int, input().split()))
cur_max = A[0]
ans = 0
for a in A:
    if cur_max < a:
        cur_max = a
    else:
        ans += cur_max - a
print(ans)

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

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')







def solve():
    N = II()
    A = LI()

    pre = 0
    ans = 0
    for a in A:
        if a < pre:
            sa = pre - a
            ans += sa
        else:
            pre = a
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
curMax = 0
stool = 0
for elem in a:
    if curMax > elem:
        stool += curMax - elem
    else:
        curMax = elem
print(stool)

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

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

print(res)

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

Solution (Python):

import sys
input = sys.stdin.readline

n = input().rstrip()
a = [int(item) for item in input().split()]

max_height = 0
ans = 0
for item in a:
    ans += max(0, max_height - item)
    max_height = max(max_height, item)

print(ans)

Solution (Python):
import sys
import math
import collections
import bisect
import itertools



sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20

MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))




def main():
    n = ni()
    a = na()

    ans = 0
    tmp_max = 0

    for ai in a:
        if ai > tmp_max:
            tmp_max = ai
        else:
            ans += tmp_max - ai

    print(ans)


if __name__ == '__main__':
    main()

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

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

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

x=A[0]
cnt=0
for i in range(n):
    if A[i]<x:
        cnt+=x-A[i]
    else:
        x=A[i]
print(cnt)

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

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

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

for Ai in A:
    if Ai>=M:
        M = max(M, Ai)
        continue
    
    ans += M-Ai

print(ans)

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

mx = 0
ans = 0
for e in a:
    if e < mx:
        ans += mx - e

    mx = max(mx, e)

print(ans)

Solution (Python):
def main():
    _ = int(input())
    A = [int(i) for i in input().split()]
    ma = A[0]
    ans = 0
    for a in A[1:]:
        if ma > a:
            ans += ma - a
        if ma < a:
            ma = a
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
h=0
r=0
for a in A:
    if a<h:
        r+=h-a
    h=max(h,a)
print(r)

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

now = A[0]
ans = 0
for a in A[1:]:
    if now > a:
        ans += now - a
    else:
        now = a
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 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
t = A[0]
for a in A:
    d = max(0, t-a)
    ans += d
    t = max(t,a)
print(ans)

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

Solution (Python):
def examA():
    N, X, T = LI()
    ans = (1 + (N-1)//X)*T
    print(ans)
    return

def examB():
    N = SI()
    s = 0
    for n in N:
        s += int(n)
    if s%9==0:
        ans = "Yes"
    else:
        ans = "No"
    print(ans)
    return

def examC():
    N = I()
    A = LI()
    now = A[0]
    cnt = 0
    for i in range(N):
        if now>A[i]:
            cnt += now-A[i]
        if now<A[i]:
            now = A[i]
    ans = cnt
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):

import sys
def main(N, A):
    ans = 0
    for i in range(1, N):
        if A[i] < A[i - 1]:
            t = A[i - 1] - A[i]
            A[i] += t
            ans += t
    print(ans)

if __name__ == '__main__':
    input = sys.stdin.readline
    N = int(input())
    *A, = map(int, input().split())
    main(N, A)

Solution (Python):
n = int(input())
a = [int(i) for i in 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

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 19 + 7
EPS = 10 ** -10

N = INT()
A = LIST()
mx = 0
ans = 0
for a in A:
    if a > mx:
        mx = a
    else:
        ans += abs(mx - a)
print(ans)

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


N = int(input())

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


ans = 0

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

print(ans)

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

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

print(ans)

Solution (Python):
def main():
    N = int( input())
    A = list( map( int, input().split()))
    now = 0
    ans = 0
    for a in A:
        if now <= a:
            now = a
            continue
        ans += now - a
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

INF = 10**18
eps = 10**-7

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

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

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

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

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

print(ans)

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

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

print (ans)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

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


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

def main():
    INF = 999999999999999999999999
    MOD = 10**9+7

    n,*A = get_all_int()

    ma = A[0]
    ans = 0

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

main()

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


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
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()))
result = 0
for i in range(1, N):
  if A[i-1]>A[i]:
    result += A[i-1]-A[i]
    A[i] = A[i-1]
print(result)

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

res = 0
last = 0
for a in A:
    res += max(last,a)-a
    last = max(last,a)

print(res)

Solution (Python):
n=int(input())
s=list(map(int,input().split()))
mx=0
ans=0
for i in s:
    ans+=max(0,mx-i)
    mx=max(mx,i)
print(ans)

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

ans = 0
prev = 0
for a in al:
    if prev <= a:
        prev = a
    else:
        rem = prev-a
        ans += rem

print(ans)

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

ans = 0

re = A[0]

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

print(ans)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    count = 0
    for i in range(1,N):
        if A[i]<A[i-1]:
            count += A[i-1] - A[i]
            A[i] = A[i-1]
    print(count)




if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub


import sys


buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    ans = 0
    ma = 0
    for a in A:
        ans += max(0, ma-a)
        ma = max(ma, a)
    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

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

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

Solution (Python):
N = int(input())
total = 0
h = 0
for A in map(int, input().split()):
	if h > A:
		total += h - A
	else:
		h = A
print(total)