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


from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi


def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)


def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y
from math import ceil

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

Solution (Python):

n, *a = map(int, open(0).read().split())
a = [0] + a
c = 0
for i in range(n):
    t = a[i] - a[i + 1]
    if t > 0:
        a[i + 1] += t
        c += t
print(c)

Solution (Python):
import sys
from math import ceil
def input(): return sys.stdin.readline().strip()

def main():
    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)




if __name__ == "__main__":
    main()

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

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

    H = 0
    ans = 0
    for a in A:
        if H > a:
            ans += H - a
        else:
            H = a
    print(ans)


if __name__ == '__main__':
    main()

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

print(ans)

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n=int(input())
a=lnii()

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

print(ans)

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

ans = 0
m = 0
for i in a:
    ans += max(0,m-i)
    m = max(m,i)
print(ans)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=0
m=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())
X = list(map(int, input().split()))

res = 0
cur = X[0]
for v in X:
    res += max(0, cur - v)
    cur = max(cur, v)

print(res)

Solution (Python):
N = int(input())
pre = 0
ans = 0
for a in input().split():
    a = int(a)
    ans += max(pre-a, 0)
    pre = max(pre, a)
print(ans)

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


if __name__ == "__main__":
    main()

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

summ=0
before=height[0]

for i in range(N-1):
    diff=height[i+1]-before

    if(diff<0):
        summ+=abs(diff)
    else:
        before=height[i+1]

print(summ)

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

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

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

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

print(ans)

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

cur = 0
s = 0
for A in As:
    if cur > A:
        s += (cur - A)
    elif cur < A:
        cur = A

print(s)

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

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

res = 0
m = h[0]

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

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

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

MAX=A[0]
ANS=0
for i in range(1,N):
    ANS+=max(0,MAX-A[i])
    MAX=max(MAX,A[i])
print(ANS)

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

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

ans = 0
cur = A[0]

for i in range(1, len(A)):
  if cur > A[i]:
    diff = cur - A[i]
    ans += diff
    A[i] += diff
  cur = A[i]

print(ans)

Solution (Python):


def main():
    N = int(input())
    A  = [int(i) for i in input().split()]
    ans = 0
    max = A[0]
    for a in A:
        if max > a:
            ans += max-a
        else:
            max = a
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

print(ans)

Solution (Python):









def LI():
    return list(map(int,input().split()))
    
def OPEN():
    return map(int,open(0).read().split())
    

n,*a=OPEN()

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

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

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

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

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

print(ret)

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

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

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

Solution (Python):
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
A = list(map(int,input().split()))
now_max = -1
for i in range(n):
  if now_max > A[i]:
    count += now_max - A[i]
  else:
    now_max = A[i]
print(count)

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

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

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

Solution (Python):
n = int(input())
l = [int(x) for x in input().split()]
mx = 0
res = 0
for i in l:
    if i < mx:
        res += mx-i
    mx = max(i,mx)
print(res)

Solution (Python):
import sys
input = sys.stdin.readline
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):
n = int(input())
a = list(map(int, input().split()))

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

print(ans)

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


if __name__ == '__main__':
    main()

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

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

ans = 0

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

Solution (Python):
N = int(input())
A = [int(x) for x in 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())
l = [int(x) for x in input().split()]
mx = 0
res = 0
for i in l:
    if i < mx:
        res += mx-i
    mx = max(i,mx)
    

print(res)

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):
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    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],0)
    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n = int(ipt())
    a = [int(i) for i in ipt().split()]
    ma = 0
    ans = 0
    for i in a:
        if i > ma:
            ma = i
        else:
            ans += ma-i

    print(ans) 

    return None

if __name__ == '__main__':
    main()

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

Solution (Python):
n = int(input())
alst = list(map(int, input().split()))
bef_max = 0
ans = 0
for a in alst:
    ans += max(0, bef_max - a)
    bef_max = max(bef_max, a)
print(ans)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    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)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

n = ni()
a = na()

m = 0
ans = 0
for v in a:
    m = max(m, v)
    ans += m - v
print(ans)

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

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

N = int(input())
A = LI()


def main():
    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)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
A=list(map(int,input().split()))
ans=0
tmp=A[0]
for x in A[1:]:
    if x<tmp:
        ans+=tmp-x
    tmp=max(tmp,x)
print(ans)

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

Solution (Python):
def solve():
  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],A[i-1])
  return ans
print(solve())

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

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

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

print(ans)

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

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

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
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
input = sys.stdin.readline

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

def main():
    mod=10**9+7
    N=I()
    A=LI()
    ans=0
    ceil=A[0]
    for i in range(N):
        if A[i]>ceil:
            ceil=A[i]
        else:
            ans+=ceil-A[i]
            
    print(ans)

main()