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 math

n = int(input())
aL = list(map(int, input().split(" ")))

mx = aL[0]
ans = 0
for a in aL[1:]:
    if mx > a:
        ans += mx - a
    else:
        mx = a

print(ans)

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

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




'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''
n=int(input())
a=list(map(int, input().split()))

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

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

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

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

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

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

Solution (Python):

import sys,math,collections,itertools
input = sys.stdin.readline

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

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

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

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

Solution (Python):

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

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

print(out)

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

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

print(ans)

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

n = int(input())
nums = map(int, input().split(" "))

out = 0
for i, num in enumerate(nums):
    if i == 0:
        cur_max = num
    else:
        if cur_max > num:
            out += cur_max - num
        else:
            cur_max = num

print(out)

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

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

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

print(ans)

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

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

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

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

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

Solution (Python):
import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase


"""def gcd(a,b):
    if b==0:
        return a
    else:
        return gcd(b,a%b)"""

"""def pw(a,b):
    result=1
    while(b>0):
        if(b%2==1): result*=a
        a*=a
        b//=2
    return result"""



def inpt():
    return [int(k) for k in input().split()]


def main():
    n=int(input())
    ar=inpt()
    mx=ar[0]
    ans=0
    for i in range(1,n):
        if(ar[i]<mx):
            ans+=mx-ar[i]
        mx=max(ar[i],mx)
    print(ans)

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()

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

maxh = 0
ans = 0
for h in a:
    if maxh > h:
        ans += maxh - h
    maxh = max(maxh, h)
print(ans)

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

Solution (Python):

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

cur_max = a[0]
ret = 0
for i in a[1::]:
    if i < cur_max:
        ret += cur_max - i
    else:
        cur_max = i

print(ret)

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

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())
l=list(map(int,input().split()))
def solve(n,l):
     if n==1:
        return 0
     ans=0
     m=0
     for i in range(n):
         if l[i]>=m:
            m=l[i]
         else:
            ans+=max(0,m-l[i])
     return ans
print(solve(n,l))

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 = [int(x) for x in 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()))

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

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

Solution (Python):
import sys

def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))

def main():
    n = II()
    aa = [0] + MII()
    sumv = 0
    for i in range(n):
        if aa[i] > aa[i+1]:
            delta = aa[i] - aa[i+1]
            sumv += delta
            aa[i+1] += delta
    print(sumv)

if __name__ == '__main__':
    main()

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

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

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

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

print(res)

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

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

ans = 0
bfr = a[0]

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

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

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

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    A = [int(a) for a in input().split()]
    currentH = 0
    add = 0
    for i, a in enumerate(A):
        if a >= currentH:
            currentH = a
        else:
            add += currentH - a
    print(add)

    return 0

if __name__ == "__main__":
    solve()

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

Solution (Python):

import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline


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

Solution (Python):
import sys

input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Solution (Python):


import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy
import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputintlist(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloatlist(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

def to10(A):
    ret=0
    for a in range(len(A)-1,-1,-1):
        ret+=X[a]*2**(N-1-a)
    return ret

N=int(input())
A=inputintlist()
mmax=A[0]
ans=0
for a in A[1:]:
    if mmax>a:
        ans+=mmax-a
    mmax=max(mmax,a)
print(ans)

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

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

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

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

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

Solution (Python):
def main():
    n=int(input())
    a=list(map(int,input().split()))
    a_max=a[0]
    ans=0
    for ai in a[1:]:
        if ai<a_max:
            ans+=a_max-ai
        a_max=max(a_max,ai)
    return ans

if __name__=='__main__':
    ans=main()
    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):
ans = 0
komi_max = 0

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

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

print(ans)

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

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

print(ans)

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    
    t_max = 1
    h = 0
    for ai in A:
        if ai >= t_max:
            t_max = ai
        else:
            h += t_max-ai

    print(h)

        
    

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




import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    
    
    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    ans=0
    for i in range(1,n):
        diff = arr[i-1]-arr[i]
        if diff > 0:
            ans+=diff
            arr[i]+=diff
    print(ans)
    
    

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):

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

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

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

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

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

print(cnt)

Solution (Python):
import sys
import  math
import fractions
from collections import defaultdict
import copy
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
A=nl()
ans=0
now=A[0]
for i in range(1,N):
    if(now<A[i]):
        now=A[i]
        continue
    else:
        ans+=now-A[i]

print(ans)

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

min_value = a[0]
result = 0

for x in a:
  diff = min_value - x
  if x < min_value:
    result += diff
    min_value = x + diff
  else:
    min_value = x
    
print(result)

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

h = 0
tot = 0

for a in A:
    tot += max(h-a, 0)
    h = max(h, a)
print(tot)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

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

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

    print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    A = read_ints()
    front = 0
    total = 0
    for a in A:
        if a < front:
            total += front-a
        else:
            front = a
    return total


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

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

h,res=A[0],0
for i in A:
    if i<=h:
        res+=(h-i)
    else:
        h=i
print(res)

Solution (Python):

import collections as cl
import sys


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 main():
    N = II()
    a = LI()
    ans = 0
    for i in range(1, N):
        if a[i] >= a[i - 1]:
            continue
        ans += a[i - 1] - a[i]
        a[i] = a[i - 1]

    print(ans)


main()

Solution (Python):
from math import ceil
from collections import defaultdict as dd
import sys
input=sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))
mx=0
ans=0
for i in l:
    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]<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()))

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

if __name__ == "__main__":
    main()

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

s = 0

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

print(s)

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]:
        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()))
pre = 0
ans = 0
for ai in a:
    if ai < pre:
        ans += pre - ai
    else:
        pre = ai
print(ans)

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

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

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

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

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

Solution (Python):
N=int(input())
*A,=map(int,input().split())
ans=0
now=A[0]
i=1
while i<N:
    if A[i]>now:
        now=A[i]
    else:
        ans+=now-A[i]
    i+=1
print(ans)

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

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

maxmin = -math.inf

board = 0
for i in range(n):
    a = alist[i]

    if a > maxmin:
        maxmin = a
    else:
        board += maxmin - a

print(board)

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

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


print(ans)

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

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

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

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

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

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

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

print(ans)

Solution (Python):
import math
import sys

n = int(input())
alist = list(map(int, input().split()))
num = 0
ans = 0

for i in alist:
    if num < i:
        num = i
    ans += abs(num-i)
print(ans)

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

mx = 0
ans = 0
for A in As:
    if mx >= A:
        ans += mx-A
    else:
        mx = A

print(ans)