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: Time Limit Exceeded

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

count = 0

for i in range(n-1):
    while True:
        if a[i] <= a[i+1]:
            break
        else:
            a[i+1] += 1
            count += 1

print(count)

Submission status: Time Limit Exceeded

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

total=0
for i in range(len(a)):
    total=total+(max(a[0:i+1])-a[i])
print(total)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
count=0
while len(a)>=2:
    ma=max(a)
    guess =a.index(ma)
    for i in range(guess+1,len(a)):
        count+=ma-a[i]
    a=a[:guess]
print(count)

Submission status: Time Limit Exceeded

Solution code:
n= int(input())
a = list(map(int, input().split()))
flag = True
res = 0
while flag:
    max_num = max(a)
    res += (a[a.index(max_num)] * len(a[a.index(max_num):]))- sum(a[a.index(max_num):])
    a = a[:a.index(max_num)]
    if len(a) == 0:
        flag = False

print(res)

Submission status: Time Limit Exceeded

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

for i in range(0,n-1):
    if max(A[0:i+1])>A[i+1]:
        x=x+((max(A[0:i+1]))-A[i+1])
    else:
        continue

print(x)

Submission status: Time Limit Exceeded

Solution code:
int_n = int(input())
list_x = list(map(int, input().split()))

#print(max(list_x))

#同じ値には、最小のインデックスを返す
#print(list_x.index(max(list_x)))

#int_plus = 0
list_x_left = list_x
list_calc_minus = list()

while len(list_x_left) != 0:
  #list_x_prosess = list_x_left[list_x_left.index(max(list_x_left)):]
  
  int_list_x_prosess_len = len(list_x_left) - list_x_left.index(max(list_x_left))
  int_max_list_x_prosess = max(list_x_left)
  list_x_left = list_x_left[:list_x_left.index(max(list_x_left))]
  
  [list_calc_minus.insert(0, int_max_list_x_prosess) for i in range(int_list_x_prosess_len)]
  
  ##  print(sum([max(list_x_prosess)-i for i in list_x_prosess]))
  #int_plus = int_plus + sum([max(list_x_prosess)-i for i in list_x_prosess])
#
  ##  print(list_x_left, list_x_prosess)
  
#print(int_plus)
#print([x - y for (x, y) in zip(list_calc_minus, list_x)])
print(sum([x - y for (x, y) in zip(list_calc_minus, list_x)]))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
count=0
while len(a)>=2:
    ma=max(a)
    guess =a.index(ma)
    for i in range(guess,len(a)):
        count+=ma-a[i]
    a=a[:guess]
print(count)

Submission status: Time Limit Exceeded

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

for i in range(N-1):
    while A[i] > A[i+1]:
        A[i + 1] = A[i + 1] + 1
        count = count + 1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = map(int, input().split())
l = list(map(int, input().split()))
from collections import deque


l = deque(l)

count=0
for i in range(len(l)-1):
    while l[i] >l[i+1]:
        l[i+1]+=1
        count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = input().split(" ")
i = 0
m = 0
d = [0] * a
while i < a :
    d[i] = int(b[i])
    i = i + 1
i = a - 1
while i > 0 :
    m = m + (max(d) - d[i])
    d.pop()
    i = i - 1
print(m)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(s) for s in input().split()]
max_index = a.index(max(a))
count = 0
last_index = len(a)
while(last_index > 0):
    for i in range(max_index,last_index):
        count += a[max_index] - a[i]
    last_index = max_index
    if last_index == 0:
        break
    max_index = a.index(max(a[:last_index]))

print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
#N, K = map(int, input().split())
# 文字列の入力
N = input()
#開業されたN個要素を受け付ける
input_list = list(map(int, input().split()))

i=1

ans = 0

while i < len(input_list):
    if input_list[i] < max(input_list[0:i]):
        tmp = max(input_list[0:i]) - input_list[i]
        ans = ans + tmp
        input_list[i] = input_list[i] + 1
    i = i+1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 0
def seek(LIST:list):
    global ans
    max_num = max(LIST)
    index = LIST.index(max(LIST))
    if(index == 0):
        ans += max_num*len(LIST)-sum(LIST)
    else:
        cal_LIST = LIST[index:]
        ans += max_num*len(cal_LIST)-sum(cal_LIST)
        new_list = LIST[0:index]
        seek(new_list)
seek(A)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
l =l[::-1]
sum=0
for j in range(len(l)):
    for i in range(len(l)-1):
        if l[i+1]>l[i]:
    ##        print("l[i]= ",l[i])
            sum+=l[i+1]-l[i]
            l[i]+=l[i+1]-l[i]

print(sum)

Submission status: Time Limit Exceeded

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




total=0
for i in range(len(a)):
    total=total+(max(a[0:i+1])-a[i])
print(total)

Submission status: Time Limit Exceeded

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

is_break = False
for i in range(len(a_list) - 1):
    if a_list[i] <= a_list[i + 1]:
        continue
    else:
        cur_i = i
        is_break = True
        break

if is_break:
    result = 0
    start_i = cur_i
    end_i = len(a_list)

    # print("start_i", start_i)
    # print("end_i", end_i)

    while True:
        a_max = max(a_list[start_i:end_i])
        # print("a_max", a_max)

        max_index = a_list.index(max(a_list[start_i:end_i]))
        # print("max_index", max_index)

        max_sum = a_max * len(a_list[max_index:end_i])
        # print("max_sum", max_sum)

        a_sum = sum(a_list[max_index:end_i])
        # print("a_sum", a_sum)

        result += max_sum - a_sum

        end_i = max_index
        # print("len", len(a_list[start_i:end_i]))
        # print("")
        if len(a_list[start_i:end_i]) == 0:
            print(result)
            break
        # break

else:
    print(0)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=0
while len(a)>0:
    if sorted(a) == a:
        print(s)
        exit()
    else:
        x=max(a)
        k=a.index(x)
        s=s+x*(len(a)-1-k)-sum(a[k+1:])
        #for i in range(k+1,len(a)):s+=x-a[i]
        a=a[:k]
print(s)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = input().split(" ")
i = 0
m = 0
d = []
while i < a :
    d.append(int(b[i]))
    i = i + 1
i = a - 1
while i > 0 :
    m = m + (max(d) - d[i])
    d.pop()
    i = i - 1
print(m)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
li = list(map(int, input().split()))
a = []
b = 0
for i in li:
  a.append(i)
  c = max(a)
  if i < c:
    b = b + c - i
print(b)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    cnt = 0
    while n > 1:
        a_max = max(a[:n])
        a_max_i = a.index(a_max)
        if a_max_i != n-1:
            cnt += (n - a_max_i) * a_max - sum(a[a_max_i:n])
        n = a_max_i
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 0
def seek(LIST:list):
    global ans
    max_num = max(LIST)
    index = LIST.index(max(LIST))
    if(index == 0):
        for i in LIST[index:]:
            ans += max_num-i
    else:
        for i in LIST[index:]:
            ans += max_num-i
        new_list = LIST[0:index]
        seek(new_list)
seek(A)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

acopy = a.copy()

'''
後ろからみて最高点より後ろは全て最高点と同じに
最高点より前でもう一度最高点を探す
'''

a_log = []
for i in range(10**4):
    h_v = np.max(a)
    h_i = np.argmax(a)

    if h_i == len(a):
      break

    a[h_i:] = np.array([h_v]*(len(a)-h_i))

    #記録
    a_log = np.concatenate([a[h_i:],a_log])

    #書き換え
    a = a[:h_i]


    if h_i == 0:
      break
  


  


print(int(np.sum(a_log - acopy)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
height = list(map(int, input().split()))
total = 0
max_number = height[0]

for i in range(1, height.index(max(height))):
	if height[i] > max_number:
		max_number = height[i]
	else:
		total += (max_number - height[i])

for i in range(height.index(max(height)), N):
	total += (max(height)-height[i])
    
print(total)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
height=list(map(int,input().split()))
humidai_list=[0]
total_height=[height[0]]
del height[0]
#リストの要素を消す
for i in height:
    if max(total_height) > i :
        humidai=max(total_height)-i
        humidai_list.append(humidai)
        total_height.append(i+humidai)
    else:
        humidai=0
        humidai_list.append(humidai)
        total_height.append(i)
print(sum(humidai_list))

Submission status: Time Limit Exceeded

Solution code:
def getFumidai(arr, idx):
  if idx == 0:
    return 0

  my_height = arr[idx]
  target = arr[0:idx]
  max_height = max(target)

  if (my_height >= max_height):
    return 0

  return max_height - my_height

n = int(input())
a = [int(i) for i in input().split(' ')]

result = 0

for i in range(n):
  result += getFumidai(a, i)

print(result)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(s) for s in input().split()]
b = a.copy()
max_index = a.index(max(a))
last_index = len(a)
while(last_index > 0):
    for i in range(max_index,last_index):
        b[i] = b[max_index]
    last_index = max_index
    if last_index == 0:
        break
    max_index = b.index(max(a[:last_index]))

count = 0
for i in range(len(a)):
    count += b[i] - a[i]
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = map(int, input().split())
l = list(map(int, input().split()))
count=0
for i in range(len(l)-1):
    while l[i] >l[i+1]:
        l[i+1]+=1
        count+=1
print(count)

Submission status: Time Limit Exceeded

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

while True:
    max_num = 0
    max_index = 0
    max_num = max(A)
    max_index = int(A.index(max_num))
    for i in range(max_index+1, len(A)):
        ans += max_num - A[i]
    del A[max_index:]

    if(max_index == 0):
        break
    else:
        pass

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
As = list(map(int, input().split()))
 
sum_humidai = 0
 
for person_idx, person_height in enumerate(As):
  
  if person_idx == 0:
    continue
    
  max_front_person = max(As[:person_idx])
  if max_front_person > person_height:
    humidai = max_front_person - person_height
    sum_humidai += humidai
 
print(sum_humidai)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
li = list(map(int, input().split()))
a = []
b = 0
for i in li:
  a.append(i)
  if i < max(a):
    b = b + max(a) - i
print(b)

Submission status: Time Limit Exceeded

Solution code:
def main():
#    N, X, T = map(int, input().split())
    N = input()
    heights = [int(s) for s in input().split()]

    sum = 0
    while True:
        max_value = max(heights)
        max_index = heights.index(max_value)

        for i in range(max_index,len(heights)):
            sum = sum + max_value - heights[i]

        if max_index == 0 :
            break

        heights = heights[0:max_index]
    print(sum)
main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    cnt = 0
    while n > 1:
        a_max = max(a[:n])
        a_max_i = a.index(a_max)
        cnt += (n - a_max_i) * a_max - sum(a[a_max_i:n])
        n = a_max_i
    print(cnt)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

while len(a)>0:
    if sorted(a) == a:
        print(s)
        exit()
    else:
        x=max(a)
        k=a.index(x)
        for i in range(k+1,len(a)):s+=x-a[i]
        a=a[:k]
print(s)

Submission status: Time Limit Exceeded

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

ans = 0
maxh = A[-1]

while 1:
    complete = True
    for i in range(1, len(A))[::-1]:
        if A[i-1] > A[i]:
            diff = A[i-1] - A[i]
            ans += diff
            A[i] += diff
            complete = False
    if complete:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N=int(input())
A=list(map(int,input().split()))
L=list()
B=0
for i in range(N):
    L.append(A[i])
    L.sort()
    B += L[i]-A[i]
print(B)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/local/bin/python3
N = int(input())
A = list(map(int, input().split()))[::-1]

ans = 0
while N != 0:
    m = max(A)
    idx = A.index(m) + 1
    ans += (len(A[:idx]) * m) - sum(A[:idx])
    A = A[idx:]
    N -= idx

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

count = [0]
sum = 0


for i in A:
    if max(count) > i:
        sum += max(count) -i
        count.append(i)
    else:
        count.append(i)

print(sum)

Submission status: Time Limit Exceeded

Solution code:
_ = input()
li = list(map(int,input().split()))
ans=0

while li!=[]:
    maxValue = max(li)
    maxLocation = li.index(maxValue)
    
    li_removed = li[maxLocation:]
    li = li[:maxLocation]
    
    for i in li_removed:
        ans = ans + (maxValue - i)
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
int_n = int(input())
list_x = list(map(int, input().split()))

#print(max(list_x))

#同じ値には、最小のインデックスを返す
#print(list_x.index(max(list_x)))

int_plus = 0
list_x_left = list_x

while len(list_x_left) != 0:
  list_x_prosess = list_x_left[list_x_left.index(max(list_x_left)):]
  list_x_left = list_x_left[:list_x_left.index(max(list_x_left))]
  
  #  print(sum([max(list_x_prosess)-i for i in list_x_prosess]))
  int_plus = int_plus + sum([max(list_x_prosess)-i for i in list_x_prosess])

  #  print(list_x_left, list_x_prosess)
print(int_plus)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = [int(x) for x in input().split()]

count = 0
max = a_list[0]

for a in a_list:
    for _ in range(n):
        if max > a:
            count += 1
            a += 1
    max = a
print(count)

Submission status: Time Limit Exceeded

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

def solve(a):
    a_max = max(a)
    idx_max = a.index(a_max)
    return ((len(a) - 1 - idx_max) * a_max) - sum(a[idx_max + 1:]), idx_max

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    while(1):
        k, idx_max = solve(a)
        ans += k
        a = a[:idx_max]
        if idx_max == 0:
            break
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

step = [0]
for i in A[1:]:
    if A[A.index(i)-1] >= i:
        step.append(A[A.index(i)-1] - i)
        A[A.index(i)] += A[A.index(i)-1] - i
print(sum(step))

Submission status: Time Limit Exceeded

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

ans = 0

while True:
    key = A.index(max(A))
    zennhann = A[:key]
    kouhann = A[key:]
    ans += max(A)*(len(A)-key) - sum(kouhann)    
    A = zennhann
    
    if len(A) == 0:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def divis(l):
    seq = []
    beg, end = 0, 0
    le = len(l)
    for i in range(1,le):
        end = i
        if l[i] < l[i-1]:
            seq.append(l[beg:end])
            beg = i
    seq.append(l[beg:end+1])
    return seq

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    a.reverse()
    cost = 0
    a = divis(a)
    a.sort()
    le = len(a)
    first = [a[i][-1] for i in range(le)]
    for i in range(le):
        for j in range(len(a[i])):
            cost += max(first[0:i+1]) - a[i][j]
    print(cost)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Time Limit Exceeded

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

gokei = 0
change = 1

def calc(lst, gokei):
    change = 0
    for i in range(N - 1, 0, -1):
        if lst[i] < lst[i - 1]:
            gokei += (lst[i - 1] - lst[i])
            lst[i] = lst[i - 1]
            change += 1

    return lst, gokei, change


while change != 0:
    A, gokei, change = calc(A, gokei)
print(gokei)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
As = list(map(int, input().split()))
 
sum_humidai = 0
 
for person_idx, person_height in enumerate(As):
  
  if person_idx == 0:
    continue
    
  max_front_person = max(As[:person_idx])
  max_person_idx = np.asarray(As[:person_idx]).argmax()
  
  if max_front_person > person_height:
    humidai = max_front_person - person_height
    As[person_idx] += humidai
    sum_humidai += humidai

print(sum_humidai)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/local/bin/python3
_ = int(input())
A = list(map(int, input().split()))[::-1]

ans = 0
while len(A) != 0:
    m = max(A)
    idx = A.index(m) + 1
    for i in A[:idx]:
        ans += m - i
    A = A[idx:]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
L=list()
B=0
for i in range(N):
    L.append(A[i])
    B += max(L)-A[i]
print(B)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

import numpy as np

A_array = np.array(A_list)

A_max_list = list()
for i in range(len(A_array)):
    max_value = np.max(A_array[:i+1])
    A_max_list.append(max_value)
    
A_max_array = np.array(A_max_list)   

print(np.sum(A_max_array - A_array))

Submission status: Time Limit Exceeded

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

count = [0]
sum = 0

for i in A:
    if max(count) > i:
        sum += max(count) -i
        count.append(i)
    else:
        count.append(i)

print(sum)

Submission status: Time Limit Exceeded

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

mintotal=0

for i in range(1,n):
  max1=max(clist[:i])
  if(clist[i]<max1):
    mintotal=mintotal+(max1-clist[i])
    
print(mintotal)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
count=0
while len(a)>=2:
    if a[0]>a[1]:
        count+=a[0]-a[1]
        w=a[0]
        a.pop(0)
        a[0]=w
    else:
        a.pop(0)
print(count)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

print(sum(steps))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
B=[0 for _ in range(N)]
C=A
for i in range(1,N):
    M=max(C[:i])
    if M>A[i]:
        B[i]=M-A[i]
        C[i]=M
print(sum(B))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
l = [0]*n

for i in range(1,n):
    while True:
        if a[i] >= a[i-1]:
            break
        else:
            a[i] += 1
            l[i] += 1
print(sum(l))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

aMax = max(a)

answer = 0
countOld = 0

for i,v in enumerate(a):
    if i != 0:
        count = 0
        while v + count < a[i-1] + countOld:
            count += 1
        answer += count
        countOld = count

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import ceil
def main():
    n, *a = map(int, read().split())
    #a2 = a[::-1]
    r = 0

    for i1 in range(1, n):
        if max(a[:i1]) > a[i1]:
            r += max(a[:i1]) - a[i1]
    print(r)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8
import sys
def input():
    return sys.stdin.readline()[:-1]
"""
N人が1列に並んでおり、前からi番目の人の身長は A_iです。
それぞれの人の足元に、高さが0以上の踏み台を設置し、全ての人が次の条件を満たすようにしたいです。
条件：踏み台を込めて身長を比較したとき、自分より前に、自分より背の高い人が存在しない
この条件を満たす時の、踏み台の高さの合計の最小値を求めてください。
"""
N = int(input())
A = list(map(int, input().split()))
out = 0
for x, a in enumerate(A):
    if x==0:
        continue
    out += max(0, max(A[:x])-a)
print(out)

Submission status: Time Limit Exceeded

Solution code:
n,*a=map(int,open(0).read().split())
count=0
for i in range(1,n):
    if i==1:
        if a[0]>a[1]:
            count+=a[0]-a[1]
    else:
        if max(a[:i])>a[i]:
            count+=max(a[:i])-a[i]
print(count)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

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

count = 0
for i, m in enumerate(nlist):
  count += max(nlist[:i+1])-m
  
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ls = list(map(int, input().split(' ')))
rst = 0
for i in range(1, N):
    while ls[i - 1] > ls[i]:
        rst += 1
        ls[i] += 1
print(rst)

Submission status: Time Limit Exceeded

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

ans = 0
a = A.index(max(A))
for i in range(1,N):
    if a < i:
        ans += max(A)-A[i]
    elif A[i-1] > A[i]:
        ans += A[i-1]-A[i]

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(1, len(A))[::-1]:
    # print(A)
    if A[i-1] > A[i]:
        diff = A[i-1] - A[i]
        for j in range(i, len(A)):
            if A[i-1] > A[j]:
                A[j] += diff
                ans += diff

# print(A)
print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

res = []
for i in range(len(A) - 1, 0, -1):
    val = A[i]
    max_val = -1
    for j in A[:i]:
        if max_val < j:
            max_val = j

    if max_val > val:
        res.append(max_val - val)
    else:
        res.append(0)

print(sum(res))

Submission status: Time Limit Exceeded

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

sum_humidai = 0

for person_idx, person_height in enumerate(As):
  
  for other_idx, other_person_height in enumerate(As[person_idx:]):
    if other_person_height < person_height:
      humidai = person_height - other_person_height
      As[person_idx + other_idx] += humidai
      sum_humidai += humidai
      
print(sum_humidai)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
A = list( int(x) for x  in input().split() )

fumidai = 0

for i in range(n):
    x_list = A[0:i+1]
    max_list = max(x_list)
    if max_list > A[i]:
        fumidai += max_list - A[i]

print(fumidai)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

for i in range(n):
    if i == 0:
        l.append(a[0])
    else:
        if max(l) <= a[i]:
            l.append(a[i])
        else:
            l.append(max(l))
            
print(sum(l) - sum(a))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def A():
    n, x, t = map(int, input().split())
    print(t * ((n+x-1)//x))

def B():
    n = list(input())
    s = 0
    for e in n:
        s += int(e)
    print("Yes" if s % 9 == 0 else "No")

def C():
    int(input())
    a = list(map(int, input().split()))
    l = 0
    ans = 0
    for e in a:
        if e < l: ans += l - e
        l = max(l, e)
    print(ans)

C()

Submission status: Accepted

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

S = []
X = 0
for i in range(N):
    if X < A[i]:
        X = A[i]
    else:
        pass
    S.append( X - A[i] )

print(sum(S))

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
tlest, total = As[0], 0
for i in range(1, N):
	tlest = max(tlest, As[i])
	if tlest - As[i]:
		total += tlest - As[i]
print(total)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
a=0
s=[]
for i in A:
  if i<a:
    s.append(a-i)
    
  else:
    a=i
print(sum(s))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
total = 0
maximum = A[0]
for a in A[1:]:
  geta = max(0, maximum - a)
  total += geta
  maximum = a + geta
#  print(a,geta)
print(total)

Submission status: Accepted

Solution code:
N = int(input())
As = input().split()

highest = 0
total = 0

for A in As:
  highest = max(int(A), highest)
  total += highest - int(A)

print(total)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
max=a[0]
dai=0
for i in range(n):
    #print(f"{i}回目")
    if max>a[i]:
        dai+=max-a[i]
       
    else:
        max=a[i]
    #print(max,dai)
print(dai)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
#readlines = sys.stdin.readlines
def main():
    n, *a = map(int, read().split())
    r = 0
    maxtall = a[0]
    for i1 in range(1, n):
        if a[i1] > maxtall:
            maxtall =  a[i1]
        else:
            r += maxtall - a[i1]
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
lis=list(map(int,input().split()))
lis_s=lis.copy()

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

print(sum(lis)-sum(lis_s))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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