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

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

A = [int(x) for x in 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()))
ans = 0
h = a[0]

for x in a:
    ans += max(0,h-x)
    h = max(h,x)
print(ans)

Solution (Python):
import sys


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

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

print(ans)

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

ans = 0
x = a[0]
for v in a[1:]:
    if v < x:
        ans += x - v
    else:
        x = v
print(ans)

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

x = A[0]
ct = 0
for a in A:
    if x < a:
        x = a
    elif x > a:
        ct += (x-a)
print(ct)

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

step_ttl = 0
step = 0
for i, a in enumerate(a_list):
  if i == 0:
    continue
  if a_list[i-1] + step < a:
    step = 0
    continue
  if a < step + a_list[i-1]:
    step = (step + a_list[i-1]) - a
  step_ttl += step

print(step_ttl)

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

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

s = 0
now = As[0]
for a in As:
    if a<now:
        s += now - a
    else:
        now = a
        
print(s)

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

Solution (Python):
n,*a=map(int,open(0).read().split())
z=a[0]
b=0
for q in a:
  if z>q:
    b+=(z-q)
  z=max(z,q)
print(b)

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

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

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

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

Solution (Python):





import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *


BUFSIZE = 8192

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 main():
    n=int(input())
    A=list(map(int,input().split()))
    mx=A[0]
    ans=0
    for a in A:
        mx=max(a,mx)
        ans+=(mx-a)
    print (ans)
if __name__ == "__main__":
    main()

Solution (Python):
n= int(input())
num=[ int (x) for x in input().split()]
pp= num[0]
eq=0
for x in range(1,n):
    if pp> num[x]:
        eq+= pp-num[x]

    else:
        pp=num[x]

print(eq)

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

Solution (Python):
import sys
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))
out = lambda x: print('\n'.join(map(str, x)))

n = ini()
a = inl()
ans = 0
tall = 0
for i in range(n):
    ans += max(0, tall - a[i])
    tall = max(tall, a[i])
print(ans)

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

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

ans = 0


front_num = A[0]

for i in range(1,n):
  if front_num > A[i]:
    dif = front_num - A[i]
    ans += dif
  else:
    
    front_num = 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()))

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

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()))
M = [0] * (N + 1)
for i in range(N):
    M[i + 1] = max(M[i], A[i])
ans = 0
for i in range(N):
    if A[i] < M[i]:
        ans += M[i] - A[i]
print(ans)

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

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

print(c_step())

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

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

print(ans)

Solution (Python):
import math

def main():
    N = int(input())
    nums = list(map(int, input().split(' ')))
    n_max = nums[0]
    total = 0
    for i in nums:
        diff = n_max-i
        if diff<0:
            n_max=i
            diff=0
        total += diff
    print(total)

if __name__=="__main__":
  main()

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

ans = 0
cur_max = 0

for x in a:
    if cur_max > x:
        ans += cur_max - x
    cur_max = max(cur_max, x)

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

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

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
max = 0
step_sum = 0
for i in range(N):
    diff = A[i] - max
    if diff <= 0:
        max = A[i] - diff
        step_sum -= diff
    else:
        max = A[i]
print(step_sum)

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

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

Solution (Python):
n, *a = map(int, open(0).read().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])
print(ans)

Solution (Python):
n = int(input())
l = [int(x) for x in input().split(" ")]
m = 0
ans = 0
for i in range(n):
  ans += max(0,m-l[i])
  m = max(m,l[i])
print(ans)

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

ans = 0
max = A[0]

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

print(ans)

Solution (Python):
n = int(input())
ans = prev = 0
for a in map(int, input().split()):
    if prev <= a:
        prev = a
        continue
    ans += prev-a
print(ans)

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

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

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

ans = 0
height = 0

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

print(ans)

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

dai=[0 for _ in range(A)]

for i, l in enumerate(B):
  if i==0:
    continue
  if B[i-1]-B[i]>0:
    dai[i]=B[i-1]-B[i]
    B[i]+=B[i-1]-B[i]

print(sum(dai))

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

mi = 0
ans = 0
for i in range(n):
    if(mi > l[i]):
        ans += (mi - l[i])
    else:
        mi = l[i]

print(ans)

Solution (Python):
if __name__=="__main__":
  N=int(input())
  x=[int(i) for i in input().split()]
  ans=0
  for i in range(1,len(x)):
    if x[i]<x[i-1]:
      
      ans+=(x[i-1]-x[i])
      x[i]+=(x[i-1]-x[i])
  print(ans)

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

step = 0
for i in range(1, len(A)):
    if A[i-1] <= A[i]:
        continue
    else:
        step += (A[i-1] - A[i])
        A[i] += (A[i-1] - A[i])

print(step)

Solution (Python):
import sys
cin = sys.stdin.readline
n = cin()
height = map(int, cin().split())
last, ans = 0, 0
for i in height:
    diff = last - i
    if diff > 0:
        ans += diff
        continue
    last = i

print(ans)

Solution (Python):
import sys
cin = sys.stdin.readline
n = int(cin())
height = map(int, cin().split())
last, ans = 0, 0
for i in height:
    diff = last - i
    if diff > 0:
        ans += diff
        continue
    last = i

print(ans)

Solution (Python):
import sys
cin = sys.stdin.readline
n = int(cin())
height = cin().split()
last, ans = 0, 0
for i in height:
    diff = last - int(i)
    if diff > 0:
        ans += diff
        continue
    last = int(i)

print(ans)

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

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

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

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

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

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

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

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

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

print(ans)

Solution (Python):
n = int(input())
heights = list(map(int, input().split()))
m = heights[0]
total = 0
for i in range(1,n):
  if heights[i] < m:
    total += (m-heights[i])
  elif heights[i] > m:
    m = heights[i]

print(total)

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

ans=0
chighest=0
for Ai in As:
    if Ai > chighest:
        chighest = Ai
    else:
        ans += chighest-Ai
print(ans)

Solution (Python):
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n = int(input())
seq = [int(x) for x in input().split()]
ans = 0
curmin = seq[0]
for i in xrange(1,n):
    if seq[i] < curmin:
        ans += curmin - seq[i]
    else:
        curmin = seq[i]
print(ans)

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

A = input().split(" ")
A = [int(n) for n in A]

ans = 0

for i in range(N): 
  if i == 0:
    max = A[i]
  elif i > 0 and max >A[i]:
    dif = max - A[i]
    ans += dif
  elif i > 0 and max < A[i]:
    max = A[i]

print(ans)

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

max_A = 0
ans = 0

for i in range(n):
    max_A = max(A[i],max_A)
    ans += max_A - A[i]

print(ans)

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

base = a[0]
ans = 0;

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

print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().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)


if __name__ == '__main__':
    solve()

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()))
stepsum = 0
for i in range(n):
  if i and a[i-1] > a[i]:
    stepsum += a[i-1] - a[i]
    a[i] = a[i] + (a[i-1] - a[i])
print(stepsum)

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

Solution (Python):
N = int(input())
lists = [int(x) for x in input().split()]
step_sum = 0

for y in range(1, N):
    if lists[y - 1] <=  lists[y]:
        continue
    else:
        s = abs(lists[y] - lists[y -1])
        lists[y] += s
        step_sum += s
        continue
        
print(step_sum)

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

count=0
count2=0

for i in range(len(A)-1):
    
    if A[i]>A[i+1]:
        count=A[i]-A[i+1]
        
        A[i+1]=A[i]
        count2+=count

print(count2)

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

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





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

print(step)

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

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

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

total = 0

front = hs[0]

for i in range(1, n):
  if front < hs[i]:
    front = hs[i]
  else:
    total = total + front - hs[i]
    
print(total)

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

a = list(map(int,input().split()))
s = 0
for i in range(len(a)-1):
    t = a[i+1] - a[i]
    if t < 0:
        s -= t
        a[i+1] -= t
print(s)

Solution (Python):
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):
N=int(input())
A=list(map(int,input().split()))

ans=mx=0
for x in A:
    mx=max(mx,x)
    ans+=mx-x

print(ans)

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

Ai = input().split()

count = 0
A = int(Ai[0])

for i in Ai:
    i = int(i)
    if A > i:
        count += A - i
    else:
        A = i
print(count)

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

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

print(ans)

Solution (Python):



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

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

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

Solution (Python):

n = int(input())
a = list(map(int, input().split( )))
s = 0
hi = 0
for i in range(n-1):
  dis = a[i] +  hi - a[i+1]
  if dis >= 0:
    hi = dis
    s += dis
  else:
    hi = 0

print(s)

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

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

ans = 0
high = 0
for i in range(1,N):
  if A[i-1] > high:
    high = A[i-1]
  if A[i] < high:
    ans += high - 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]
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())
ls = input().split()
sumh = 0
for n in range(len(ls)):
  if n == 0:
    h = int(ls[0])
  else:
    if h <= int(ls[n]):
      h = int(ls[n])
    else:
      sumh += h - int(ls[n])
print(sumh)

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

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


now = 0
total = 0

for a in A:
    if a >= now:
        now = a
        pass
    else:
        total += now - a

print(total)

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

ans = 0

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

print(ans)

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


now = 0
total = 0

for a in A:
    

    if a >= now:
        now = a
        pass
    else:
        total += now - a
    
    

print(total)

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


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

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

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

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

print(ans)

Solution (Python):
n = int(input())
alist=list(map(int,input().split()))
tempmax = alist[0]
ans = 0

for i in range(n):
    if tempmax <= alist[i]:
        tempmax = alist[i]
    else:
        ans += tempmax - alist[i]

print(ans)

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

p,t=A[0],0
for a in A:
  if p>a:
    t+=p-a
  else:
    p=a

print(t)

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

step = 0
max = 0
for i in A :
    if max < i :
        max = i
    else :
        step += max - i

print(step)

Solution (Python):
num = int(input())
item = input().split()
res = 0

for i in range(num):
  if i == 0:
    continue
  b = int(item[i - 1])
  t = int(item[i])
  
  if b > t:
    res += (b - t)
    item[i] = b
    
      
print(res)

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

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

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

res = 0
max_size = A[0]
for i in range(N):
  max_size = max(max_size, A[i])
  res += max_size - A[i]
print(res)

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

res = 0
max_size = A[0]
for i in range(N):
  max_size = max(max_size, A[i])
  d = max_size - A[i]
  res += d
print(res)

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

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

print(ans)

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

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

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

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

ans = 0
prev = A[0]

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

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

Solution (Python):






N = int(input())



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






















m = 0
tot = 0
for i in A:
    if i > m:
        m = i
    else:
        tot += m - i
print(tot)

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


if __name__ == '__main__':
    main()

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

for a in Alist:
    if a < now:
        sum1 += (now-a)
    else:
        now = a

print(sum1)

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]
		
		
print(c)

Solution (Python):
n = int(input())
heights = [int(x) for x in input().split()]
result = 0
maxSeen = float("-inf")
for h in heights:
    if h < maxSeen:
        result += maxSeen - h
    else:
        maxSeen = max(maxSeen, h)
print(result)

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

sum_num = 0
b_max = 0
for a in A:
    
    sum_num += max(b_max-a, 0)
    b_max = max(b_max, a)

print(sum_num)

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())
ls = list(map(int, input().split()))
ans = 0

for i in range(1, n):
    if ls[i-1] > ls[i]:
        ans += ls[i-1] - ls[i]
        ls[i] = ls[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):
N = int(input())
A = [int(x) for x in 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())
lst = list(map(int, input().split()))

ans = 0
m = lst[0]

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

print(ans)

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

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

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

fllor = a[0]
ans = 0
for i in range(n):
    if fllor > a[i]:
        ans += fllor - a[i]
    else:
        fllor = max(fllor, a[i])
print(ans)

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

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

print(ans)

Solution (Python):
n=int(input())
arr=[int(i) for i in input().split()]
min=arr[0];su=0
for i in range(1,n):
  if (min-arr[i]<0):
    min=arr[i]
  else:
    su+=min-arr[i]
print(su)

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] - a[i]
print(ans)

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

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

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
res = 0
pre = a[0]
for i,x in enumerate(a):
    if i == 0: continue
    if x < pre:
        res += pre-x
    else:
        pre = x
print(res)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
max_a = 0
sum_s = 0
for ai in a:
    max_a = max(max_a, ai)
    sum_s += max_a - ai
print(sum_s)

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()))
x=0
for i in range(n):
    b=0
    if i!=n-1:
        
        if a[i]>a[i+1]:
            b=a[i]-a[i+1]
            a[i+1]=a[i+1]+b
            x+=b
print(x)

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

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

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

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

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

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

def solve():
    count = 0
    tmp = A[0]
    for i in range(1, N):
        if A[i] >= tmp:
            tmp = A[i]
        else:
            count += tmp - A[i]
    return count

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

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

h_sum=0
max_h=A[0]
for n in range(1,N):
    if A[n]<=A[n-1]:
        h_sum+=A[n-1]-A[n]
        A[n]=A[n-1]
    else:
        max_h=A[n]
print(h_sum)

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

sum = 0
m = lis[0]
for i in lis:
    if m > i:
        sum += (m-i)
    else:
        m = i

print(sum)

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

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

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

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

Max_num = 0
ans = 0

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

print(ans)