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

Solution (Python):
'''
Created on 2020/08/22

@author: harurun
'''
def main():
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write

  N=int(pin())
  A=list(map(int,pin().split()))
  now=0
  ans=0
  for i in A:
    if now>i:
      ans+=now-i 
    else:
      now=i
  print(ans)
  return 
main()

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

Solution (Python):
import math
n=int(input())
l=list(map(int,input().split(" ")))

ma_h=0
g=0

for i in range(n):
  if ma_h>l[i]:
    g=g+ma_h-l[i]
  else:
    ma_h=l[i]
print(g)

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

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


if __name__=="__main__":
    main()

Solution (Python):
from itertools import accumulate


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

run_max = list(accumulate(a, max))


min_sum = sum([x - y for x, y in zip(run_max, a)])
print(min_sum)

Solution (Python):








    






mod = int(1e9+7)

INF = 1<<50

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

def main():
    n = int(input())
    a = readInt()
    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__':
    main()

Solution (Python):
import sys

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

ans = 0
tall = A[0]

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

print(ans)

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

a_max = [a[0]]
for i in range(1,n):
    a_max.append(max(a_max[i-1],a[i]))
print(sum(a_max)-sum(a))

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
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 S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()

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

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

fumi = 0
maxA = A[0]
for x in A:
    if x < maxA:
        fumi += maxA - x
    else:
        maxA = x
print(fumi)

Solution (Python):
import sys
import math
N = int(input())
A = [0]+list(map(int, input().split()))
ans = 0

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


print(ans)

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

Solution (Python):

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

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

Solution (Python):
n=int(input())
input_list = list(map(int,input().split()))
count = 0
temp = input_list[0]
for num in input_list:
    if num>temp:
        temp = num
    else:
        count += temp-num
print(count)

Solution (Python):
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque
from fractions import gcd
from functools import reduce
from itertools import groupby
from heapq import heapify, heappush, heappop



n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return map(lambda x: x + d, A)
def ldec(A, d=1): return map(lambda x: x - d, A)

N = n1()
A = nn()

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

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

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

lim = 0
ans = 0
for a in A:
  ans += max(0, lim - a)
  lim = max(lim, a)
  
  
print(ans)

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

ans = 0
for i in range(1,n):
    diff = max(0, a[i-1] - a[i])
    ans += diff
    a[i] += diff

print(ans)

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

tmp = lis[0]
ans = 0

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

print(ans)

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

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

a_prev = 0
ans = 0
for a in A:
    if a < a_prev:
        ans += a_prev - a
        a = a_prev
    a_prev = a

print(ans)

Solution (Python):
import sys
N = int(input())
A = [int(i) for i in input().split()]
cur = A[0]
if N == 1:
    print(0)
    sys.exit()
ans = 0
for a in A[1:]:
    if a < cur:
       ans += abs(cur-a)
    else:
        cur = a
print(ans)

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()))
min_size = A[0]
res = 0
for i in range(N):
  if A[i] < min_size:
    d = min_size - A[i]
    res += d
  elif A[i] > min_size:
    min_size = A[i]
print(res)

Solution (Python):

N=int(input())
A=list(map(int,input().split()))
h=A[0]
ans=0

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

print(ans)

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

Solution (Python):
import math
from decimal import *
import random

def pf(n):
    ans = [1]
    if(n%2==0):
        if(n!= 2):
            ans.append(2)
        while(n%2==0):
            n//=2
    p = 3
    while(n>1):
        if(n%p==0):
            ans.append(p)
            while(n%p==0):
                n//=p
        p+=1
    return ans

n =int(input())
ans = 0
arr = list(map(int, input().split()))
mx = arr[0]
for i in range(1, n):
    if(arr[i]< mx):
        ans+=(mx-arr[i])
    else:
        mx = max(mx, arr[i])
print(ans)

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

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()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

    ans = [0]*N
    p = 0

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

    print(sum(ans))

if  __name__=='__main__':
    main()

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
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 print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False



def main():
    N = I()
    A = LI()
    mx = A[0]
    ans = 0

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

    print(ans)

if __name__ == '__main__':
    main()

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

print(c)

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

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())
A=list(map(int,input().split()))
H=A[0]
ans=0
for i in range(N):
    ans+=max(0,(H-A[i]))
    H=max(H,A[i])
    
print(ans)

Solution (Python):
from collections import defaultdict,deque
import math
def main():
    n = int(input())
    A=[int(i) for i in input().split()]
    res = 0
    highest = A[0]
    for i in range(1,n):
        if A[i] < highest:
            res += A[i]-highest

        if A[i] > highest:
            highest = A[i]

    print(abs(res))

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N = I()
    T = LI()
    res = 0
    past = T[0]
    for i in range(1, N):
        now = T[i]
        if now < past:
            res += (past-now)
        else:
            past = now
    print(res)
if __name__ == "__main__":
    main()

Solution (Python):
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
A = inintl()

prev = A[0]

ans = 0

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

print(ans)

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

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

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

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

ans = 0

step = a[0]

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

print(ans)

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

for i, value in enumerate(A):
    if value > height:
        height = value
    else:
        ans += height - value
    
print(ans)

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

cnt=0
m=a[0]

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

print(cnt)

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

Solution (Python):
def LI(): return list(map(int, input().split()))
def I(): return map(int, input().split())
mod = 10**9 + 7

def main():
  n = int(input())
  a = LI()
  cnt = 0
  for i in range(1,n):
    if a[i-1] > a[i]:
      cnt += (a[i-1] - a[i])
      a[i] = a[i-1]
  print(cnt)
  
if __name__ == '__main__':
  main()

Solution (Python):


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

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

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

res = 0
pre = a[0]
for i in range(1, n):
    if pre <= a[i]:
        pre = a[i]
        continue
    res += pre - a[i]
print(res)

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

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter
import heapq
from fractions  import gcd


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

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

Solution (Python):
from sys import exit
import math
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = input()
a = li()

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

print(cnt)

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

step = 0
before_max = A[0]

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

print(step)

Solution (Python):
N=int(input())
d=input().split()
D=[int(s) for s in d]
ans=0
for i in range(1,N):
    if D[i-1]>D[i]:
        x=D[i-1]-D[i]
        ans+=x
        D[i]=D[i-1]
print(ans)

Solution (Python):
n=int(input())
l=[*map(int,input().split())]
t=l[0]
a=0
for i in l:
  if i>t: t=i
  a+=t-i
print(a)

Solution (Python):
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext

def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n = i_input()
    num_list = i_list()

    count = 0

    max_num = num_list[0]

    for i in range(1,n):
        if num_list[i] < max_num:
            count += max_num - num_list[i]
        else:
            max_num = num_list[i]

    print(count)

if __name__ == '__main__':
    main()

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

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    A = LI()
    t = A[0]
    ans = 0
    for x in A[1:]:
        if x < t:
            ans += t - x
        else:
            t = x
    print(ans)

if __name__ == '__main__':
    main()

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

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

Solution (Python):

 

 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy


 











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

tmp = A[0]
ans = 0

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

Solution (Python):
N = int(input())
li = list(map(int,input().split()))
humi = li[0]
ans = 0
for l in li[1:]:
    if l <= humi:
        ans += humi - l
    else:
        humi = l
print(ans)

Solution (Python):
def main():
    number_of_person = int(input())
    array = [int(x) for x in input().split()]
    pre_h = 0
    step_total = 0
    for height in array:
        step_total += max(pre_h - height, 0)
        pre_h = max(height, pre_h)
    return step_total


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

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

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

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

count = 0

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

print(count)

Solution (Python):
"""
    Satwik_Tiwari ;) .
    21st AUGUST , 2020  - FRIDAY
"""



from __future__ import division, print_function

from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase

from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter  
from itertools import combinations as comb 
from itertools import permutations as permutate
from bisect import bisect_left as bl


from bisect import bisect_right as br
from bisect import bisect





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


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)





from types import GeneratorType
def iterative(f, stack=[]):
  def wrapped_func(*args, **kwargs):
    if stack: return f(*args, **kwargs)
    to = f(*args, **kwargs)
    while True:
      if type(to) is GeneratorType:
        stack.append(to)
        to = next(to)
        continue
      stack.pop()
      if not stack: break
      to = stack[-1].send(to)
    return to
  return wrapped_func





mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") 
def out(var): sys.stdout.write(str(var))  
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())

def zerolist(n): return [0]*n
def nextline(): out("\n")  
def testcase(t):
    for p in range(t):
        solve()
def printlist(a) :
    for p in range(0,len(a)):
        out(str(a[p]) + ' ')
def google(p):
    print('Case 
def lcm(a,b): return (a*b)//gcd(a,b)
def power(x, y, p) :
    res = 1     
    x = x % p  
    if (x == 0) :
        return 0
    while (y > 0) :
        if ((y & 1) == 1) : 
            res = (res * x) % p

        y = y >> 1      
        x = (x * x) % p
    return res
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) :
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6
    return True



@iterative
def dfs(v,visited,st,new):
    new.append(st)
    visited[st] = 1
    for i in v[st]:
        if(visited[i] == 0):
            ok = yield dfs(v,visited,i,new)
    yield True


def solve():
    n = int(inp())
    a = lis()
    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)






testcase(1)

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

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

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

Solution (Python):
n=int(input())
a=[int(x) for x in 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):
N = int(input())
A = list(map(int, input().split()))

cnt = 0
prv = 0

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

print(cnt)

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

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

h=0
ans=0

for i in range(N):
	a=A_ls[i]
	h=max(h,a)
	ans+=h-a

print(ans)

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]
  else:
    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()))
if n==1:
    print(0)
else:
    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()))

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

print(ans)

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

ans=0

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

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

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

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

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

print(c)

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

dai = 0

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

print(dai)

Solution (Python):

n = int(input())
A = list(map(int,input().split()))
ans = 0 
temp = 0
for i in range(n):
    if A[i]<temp:
        ans += temp-A[i]
    else:
        temp = max(temp,A[i])

print(ans)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 0
max_a = 0
for ai in A:
    max_a = max(ai, max_a)
    ans += (max_a-ai)

print(ans)

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

Solution (Python):
import sys
import math

if __name__ == '__main__':
    N = int(sys.stdin.readline())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    
    n = 0
    for i in range(1, len(A)):
        if A[i-1] > A[i]:
            n += A[i-1] - A[i]
            A[i] = A[i-1]
    print(n)

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

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

print(ans)

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

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

print(ans)

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

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

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

Solution (Python):
def i():
	return int(input())
def s():
	return input()
def mp():
	return map(int,input().split())

a = input()
b = list(mp())
mx = b[0]
ans =0
for i in range(1,len(b)):
	if b[i]<mx:ans+=mx-b[i]
	else:mx = b[i]
print(ans)

Solution (Python):









def solve():
    n = int(input())
    ts = map(int, input().split())

    total = 0
    pre = 0
    for t in ts:
        d = max(pre - t, 0)
        total += d
        pre = t + d

    return total


def main():
    print(solve())


if __name__ == "__main__":
    main()

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

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

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

max_h=a[0]
ans=0

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

print(ans)