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:
#5_numpy で最後は計算
import numpy as np

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

list_x_left = list_x
list_calc_minus = list()
 
while len(list_x_left) != 0:
  
  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([x - y for (x, y) in zip(list_calc_minus, list_x)]))

print((np.array(list_calc_minus) - np.array(list_x)).sum())

Submission status: Time Limit Exceeded

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

max_index = len(array) - 1

while(True):
    if len(array) == 0:
        break
    m = max(array)
    max_index = array.index(m)

    t = 0
    for x in range(max_index + 1,len(array)):
        t += m - array[x]
    ans += t
    del array[max_index :]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def test(N, A):
  B = 0
  mx = A[0]
  if N != 1: 
    for i in range(1,N):
      if A[i] - mx < 0:
        B = B + mx - A[i]
      mx = max(A[1:i+1])  
  return B

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

B = test(N, A)

print(B)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(input().split(" "))
a = [int(_) for _ in a]
cnt = 0
fa = [0 for i in range(len(a))]
while True:
    for k in range(1,len(a)):
        if a[k-1] > a[k]:
            a[k] = a[k] + 1
            cnt = cnt + 1
        else:
            fa[k-1] = 1
    if all(fa[0:-1]) == 1 and fa[-1] == 0:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst, reverse=True)

l_count = N
count = 0

for i in lst2:
    k_index = lst.index(i)
    
    if k_index < l_count:
        count += (l_count - k_index) * i
        l_count = k_index

for x in lst:
    count -=x
        
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.reverse()
ans = 0
for i in range(N):
    max_step = 0
    max_step = max(A[i:]) - A[i]
    if max_step > 0:
        ans += max_step
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
N = input()
tmp_list = list(map(int, input().split()))
count = 0
while True:
    max_value = max(tmp_list)
    now_ind = tmp_list.index(max_value)
    count = count + sum(list(map(lambda x: max_value - x, tmp_list[now_ind:len(tmp_list)])))
    tmp_list = tmp_list[0:now_ind]
    if now_ind == 0:
        break
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
min = 0
for i in range(N):
    if max(A[:i],default=0) - A[i] > 0:
        min = min + max(A[:i],default=0) - A[i]
print(min)

Submission status: Time Limit Exceeded

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

c = 0
hoge = []

for i in range(len(A)):
  hoge.append(max(A[0:i+1]))

print(sum(hoge)-sum(A))

Submission status: Time Limit Exceeded

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

mae = []
stepsum = 0
for i in A:
  if mae and max(mae) > i:
    stepsum += max(mae)-i
  mae.append(i)

print(stepsum)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

input = lambda: sys.stdin.readline().rstrip()

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

def solve2(a):
    idx_min = 0
    sum_a = 0
    for i in range(1, len(a)):
        if a[0] < a[i]:
            return ((i - 1) * a[0]) - sum_a, i
        else:
            sum_a += a[i]
            if i == len(a) - 1:
                return (i * a[0]) - sum_a, i + 1

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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
count = 0
for _ in range(N-1):
    while A[_] > A[_+1]:
        A[_ + 1] = A[_ + 1] + 1
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

sum=0
while 1:
    max_A=max(A)
    max_index=np.argmax(A)
    A_new=A[max_index:]
    for a in A_new:
        sum+=max_A-a
    A=A[:max_index]
    if max_index==0:
        break

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
count = 0
for i in range(N-1):
    while A[i] > A[i+1]:
        A[i + 1] = A[i + 1] + 1
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
B = A
A = np.array(A)
B = np.array(B)
emp = np.zeros(N)
Amax_ind = np.argmax(A)
emp[Amax_ind:] = max(A)
A[Amax_ind:] = 0
while True:
    subAmax_ind = np.argmax(A)
    emp[subAmax_ind:Amax_ind] = max(A)
    A[subAmax_ind:Amax_ind] = 0
    Amax_ind = subAmax_ind
    if 0 not in emp:
        break
print(int(sum(emp-B)))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
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 += 1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

current_index = 0
total = 0
for index in range(N):
  value = A[current_index]
  match_list = [i for i in A[current_index:] if i > value]
  if len(match_list) > 0:
    next_index = A.index(match_list[0])
    diff_list = list(map(lambda x: value - x, A[current_index:next_index]))
    total += sum(diff_list)
    del A[0:next_index]
  else:
    diff_list = list(map(lambda x: value - x, A[current_index:]))
    total += sum(diff_list)
    break
print(total)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
an = list(map(int,input().split()))
solve = []

while len(an) > 0:
    k = np.argmax(an)
    l = max(an)
    j = 0
    for i in range(k,N):
        j = l - an[i]
        solve.append(j)
    del an[k:N]
    N -= len(solve)

solved = sum(solve)

print(solved)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))

c=[]
for i in range(n):
    if i==0 :
        c.append(a[0])
        
    elif i>0:
        if max(c)>a[i]:
            c.append(max(c))
            
        else :
            c.append(a[i])

na=np.array(a)
nc=np.array(c)
print(sum(nc-na))

Submission status: Time Limit Exceeded

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

su = sum(array)

def func(c,ar):
    num = max(ar[0:c])
    idx = ar.index(num)
    for i in range(idx+1,c):
        ar[i] = num 
    if idx == 0:
        print(sum(ar)-su)
        return
    func(idx,ar)
    
res = func(N,array)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(1, len(A)):
  ki = max(A[0:i])
  if A[i] < ki:
    c += abs(A[i]-ki)

print(c)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=sum(A)
s=N+1
j=s

if A==sorted(A):
    print(0)
    exit()

for i in range(N):
    x=max(A[:s])
    s=A.index(x)
    A[s:j]=[x]*len(A[s:j])
    j=s
    if  A==sorted(A):
        break

print(sum(A)-ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
an = list(map(int,input().split()))
solve = []

while len(an) > 0:
    k = np.argmax(an)
    l = max(an)
    j = 0
    for i in range(k,N):
        j = l - an[i]
        solve.append(j)
    del an[k:N]
    N -= len(solve)

solved = 0
for i in range(len(solve)):
    solved += solve[i]

print(solved)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
_ = input()
li = list(map(int,input().split()))
ans=0
 
while li!=[] and li!=sorted(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:


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


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



def hd(n,a):
    l = [0]*10**4
    a_log = []
    for i in range(10**4):
        h_v = max(a)
        h_i = a.index(max(a))

        if h_i == len(a):
          break

        l[i] = h_v*(len(a)-h_i)

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

        if h_i == 0:
          break

    return sum(l)


ans = hd(n,a)

print(ans- asum)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=0
for i in range(len(A)-1):
  m=max(A[0:i+1])
  if m>A[i+1]:
    d=m-A[i+1]
    c+=d
print(c)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if i == 0:
        continue
    if max(As[0:i]) > As[i]:
        s += max(As[0:i])-As[i]
    else:
        continue

print(s)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

while li!=[] and li!=sorted(li):
    maxValue = max(li)
    maxLocation = li.index(maxValue)
    
    li_removed = li[maxLocation:]
    li = li[:maxLocation]
    
    ve = np.array(list([maxValue]*len(li_removed))) - np.array(li_removed)
    tmpVe = list([1]*len(li_removed))
    ans +=int(np.matrix(tmpVe)@(np.matrix(ve).T))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.reverse()
step = 0
ans = 0
for i in range(N):
    for j in range(i, N):
        if A[i] - A[j] > 0:
            step = A[i] - A[j]
            ans = max(ans, step)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst, reverse=True)

l_count = N
count = 0

for i in lst2:
    k_index = lst.index(i)
    
    if k_index >= l_count:
        continue
    else:
        count = count + (l_count - k_index) * i
        l_count = k_index

print(count-sum(lst))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst, reverse=True)

l_count = N
count = 0

for i in lst2:
    k_index = lst.index(i)
    
    if k_index >= l_count:
        continue
    else:
        count = count + (l_count - k_index) * i
        l_count = k_index
    if k_index == 0:
        break

print(count-sum(lst))

Submission status: Time Limit Exceeded

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


acopy = a.copy()

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

def hd(n,a):
    ans = 0
    a_log = []
    for i in range(10**4):
        h_v = max(a)
        h_i = a.index(max(a))

        if h_i == len(a):
          break

        ans += h_v*(len(a)-h_i)

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

        if h_i == 0:
          break

    return ans


ans = hd(n,a)

print(ans- sum(acopy))

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))
count = 0
last_index = len(a)
while(last_index > 0):
    b[max_index: last_index] = [a[max_index]] * (last_index - max_index)
    last_index = max_index
    
    if last_index == 0 or len(set(a[:last_index])) <= 1:
        break
    max_index = a.index(max(a[:last_index]))

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

Submission status: Time Limit Exceeded

Solution code:


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


acopy = a.copy()

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

def hd(n,a):
    ans = 0
    a_log = []
    for i in range(10**4):
        h_v = max(a)
        h_i = a.index(max(a))

        if h_i == len(a):
          break

        ans += h_v*(len(a)-h_i)

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

        if h_i == 0:
          break

    return ans


ans = hd(n,a)

print(ans- sum(acopy))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
l=[]
dx=0
for i in A:
    l.append(i)
    if i==max(l):
        dx+=0
    else:
        dx+=(max(l)-i)
print(dx)

Submission status: Time Limit Exceeded

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

for i in range(1,n):
  for j in range(i):
    hl.append(a[j])
  if a[i]>=a[i-1]:
    list.append(0)
  else:
    mn=max(hl)
    tmp=mn-a[i]
    list.append(tmp)
    
print(sum(list))

Submission status: Time Limit Exceeded

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

max=max(A)
if A[0] == max:
  total=0
  for i in range(len(A)):
    total = total + max - A[i]
  print(total)
  exit()

ans=0
total=0
b=len(A)
a=1

while a!=0:
  max=0
  for i in range(b):
    if max < A[i]:
      a=i
      max=A[i]
  for i in range(a+1,b):
    total += max - A[i]
  b=a

print(total)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=sum(A)
s=N+1
j=s
sum_A=0

if A==sorted(A):
    print(0)
    exit()

for i in range(1,N):
    x=max(A[:s])
    s=A.index(x)
    A[s:j]=[x]*len(A[s:j])
    j=s
    if s==0:
        
        break
print(sum(A)-ans)

Submission status: Time Limit Exceeded

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

int_sum = 0
list_calc_minus_2 = list()

#print([max(list_x[0:len(list_x)-i]) for i in range(len(list_x))])

for i in range(len(list_x)):
  #list_calc_minus_2.insert(0, max(list_x[0:len(list_x)-i]))
  int_sum = int_sum + max(list_x[0:len(list_x)-i]) - list_x[len(list_x)-i-1]
#print(list_calc_minus_2)

#print(sum([x - y for (x, y) in zip(list_calc_minus_2, list_x)]))
print(int_sum)

Submission status: Time Limit Exceeded

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

def func(c,result):
    num = max(array[0:c])
    idx = array.index(num)
    result += (-sum(array[idx+1:c]) + (num*(c-idx-1)))
    if idx == 0:
        print(result)
        return
    func(idx,result)
    
res = func(N,0)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

_ = 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]
    
    ve = np.array(list([maxValue]*len(li_removed))) - np.array(li_removed)
    tmpVe = list([1]*len(li_removed))
    ans +=int(np.matrix(tmpVe)@(np.matrix(ve).T))

print(ans)

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):
    if l[i] >l[i+1]:
        c=l[i] -l[i+1]
        l[i+1]+=c
        count+=c

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
max_A=max(A)
l=[]
dx=0

for i in A:
    l.append(i)
    if i==max(l):
        dx+=0
    else:
        dx+=abs(max(l)-i)    
print(dx)

Submission status: Time Limit Exceeded

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

for x in reversed(range(1,len(array))):
    m = max(array)

    if m > array[x]:
        ans += m - array[x]
    array.pop(-1)

print(ans)

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))
count = 0
last_index = len(a)
while(last_index > 0):
    b[max_index: last_index] = [a[max_index]] * (last_index - max_index)
    last_index = max_index
    if last_index == 0:
        break
    max_index = a.index(max(a[:last_index]))

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# n = 2 * (10 ** 5)
# # a_list = [random.randrange(10 ** 9) for i in range(n)]
# a_list = [i for i in range(n)]
# a_list = a_list[:-1]

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)
    count = 0

    for i in range(len(a_list[start_i:end_i])):
        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:
            break

        # break
    print(result)

else:
    print(0)

Submission status: Time Limit Exceeded

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

def func(c,result):
    num = max(array[0:c])
    idx = array.index(num)
    result += (-sum(array[idx:c]) + (num*(c-idx)))
    if idx == 0:
        print(result)
        return
    func(idx,result)
    
res = func(N,0)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
aN = list(map(int, input().split()))
sum_board = 0
for i in range(1,n):
    if aN[i] < max(aN[:i]):
        sum_board +=  max(aN[:i]) - aN[i]

print(sum_board)

Submission status: Time Limit Exceeded

Solution code:
import copy
N = int(input())
lst = list(map(int, input().split()))
lst2 = sorted(lst, reverse=True)

j = 0.1
l_count = N
count = 0

for i in lst2:
    k_index = lst.index(i)
    
    if k_index >= l_count:
        continue
    else:
        count = count + (l_count - k_index) * i
        l_count = k_index
        j = i
    if k_index == 0:
        break

print(count-sum(lst))

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

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

def hd(n,a):
    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

    return a_log


a_log = hd(n,a)

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

Submission status: Time Limit Exceeded

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


list_calc_minus_2 = list()

#print([max(list_x[0:len(list_x)-i]) for i in range(len(list_x))])

for i in range(len(list_x)):
  list_calc_minus_2.insert(0, max(list_x[0:len(list_x)-i]))
  
#print(list_calc_minus_2)

print(sum([x - y for (x, y) in zip(list_calc_minus_2, list_x)]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
height = []
height = input().split()
goukei = 0
count = 0
takasa = []
for i in range(n):
    takasa.append(int(height[i]))
    count = 0
    while(i!=0 and takasa[i]<=takasa[i-1]-1):
        takasa[i] += 1
        count += 1
    goukei += count

print(goukei)

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):
    if l[i] >l[i+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 = list(map(int, input().split()))
c = 0

for i, d in enumerate(b):
    if i == 0:
        continue
    h = b[:i]
    h.sort()
    if h[-1] > d:
        f = h[-1] - d
        b[i] += f
        c += f

print(c)

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 = 0
        max_index = 0
        temp_sum =0
        for i in range(0, len(heights)):
            if max_value < heights[i]:
                max_value = heights[i]
                max_index = i
                temp_sum = 0
            temp_sum= temp_sum + heights[i]

        sum = sum + (len(heights) - max_index ) * max_value - temp_sum
        if max_index == 0 :
            break

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys

sys.setrecursionlimit(10**6)

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

acopy = a.copy()

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

x = set([i for i in range(7*1000)])

a_log = np.array([])
for i in x:
    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())
a=list(map(int,input().split()))
s=0
if sorted(a) == a:
    print(s)
    exit()
while len(a)>0:
    x=max(a)
    k=a.index(x)
    s=s+x*(len(a)-1-k)-sum(a[k+1:])
    a=a[:k]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import io
import sys
import math
import itertools
N = int(input())
A = list(map(int, input().split()))

Springboard = []
for i in range(1,len(A)):
  # 自分より前に自分より身長が高い人がいないとき
  if max(A[:i+1]) == A[i]:
    Springboard.append(0)
    # 自分より前に自分より身長が高い人がいるとき
  else:
    Springboard.append(max(A[:i]) - A[i])

print(sum(Springboard))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
height = []
height = input().split()
goukei = 0
count = 0
takasa = []
for i in range(len(height)):
    takasa.append(int(height[i]))
    count = 0
    while(i!=0 and takasa[i]<=takasa[i-1]-1):
        takasa[i] += 1
        count += 1
    goukei = goukei + count

print(goukei)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
N = int(input())
Ai = input().split()
Ai_i = list(map(int, Ai))
now_ind = -1
tmp_list = Ai_i
count = 0
while True:
    max_value = max(tmp_list)
    now_ind = tmp_list.index(max_value)
    count = count + sum(list(map(lambda x: max_value - x, tmp_list[now_ind:len(tmp_list)])))
    tmp_list = tmp_list[0:now_ind]
    if now_ind == 0:
        break
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
v = max(d)
while i > 0 :
    k = 0
    m = m + (v - d[i])
    if d[i] == max(d) :
        k = k + 1
    d.pop()
    if k == 1 :
        v = max(d)
    i = i - 1
print(m)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys

sys.setrecursionlimit(10**6)

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

acopy = a.copy()

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

x = set([i for i in range(10000)])

a_log = np.array([])
for i in x:
    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:
a = int(input())
b = list(map(int, input().split()))
c = 0

for i, d in enumerate(b):
    if i == 0:
        continue
    flag = False
    f = 0
    h = b[:i]
    h.sort(reverse=True)
    for z in h:
        if z > d:
            flag = True
            f = z - d
            break
    if flag:
        b[i] += f
        c += f

print(c)

Submission status: Time Limit Exceeded

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

x=[0]
for i in range(N-1):
    if max(A[:i+1])<=A[i+1]:
        x.append(0)
    else:
        x.append(max(A[:i+1])-A[i+1])

print(sum(x))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
min = 0
M = [0] * N
for i in range(N) :
    for j in range(i):
        if M[i] > A[i]- A[j] :
            M[i] = A[i]- A[j]
    min = min + abs(M[i])
print(min)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

import numpy as np

B_list = A_list.copy()

A_list = [np.max(A_list[0:i+1]) for i in range(1, len(A_list)) ]

A_list = [B_list[0]] + A_list

A_array = np.array(A_list)
B_array = np.array(B_list)

print(np.sum(A_array - B_array))

Submission status: Time Limit Exceeded

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

current_index = 0
total = 0
for index in range(N):
  value = A[current_index]
  match_list = [i for i in A[current_index:] if i > value]
  if len(match_list) > 0:
    next_index = A.index(match_list[0])
    diff_list = list(map(lambda x: value - x, A[current_index:next_index]))
    total += sum(diff_list)
    current_index = next_index
  else:
    diff_list = list(map(lambda x: value - x, A[current_index:]))
    total += sum(diff_list)
    break
print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    max_height_idx = 0
    continue

  if an >= max_height:
    max_height = an
    max_height_idx = i
    continue

  max_height = max(a[max_height_idx:i])
  result += (max_height - an)

print(result)

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:
# input here
_INPUT = """\
5
3 3 3 3 3
"""

def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    Springboard = []
    for i in range(1,len(A)):
    # 自分より前に自分より身長が高い人がいないとき
        if max(A[:i+1]) == A[i]:
            Springboard.append(0)
     # 自分より前に自分より身長が高い人がいるとき
        else:
            Springboard.append(max(A[:i]) - A[i])

    print(sum(Springboard))

if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools

    # sys.stdin = io.StringIO(_INPUT)
    main()

Submission status: Time Limit Exceeded

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

ans = 0
maxV = max(list)
for i in reversed(range(0,len(list))):
    if i != 0:
        sl = list[:i]
        maxV = max(sl)
        if list[i] < maxV:
            ans += maxV - list[i]

print(ans)

Submission status: Time Limit Exceeded

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

max=max(A)
 
if A[0] == max:
  total=0
  for i in range(len(A)):
    total=total+max-A[i]
  print(total)
  exit()

ans=0
total=0
b=len(A)
a=1

while a!=0:
  max=0
  for i in range(b):
    if max < A[i]:
      a=i
      max=A[i]
  for i in range(a,b):
    total += max - A[i]
  b=a

print(total)

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

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

x = 10**4

a_log = np.array([])
for i in range(x):
    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())
a = [int(i) for i in input().split(' ')]
result = 0
max_height = 0

for i in range(n):
  an = a[i]
  if i == 0:
    max_height = an
    continue

  if an >= max_height:
    max_height = an
    continue

  max_height = max(a[0:i])
  result += (max_height - an)

print(result)

Submission status: Time Limit Exceeded

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

ans = 0
memo = defaultdict(lambda: 0)

for i in range(1, len(A))[::-1]:
    memo[A[i]] += 1
    
    for h in list(memo.keys()):
        if (A[i-1] > h):
            diff = A[i-1] - h
            ans += diff * memo[h]
            # print(h, memo[h], diff)
            memo[h+diff] = memo[h]
            memo[h] = 0
            

    # print(memo)

print(ans)