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 = input().split()
ans = 0

current_max = 0

for i in range(len(A)):
  if int(A[i]) > current_max:
    current_max = int(A[i])
  else:
    ans += current_max - int(A[i])

print(ans)

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

A = 0
for i in range(0,N):
    A = A+L[i]

for i in range(0,N-1):
    if L[i+1]<L[i]:
        L[i+1] = L[i]
    else:
        pass

B = 0
for i in range(0,N):
    B = B+L[i]

print(B-A)

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

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

Solution (Python):
n = int(input())
strNums = input().split()
intNums = [int(i) for i in strNums]
step = []
initialValue = intNums[0]
for i in range(len(intNums)):
    if intNums[i] < initialValue:
        step.append(initialValue - intNums[i])
    else:
        step.append(0)
        initialValue = intNums[i]
print(sum(step))

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

for i in range(1,N):
  t = A[i-1]-A[i]
  if (t>=0):
    B.append(t)
    A[i] = A[i] + t
r = 0
for i in B:
  r = r + i
print(r)

Solution (Python):
N = int(input())
A_list = [int(i) for i in input().split()]
h_list = []
A_before = 0
for A in A_list:
    h = 0
    if A < A_before:
        h = A_before - A
    else:
        h = 0
    h_list.append(h)
    A_before = A + h
print(sum(h_list))

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

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

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

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

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

print(S)

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

print(sum(m))

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

Solution (Python):
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    A = read_list()
    pre = A[0]
    res = 0
    for a in A:
        res += max(0, pre - a)
        pre = max(a, pre)
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input().rstrip())
member_str_list = input().rstrip().split(' ')
member_list = []

for member in member_str_list:
    member_list.append(int(member))

step_list = [0]
for i in range(len(member_list)):
    if i - 1 >= 0:
        front_member = member_list[i-1] + step_list[i-1]
        current_member = member_list[i]
        if front_member > current_member:
            step_list.append(front_member - current_member)
        else:
            step_list.append(0)

print(sum(step_list))

Solution (Python):


import sys
import math
from decimal import Decimal, ROUND_DOWN
from collections import deque
from itertools import combinations
import copy

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        for line in sys.stdin:
            lines.append(line.strip())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    (n, ) = [int(e) for e in lines[0].split(' ')]
    a = [int(e) for e in lines[1].split(' ')]


    return (n, a)


def solve(n, a):

             
    current = a[0]   
    total = 0
    for i in range(n):

        tmp = a[i] - current
        if tmp < 0:
            total = total + (-tmp)
        
        if a[i] > current:
            current = a[i]


    return total
    


def main():
    
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
a = lr()
ans = 0
for i in range(n-1):
    if a[i+1] < a[i]:
        tmp = a[i]-a[i+1]
        ans += tmp
        a[i+1] = a[i]
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(1000000)
from collections import deque


N = int(input())

A = list(map(int, input().split()))
nmin = A[0]
su = 0
for ai in A:
    if ai < nmin:
        su += nmin - ai
    else:
        nmin = ai

print(su)

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

Amax = 0
ans = 0

for i in range(len(A)):
  if (Amax - A[i]) >= 0:
    ans += (Amax-A[i])
  elif (Amax - A[i])<0:
    Amax = A[i]
print(ans)

Solution (Python):
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    mi = 0
    le = len(a)
    cost = 0
    for i in range(le-1):
        if mi > a[i+1] or a[i] > a[i+1]:
            mi = max(a[i], mi)
            cost += mi - a[i+1]
    print(cost)
    return 0

if __name__ == "__main__":
    solve()

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

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

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

Solution (Python):
l = int(input())
xs = [int(x) for x in input().split()]
step = 0
max = 0
for x in xs:
    
    if x > max:
        max = x
        continue  
    step += max - x
print(step)

Solution (Python):



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

count = 0
i = 0


for i in range(n-1):
    if height[i] > height[i+1]:

        count += height[i] - height[i+1]
        height[i+1] += height[i] - height[i+1]


print(count)

Solution (Python):
x = int(input())
lst = list(map(int, input().split()))
i = 1
cnt = 0
a = lst[0]
if x == 1:
    print(0)
else:
    while True:
        b = lst[i]
        if b < a:
            cnt += abs(b - a)
            a = a
        else:
            a = b
        if i >= x - 1:
            break
        i += 1
    print(cnt)

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

Solution (Python):
n=int(input())
ppl=(input().split())
answer=0
if n==1:
    print(0)
for x in range(0,n-1):
    ppl[x]=int(ppl[x])
    ppl[x+1]=int(ppl[x+1])
    if ppl[x]>ppl[x+1]:
        answer+=ppl[x]-ppl[x+1]
        ppl[x+1]=ppl[x]
if(n!=1):
    print(answer)

Solution (Python):
N = int(input())
l = input().split()

cnt = 0
current = int(l[0])
for i in range(1, N):
    now = int(l[i])
    if now >= current:
        current = now
    if now < current:
        cnt += current - now

print(cnt)

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

for i in range(len(A)):
  if A[i] - old_a < 0:
    diff = (old_a - A[i])
    step += diff
  else:
    diff =0
  old_a = A[i] + diff
  
print(step)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
b=A[0]
c=0
d=len(A)

for i in range(1,d):
    if b>A[i]:
        c+=b-A[i]
    else:
        b=A[i]

print(c)

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

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

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

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

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

Solution (Python):


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


for i in range(len(a) - 1):
    if a[i + 1] < a[i]:
        l.append(a[i]-a[i + 1])
        a[i + 1] = a[i]


print(sum(l))

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


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

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

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

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

Solution (Python):
n = int(input())
a1 = input().split(" ")
an = [int(i) for i in a1]

goukei = 0
for i in range((n-1)):
    an01=an[i]-an[i+1]
    if  an01 >= 0:
        an[i+1] = an[i]
        goukei += an01

print(goukei)

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

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

Solution (Python):

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

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

s=list([0])
for i in range(N-1):
  if A[i]>A[i+1]:
    s.append(A[i]-A[i+1])
    A[i+1] = A[i]
  else:
    s.append(0)
print(sum(s))

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

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

Solution (Python):
n = int(input())
H = list(map(int, input().split()))
mh = 0
cnt = 0
for i in range(n - 1):
    mh = max(mh, H[i])
    if H[i] <= H[i + 1] and mh < H[i + 1]:
        continue
    else:
        cnt += mh - H[i + 1]
print(cnt)

Solution (Python):


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    
    
    B = [0] * N
    
    B[0] = 0
    sum = 0
    for i in range(1, N):
        if A[i-1] + B[i-1] > A[i]:
            B[i] = A[i-1] + B[i-1] - A[i]
        else:
            B[i] = 0
            
        sum = sum + B[i]

    print(sum)

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

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

ans = 0

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

print(ans)

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

lst = A[:]
B = [0]
for i in range(1, len(A)):
  if lst[i] < lst[i-1]:
	  B.append(lst[i-1] - lst[i])
	  lst[i] = lst[i-1]
  else:
    B.append(0)
    
print(sum(B))

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

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

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

sum=0

for i in range(1,len(h)):
  if h[i]-h[i-1]<0:
    sum=sum+abs(h[i]-h[i-1])
    h[i]=h[i]-(h[i]-h[i-1])

print(sum)

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

a=[int(i) for i in input().split()]
sa=0

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

Solution (Python):
import copy

N = int(input())
A = list(map(int, input().split()))
A2 = copy.copy(A)
B = [A[0]]

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

Solution (Python):
from sys import stdin,stdout
n=int(stdin.readline())
a=list(map(int,stdin.readline().split()));ans=0
for i in range(n-1):
    f=a[i];b=a[i+1]
    if b<f:
        ans+=(f-b)
        a[i+1]+=(f-b)
print(ans)

Solution (Python):



import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5
2 1 5 4 3"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5
3 3 3 3 3"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """4
3 1 1 3"""
        output = """4"""
        self.assertIO(input, output)


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


if __name__ == "__main__":
    
    resolve()

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

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

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

ans = 0
heigt = A[0]
for i in A:
    if heigt > i:
        ans += heigt - i
    else:
        heigt = i

print(ans)

Solution (Python):
n = int(input())
l1 = [0] * n
l2 = [int(n) for n in input().split()]
b = 0
for p in range(1, n):
    s = l2[b] - l2[p] + l1[b]
    if s > 0:
        l1[p] = s
    b = p
print(sum(l1))

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

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

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

print(result)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
count=0
for i in range(N):
  try:
    a = A[i]
    b = A[i+1]
    if a <= b:
      pass
    else:
      c = abs(a-b)
      A[i+1] = b+c
      count+=c
  except:
    pass
print(count)

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

sum = 0

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

  else:
    pass
print(sum)

Solution (Python):

import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from functools import lru_cache

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A_i = LI()

total_fumidai = 0
prev_num = -1
for num in A_i:
    if prev_num < 0:
        prev_num = num
        continue

    if prev_num <= num:
        prev_num = num
    else:
        total_fumidai += (prev_num - num)
print(total_fumidai)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
step = [0]
for k,i in enumerate(A[1:]):
    if A[k] >= i:
        step.append(A[k] - i)
        A[k+1] = A[k+1] + A[k] - i
print(sum(step))

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

for i in range(N):
  if A[i] > basic:
    basic = A[i]
  else:    
    count += basic - A[i]

print(count)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
count = 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]
print(count)

Solution (Python):
import sys
import math
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
n=int(input())
a=get_array()
arr=a[0]
ans=0
for i in range(n-1):
    if a[i+1]<arr:
        ans+=(arr-a[i+1])
    else:
        arr=a[i+1]
print(ans)

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

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

step = []
sum = 0

for i in range(int(N) -1):
  if A[i] > A[i+1]:
    n = A[i] - A[i+1]
    A[i+1] = A[i+1] + n
    step.append(n)
  
for i in step:
  sum = sum + i

print(sum)

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

        ans+=arr[i-1]-arr[i]
        arr[i] = arr[i - 1]
print(ans)

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

sum_org=sum(A)


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

sum_new=sum(A)


print(sum_new-sum_org)

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

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):
def readInt():
    return list(map(int, input().split()))

n = int(input())
a = readInt()
ans = 0
height = a[0]
for i in range(1,len(a)):
    if a[i] <= height:
        ans += height - a[i]
    else:
        height = a[i]
print(ans)

Solution (Python):

import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
 


from math import gcd, ceil

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

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

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

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


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


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

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

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

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

print(sum_)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
wa = 0
for x in range(len(A)):
    y = min(x+1, len(A)-1)
    z = A[x] - A[y]
    if z > 0:
        wa += z
        A[y] += z
print(wa)

Solution (Python):
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math

n=int(input())
A=INPUT()
B=[A[0]]

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

Solution (Python):
import copy
N = int(input())
Ai = list(map(int, input().strip().split()))
height = 0;
boards = copy.copy(Ai)
for i in range(len(boards)-1):
    if boards[i+1] >= boards[i]:
        continue;
    else:
        height += boards[i] - boards[i + 1]
        boards[i+1]= boards[i]
print(height)

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

B = [0]*N

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

ans = sum(B)
print(ans)

Solution (Python):




n = int(input())

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

now_high = a[0]
fumidai = 0

for _a in a:
    if now_high > _a:
        fumidai += now_high - _a
    else:
        now_high = _a

print(fumidai)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n = int(readline())
lst1 = list(map(int,readline().split()))

mx = 0
ans = 0
for i in lst1:
    if i < mx:
        ans += mx-i
    else:
        mx = max(mx,i)

print(ans)

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

B = 0
A0, *AA = A

for a in AA:
  if A0 > a:
    B += A0 - a
  else:
    A0 = a

print(B)

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



cou = 0

for i in range(1,len(A)):
    if A[i] < A[i - 1]:
        dan = A[i - 1] - A[i]
        
        cou = cou + dan
        A[i] = A[i - 1]
        

print(cou)

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

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

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

Solution (Python):
N = int(input())
A = list((int(x) for x in input().split()))

beforeAi=0
counter=0

for Ai in range(N):
    if beforeAi>A[Ai]:
        counter+=beforeAi-A[Ai]
        A[Ai]=beforeAi
    beforeAi=A[Ai]

print(counter)

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

sum = 0
for i in range(len(A)-1):
    if A[i+1]<A[i]:
        sum = sum + (A[i]-A[i+1])
        A[i+1]=A[i]

print(sum)

Solution (Python):
def Step():
    
    num = int(input())

    data = list(map(int,input().split()))
    
    count = 0
    
    fn = 0
    
    for i in range(len(data)-1):
        if (data[i+1] - (data[i] + fn)) < 0:
            count = count + abs(data[i+1] - (data[i] + fn))
            fn = abs(data[i+1] - (data[i] + fn))
    
        else:
            fn = 0
    
    print(count)
    
if __name__ == '__main__':
    Step()

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

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


before_max = a[0]
humidai = 0
humidai_sum = 0
for i in range(1,n):
    humidai = 0
    if(before_max >= a[i]):
        humidai = before_max - a[i]
        humidai_sum += humidai

    if(before_max < a[i]):
        before_max = a[i]
        

    
print(humidai_sum)

Solution (Python):


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

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


print(diff_sum)

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 = cnt + (a[i-1] - a[i])
        a[i] = a[i-1]

print(cnt)

Solution (Python):
def add_each_number(num):
  s = str(num)
  array = list(map(int, s))
  return sum(array)
 
n = int(input())
first_stand = 0
second_stand = 0
first_height = 0
second_height = 0
total = 0
height_list = map(int, input().split())
for second_height in height_list:
  if( (first_stand + first_height) > second_height ):
    second_stand = (first_stand + first_height) - second_height
  else:
    second_stand = 0
  total += second_stand
  first_stand = second_stand
  first_height = second_height
print(total)

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

step = 0
ma = max(A)
idx = A.index(ma)
for i in range(idx-1):
    dif = A[i+1]-A[i]
    if dif < 0:
        step += abs(dif)
        A[i+1] += abs(dif)
for i in range(idx,n):
    step += ma-A[i]
print(step)

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

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

print(ans)