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: Wrong Answer

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)
    for i in a:
        for j in range(len(i)-1):
            cost += i[-1] - i[j]
    print(cost)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n=int(input())

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

t=0
x=0

for i in range(n):
  if t>l[i]:
    x+=t-l[i]
    t=l[i]
    
print(x)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

n = int(input())
A = [int(a) for a in input().strip().split()]

pre = A[0]
s = 0
for a in A:
    ad = max(0, a-pre)
    pre = a
    s += ad
print(s)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())
As = list(map(int, input().split()))
h = 0
for i in range(n - 1):
  d = As[i + 1] - As[i]
  if d > 0:
    h += d
print(h)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import ceil
def mlt(): return map(int, input().split())


x = int(input())
s = list(mlt())

res = 0

for n in range(1, x):
    if s[n] < s[n-1]:
        d = s[n-1]-s[n]
        res += d
        s[n] += 1

print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
prev = a[0]
for e in a:
  if e - prev > 0:
    ans += e - prev
  prev = e

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())

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

t=0
x=0

for i in range(n):
  if t>l[i]:
    t+=t-l[i]
    x+=t-l[i]
    
print(x)

Submission status: Wrong Answer

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

for i in range(len(a_list) - 1):
  if a_list[i] < a_list[i+1]:
    score += a_list[i+1] - a_list[i]
    
print(score)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
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


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


# n*(n-1)*...*(l+1)*l
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

# Union Find
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+1]-a[i]
        ans += tmp
        a[i+1] += tmp
print(ans)

Submission status: Wrong Answer

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

ans=0
high=A[0]

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

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
m=l[0]
c=0
for i in range(1,n):
    if(l[i]<m):
        c=max(c,m-l[i])
    m=max(l[i],m)
print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
from collections import Counter, deque, defaultdict
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)

def main():
    from builtins import int, map
    N = int(input())
    A = list(map(int, input().split()))
    count = 0
    for i in range(1, N):
        diff = A[i - 1] - A[i]
        if diff < 0:
            count += abs(diff)
    print(count)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
As = list(map(int, input().split()))
mx = 0
prev = -1
ans = 0
for a in As:
    if prev < a:
        mx = a
    else:
        ans += mx - a
    prev = a
print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def MAP1()  : return map(lambda x:int(x)-1,input().split())
def LIST()  : return list(MAP())
def LIST1() : return list(MAP1())

def solve():
    n = INT()
    a = LIST()
    ans = 0
    for i in range(1, n):
        ans += max(0, a[i]-a[i-1])
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n=input()

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

ma=max(l)
mi=min(l)

print(int(ma-mi))

Submission status: Wrong Answer

Solution code:
N = int(input())
pre = 0
ans = 0
for a in input().split():
    a = int(a)
    ans += max(a-pre, 0)
    pre = max(pre, a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools as fts
import itertools as its
import math
import sys

INF = float('inf')


def solve(N: int, A: "List[int]"):
    return sum(max(A[:i+1]) for i in range(N)) - sum(A)


def main():

    sys.setrecursionlimit(10 ** 6)

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(f'{solve(N, A)}')


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

diff, diffm = 0,0

for i in range(len(A)-1):

  diff = max(A[i:]) - A[i]

  if diff > diffm: diffm = diff

print(diffm)

Submission status: Time Limit Exceeded

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

diff, diffm = 0,0

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(400000)

def find_ans(s,lo,hi):
    if hi-lo<=1 :
        return(0)
    j=0
    Max=-10
    for i in range(lo,hi):
        if s[i]>=Max :
            j=i
            Max=s[i]
    
    ans=0
    for i in range(j+1,hi):
        ans+=(Max-s[i])
    
    ans+=find_ans(s,lo,j) 
    return(ans)

n=int(input())
s=list(map(int,input().split()))
print(find_ans(s,0,n))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count = 0

for x in range(len(nums) - 1):
    while nums[x] > nums[x + 1]:
        nums[x + 1] += 1
        count += 1

print(count)

Submission status: Time Limit Exceeded

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

count = 0

def check(x):
    global count
    while nums[x] > nums[x + 1]:
        nums[x + 1] += 1
        count += 1

for x in range(len(nums) - 1):
    check(x)

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):
    B = A[0:i]
    x = max(B)
    if A[i] < x:
        ans += x - A[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = input()
A = input()

A = A.split()
A = [int(a) for a in A]

ANS = 0
while len(A) != 0:
    ANS = ANS + (len(A) - A.index(max(A))) * A[A.index(max(A))] - sum(A[A.index(max(A)):])
    A = A[:A.index(max(A))]

print(ANS)

Submission status: Time Limit Exceeded

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

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
##C - Step(TLE)
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N):
    ##print(i,A)
    if A != []:
        maxi = max(A)
        Idx = A.index(maxi)
        ans += maxi*(len(A) - Idx) - sum(A[Idx::])
        del A[Idx::]
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
##C - Step
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N):
    ##print(i,A)
    if A != []:
        Idx = A.index(max(A))
        ans += max(A)*(len(A) - Idx) - sum(A[Idx::])
        del A[Idx::]
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
##C - Step
N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N):
    if A:
        Idx = A.index(max(A))
        ans += max(A)*(len(A) - Idx) - sum(A[Idx::])
        del A[Idx::]
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=(int)(input())
ss = list(map(int, input().strip().split()))
humidai=[]
humidai_komi=[]
for i in range(len(ss)):
    if i == 0:
        humidai.append(0)
        humidai_komi.append(ss[0])
    else:
        maemax = max(humidai_komi)
        if maemax > ss[i]:
            diff_len = maemax - ss[i]
            humidai.append(diff_len)
            humidai_komi.append(maemax)
        else:
            humidai.append(0)
            humidai_komi.append(ss[i])

print(sum(humidai))

Submission status: Time Limit Exceeded

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

for count_a in range(len(A)):
    fumidai = 0

    if(A[count_a] >= A_after):
        A_after = A[count_a]
        fumidai_list.append(fumidai)

    elif(A[count_a] < A_after):

        while True:
            fumidai = fumidai + 1
            A[count_a] = A[count_a] + fumidai
            if(A[count_a] >= A_after):
                break

        fumidai_list.append(fumidai)

print(sum(fumidai_list))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
kk = list(map(int, input().split()))
a = sum(kk)

if N == 1:
  print(0)
else:
  for k in range (N-1):
    if kk[N-k-2] > kk[N-k-1]:
      c = kk[N-k-1:N]
      d=kk[N-k-2]
      c_=[]
      c_=[d if i<d else i for i in c]
      kk[N-k-1:N]=c_
  b = sum(kk)
  print(b-a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
kk = list(map(int, input().split()))
a = sum(kk)

if N == 1:
  print(0)
else:
  for k in range (N-1):
    if kk[N-k-2] > kk[N-k-1]:
      c = kk[N-k-1:N]
      d=kk[N-k-2]
      c_=[d if i<d else i for i in c]
      kk[N-k-1:N]=c_
  b = sum(kk)
  print(b-a)

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

print(count-sum(lst))

Submission status: Time Limit Exceeded

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

step = 0
num_min = 1000

for i in range(len(A)-1):
    if A[i] > A[i+1]:
        for j in range(A[i] - A[i+1]):
            step += 1
            A[i+1] += 1
    else: continue
    
print(step)

Submission status: Time Limit Exceeded

Solution code:
n =2 # nは入力回数
num_list = []
for i in range(n):
    num_list.append(list(map(int,input().split())))
# print(num_list)

A = num_list[1]
step = 0
num_min = 1000

for i in range(len(A)-1):
    if A[i] > A[i+1]:
        for j in range(A[i] - A[i+1]):
            step += 1
            A[i+1] += 1
    else: continue
    
print(step)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
num_list = [int(i) for i in input().split()]
count = 0

while num_list:
    max_num_index = num_list.index(max(num_list))
    for i in range(max_num_index,len(num_list)):
        count += (num_list[max_num_index]-num_list[i])
        num_list[i] = num_list[max_num_index]
    num_list.remove(num_list[max_num_index])

print(count)

Submission status: Time Limit Exceeded

Solution code:
from typing import Counter


n = int(input())
num_list = [int(i) for i in input().split()]
count = 0

while num_list:
    max_num_index = num_list.index(max(num_list))
    for i in range(max_num_index,len(num_list)):
        count += (num_list[max_num_index]-num_list[i])
        num_list[i] = num_list[max_num_index]
    num_list.remove(num_list[max_num_index])

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list = [int(i) for i in input().split()]
count = 0

for i in range(n-1):
    for j in range(i+1,n):
        if list[j] < list[i]:
            count += (list[i] - list[j])
            list[j] = list[i]

print(count)

Submission status: Time Limit Exceeded

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

ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
N=input()
A=list(map(int, input().split()))
c=0
while len(A)!=0:
  a=A.index(max(A))
  for n in A[a:]:
    c+=(max(A)-n)
  del A[a:]
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

sum = 0

for i in range(1,len(li)):
    if max(li[:i]) > li[i]:
        sum += max(li[:i]) - li[i]

print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = input()
hight = input()
arr = [int(i) for i in hight.split()]
newArr = [int(i) for i in hight.split()]
sum = 0
index = newArr.index(max(newArr))
for i in range(index,len(newArr)):
    newArr[i] = max(newArr)
for i in range(0,index):
    newArr[i] = max([newArr[j] for j in range(0,index)])
for i in range(len(arr)):
    sum+=newArr[i]-arr[i]

print(sum)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
S=0
i=N
while i>1:
    saidai=A.index(max(A[0:i]))
    S+=sum(list(map(lambda x:max(A[0:i])-x,A[saidai:i])))
    i=saidai
print(S)

Submission status: Time Limit Exceeded

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

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)):
    max_num=max(a[:i+1])
    total=total+max_num-a[i]
print(total)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in a:
    if i == 0 and max(a):
        continue
    elif i < max(a):
        ans += 1

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = []
array += map(int,input().split())
sumarray = sum(array)
step = 0
while array != []:
    rearray=array[array.index(max(array)):]
    step+=len(rearray)*max(array)
    del array[array.index(max(array)):]
ans=step-sumarray
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#C
import numpy as np

n = int(input())
a_ls = list(map(int, input().split()))
# n = 5
# a_ls = [2, 2, 2, 2, 2]
a_ls_new = []
a_ls_tmp = a_ls

while True:
  max_value = max(a_ls_tmp)
  max_index = a_ls_tmp.index(max_value)
  if max_index != 0:
    a_ls_new = a_ls_new + list(np.repeat(max_value, len(a_ls_tmp) - max_index))
    a_ls_tmp = a_ls_tmp[:max_index] 
  else:
    a_ls_new = a_ls_new + list(np.repeat(max_value, len(a_ls_tmp) - max_index))
    a_ls_tmp = a_ls_tmp[:max_index] 
    break

# print(a_ls_new)
print(sum(a_ls_new) - sum(a_ls))

Submission status: Time Limit Exceeded

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

# n = 5
# a_ls = [2, 2, 2, 2, 2]
humi_ls = []

for i in range(n):
  humi = a_ls[i] - max(a_ls[0:i+1])
  humi_ls = humi_ls + [abs(humi)]

print(sum(humi_ls))

Submission status: Time Limit Exceeded

Solution code:
# AtCoder Beginner Contest 176
# C - Step

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

ans=0

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())#整数
A = list(map(int, input().split()))#配列Int型

ans = 0

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# dp[i] := i番目の人が選ばれたときの踏み台

def main():
    # INF = 10 ** 9
    N = int(input())
    A = list(map(int, input().split()))

    # initialize
    dp = [0] * (N + 10)
    dp[0] = A[0]

    res = 0
    for i in range(1, N):
        for j in range(10**9):
            tmp = A[i] + j
            if dp[i-1] <= tmp:
                dp[i] = tmp
                res += j
                break
    print(res)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
# dp[i] := i番目の人が選ばれたときの踏み台

def main():
    # INF = 10 ** 9
    N = int(input())
    A = list(map(int, input().split()))

    # initialize
    dp = [0] * (N + 10)
    dp[0] = A[0]

    res = 0
    for i in range(1, N):
        for j in range(10**9):
            if dp[i-1] <= (tmp := A[i] + j):
                dp[i] = tmp
                res += j
                break
    print(res)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = []
a = 0
b = N
ans = 0
import bisect as bi
while(a < b):
    B = A[a:b]
    x = B.index(max(B))
    for i in range(x+1,b):
        ans += max(B) - B[i]
    b = x

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input().rstrip())
line = input().rstrip().split(" ")
aimae = int(line[0]) #比較用Ai-1
counter = 0
 
#counterlistに数字を入れる
for i in range(N - 1):
  ai = int(line[i + 1]) #Ai
  while ai < aimae:
    ai += 1
    counter += 1
  aimae = ai
 
print(counter)

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

print(count-sum(lst))

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] += 1
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = []
a = 0
b = N
ans = 0
import bisect as bi
while(a < b):
    B = A[a:b]
    x = bi.bisect_left(B,max(B))
    for i in range(x+1,b):
        ans += max(B) - B[i]
    b = x

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
N = input()
tmp_list = list(map(int, input().split()))
A = tmp_list
count = 0
while True:
    max_value = max(tmp_list)
    now_ind = tmp_list.index(max_value)
    A[now_ind:len(tmp_list)] = list(map(lambda x: max_value - x, tmp_list[now_ind:len(tmp_list)]))
    #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(sum(A))

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 += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
def max_sub(list):
  max_value = max(list)
  max_index = list.index(max_value)
  return max_value,max_index

N=int(input())
xs=list(int(x) for x in input().split())
max_index=len(xs)
sum=0
while max_index>0:
  xs0=[]
  for i in range(max_index):
    xs0.append(xs[i])
  max_value,max_index0 = (max_sub(xs0))
  for i in range(max_index0,max_index):
    sum+=max_value-xs[i]
  max_index=max_index0
print(sum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = list(map(int, input().split()))
ans = 0
next = 1
a = True
while (a):
    a = False
    b = False
    for i in range(next,len(array)):
        if array[i] < array[i-1]:
            array[i] = array[i] + 1
            ans += 1
            a = True
            if b == False:
                b= True
                next = i

print(ans)

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 0 in emp:
    subAmax_ind = np.argmax(A)
    emp[subAmax_ind:Amax_ind] = max(A)
    A[subAmax_ind:Amax_ind] = 0
    Amax_ind = subAmax_ind
print(int(sum(emp-B)))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = list(map(int, input().split()))
ans = 0
next = 1
a = True
while (a):
    a = False
    for i in range(next,len(array)):
        if array[i] < array[i-1]:
            array[i] = array[i] + 1
            ans += 1
            a = True

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
an = list(map(int, input().split()))
dan=0
for i in range(n-1):
  while an[i]>an[i+1]:
    an[i+1]=an[i+1]+1
    #print(an[i])
    dan=dan+1
print(dan)

Submission status: Time Limit Exceeded

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

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

print(sum(A)-ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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:
_ = 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_removed.sort()
    li = li[:maxLocation]
    maxLocation2 = li_removed.index(maxValue)
    li_calculate = li_removed[:maxLocation2]
    
    for i in li_calculate:
        ans = ans + (maxValue - i)
        
print(ans)

Submission status: Time Limit Exceeded

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

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