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.



Sample input:
5
2 1 5 4 3


Sample output:
4

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,*a = map(int,read().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)
#print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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
  now=max(now,x)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
# sys.setrecursionlimit(1000000)

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

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

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

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

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

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

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

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy

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

    return so

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

    print(ans)

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineering College

'''
from os import path
import sys
from heapq import heappush,heappop
from functools import cmp_to_key as ctk
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input().rstrip()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
mod=1000000007
# mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]

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

file = 1



    
def solve():


    
    # for _ in range(ii()):

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








        
if __name__ =="__main__":

    if(file):

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]"):
    m = A[0]
    s = 0
    for i in range(1,len(A)):
        a = A[i]
        if a < m:
            s += m-a
        else:
            m = a
    print(s)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N = I()
A = LI()

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():

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

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


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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:



import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0

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

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

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

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

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

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

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


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

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

    print(ans)


    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

front_height = A[0]

ret = 0

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

print(ret)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ret = 0
min_val = 0

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

print(ret)

Submission status: Accepted

Solution code:
import sys

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

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

Submission status: Accepted

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

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

print(sum_)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

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

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

print(ans)

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

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

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

  return ans

# main()
print(main())

Submission status: Accepted

Solution code:
from math import gcd

from math import factorial as f

from math import ceil, floor, sqrt
import math

import bisect
import re
import heapq

from copy import deepcopy
import itertools
from itertools import permutations

from sys import exit

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

yes = "Yes"
no = "No"


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


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque, defaultdict
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

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

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

Solution code:
#C
N=int(input())
A=list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import bisect
import itertools

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
# MOD = 10 ** 9 + 7
MOD = 998244353

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


# ===CODE===

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

    ans = 0
    tmp_max = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

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

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

print(ans)

Submission status: Accepted

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

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

    mx = max(mx, e)

print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

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

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

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

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

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

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(N, A):
    ans = 0
    for i in range(1, N):
        if A[i] < A[i - 1]:
            t = A[i - 1] - A[i]
            A[i] += t
            ans += t
    print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


N = int(input())

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


ans = 0

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

print (ans)

Submission status: Accepted

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

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

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

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

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

    n,*A = get_all_int()

    ma = A[0]
    ans = 0

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

main()