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:
#N=int(input())
#A=list(map(int,input().split()))

N=200000
A=[i for i in range(1,200001)]

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)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int,input().split()))
smaller = min(l)
smaller_index=l.index(smaller)
taller = max(l)
taller_index = l.index(taller)
largesmall = l[0]
stool=0
if(all(elem == l[0] for elem in l)):
    print(0)
else:
    for i in range(smaller_index+1):
        if(largesmall>l[i]):
            largesmall=l[i]
    for j in range(smaller_index+1):
        m=largesmall-smaller
        if(largesmall!=l[j]):
            l[j]+=m
            stool+=1
    for k in range(taller_index+1,len(l)):
        b=taller-l[k]
        l[k]+=b
        stool+=1
    print(stool+1)

Submission status: Wrong Answer

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

ind = a.index(m)
for i in range(n):

    if i == n-1:
#        print(i)
        break
    else:
        if a[i]>a[i+1] and i < ind:
            s += a[i]-a[i+1]
#            print(i,":",s)

        elif a[ind]>=a[i] and i >= ind:
            s += a[ind]-a[i+1]
#            print(i,":",s)
#        else:
#            print(i,a[i],a[i+1],":",s)
 
print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())
A_high_list = list(map(int,input().split()))
B_list = [A_high_list[0]]
Fumidai = 0
s = 0

for n, m in enumerate(A_high_list):
    if B_list[n] > A_high_list[n]:
        s = max(B_list) - int(A_high_list[n])
        Fumidai += s
        B_list.append(A_high_list[n])
    else:
        B_list.append(A_high_list[n])
        continue

print(Fumidai)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

print(s)

top = s[0]
count = 0

for x in s :
    if x <= top :
        count = count + top - x
    else :
         top = x

print(str(count))

Submission status: Wrong Answer

Solution code:
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

# N 人が 1 列に並んでおり、前から i 番目の人の身長は Ai です。
# それぞれの人の足元に、高さが 0 以上の踏み台を設置し、全ての人が次の条件を満たすようにしたいです。
# 条件：踏み台を込めて身長を比較したとき、自分より前に、自分より背の高い人が存在しない
# この条件を満たす時の、踏み台の高さの合計の最小値を求めてください。

n = I()
a_l = LI()
ans = [0]

print(a_l)

for i in range(n-1):
    diff = a_l[i + 1] - a_l[i]
    a = 0
    if diff < 0:
        a_l[i + 1] = a_l[i + 1] + abs(diff)
        a = abs(diff)
    ans.append(a)
print(sum(ans))

Submission status: Wrong Answer

Solution code:
lst = []
count = 0
n = int(input())
lst = list(map(int, input().split(' ')[:n]))
for j in range(len(lst)-1):
    if lst[j]<=lst[j+1]:
        count = count + (lst[j+1]-lst[j])
    else:
        count = count + 0
print(count)

Submission status: Wrong Answer

Solution code:
n = input()
l = [int(i) for i in input().split()]
max = 0
sum = 0
print(l)
for i in range(1,len(l)):

  if l[i-1] > max:
    max = l[i-1]
  if l[i] < max:
    sum += max - l[i]
print(sum)

Submission status: Wrong Answer

Solution code:
N=int(input())
A = list(map(int,input().split()))
target=A[0]
cost=0
for v in range(N-1):
    B=A[v+1]-A[v]
    if B>=0:
        target=A[v+1]
    elif B<0:
        cost+=target-A[v+1]
print(cost)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=input()
list=[int(x) for x in input().split()]
i=0
kei=0
k=0
while(i!=len(list)-1):
    if(list[i]>list[i+1]):
        kei+=list[i]-list[i+1]
        list[i+1]=list[i]
        print(kei)
    i+=1
print(kei)

Submission status: Wrong Answer

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]
    
    li_removed.sort()
    
    print(li_removed)
    
    for i in li_removed[:li_removed.index(maxValue)-1]:
        
        ans = ans + (maxValue - i)
        
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
s = 0
if N==1:
  print(0)
  exit()

for i,a in enumerate(A):
  if i==0:
    continue
  
  tmp = A[i-1] - a
  s += (tmp>0)*tmp

print(s)

Submission status: Wrong Answer

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

h_list = [0]*(2*10**5+10)

for i in range(1,n):
    if a_list[i] < a_list[i-1]:
        h_list[i] = a_list[i-1] + h_list[i-1] - a_list[i] 
    elif a_list[i] >= a_list[i-1]:
        h_list[i] = 0
        
print(sum(h_list))

Submission status: Wrong Answer

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

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

diff = []
for i in range(N - 1):
  diff.append(A[i + 1] - A[i])
    
print(sum(diff))

Submission status: Wrong Answer

Solution code:
def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  sortAs = sorted(As)
  max_a = sortAs[-1]
  for i in range(n-1, -1, -1):
    if i == 0:
      break
    if As[i] < As[i-1]:
      if(max_a not in As[0:i-1]):
        max_a = As[i-1]
      r += max_a - As[i]
  print(r)

main()

Submission status: Wrong Answer

Solution code:
n = int(input())

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

print(s)

top = s[0]
count = 0

for x in s :
    if x <= top :
        count = count + top - x
    else :
         top = x

print(count)

Submission status: Wrong Answer

Solution code:
def minimumStoolHeight(A):
    stool_tot = 0
    height_min = 0
    
    if len(A) == 1:
        return 0
    
    height_min = A[0]
    for i in range(1, len(A)):
        if A[i] < A[i-1]:
            
            if A[i-1] > height_min:
                height_min = A[i-1]
            stool_tot += (height_min - A[i])
            
            print(i, height_min, stool_tot)
          
    return stool_tot        
        
N = int(input())
A = list(map(int, input().rstrip().split()))
print(minimumStoolHeight(A))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/local/bin/python3
N = int(input())
A = list(map(int, input().split()))[::-1]
 
ans = 0
m = A[0]
s = n = 0
for i in range(N):
    if A[i] <= m:
        ans += n*m - s
        s = n = 0
    m = A[i]
    s += A[i]
    n += 1
if A[i] <= m:
    ans += n*m - s

print(ans)

Submission status: Wrong Answer

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

an =0
ki=a[0]
for i in range(n-1):
    wk=a[i]-a[i+1]
    if wk<0:
        an+=abs(wk)
        ki-=wk
print(an)

Submission status: Wrong Answer

Solution code:
#!/usr/local/bin/python3
N = int(input())
A = list(map(int, input().split()))[::-1]
 
ans = 0
m = A[0]
s = n = 0
for i in range(N):
    if A[i] <= m:
        ans += n*m - s
        s = n = 0
    m = A[i]
    s += A[i]
    n += 1
    print(f"result: {m}, {n}, {s}, {ans}")
if A[i] <= m:
    ans += n*m - s

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
s = 0
if N==1:
  print(0)
  exit()

for i,a in enumerate(A):
  if i==0:
    continue
  
  s+=(a<A[i-1])*a

print(s)

Submission status: Wrong Answer

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

if n==1:
	print(0)

for i in range(1,n):

	if a[i]<a[i-1]:
		res= ( a[i-1] - a[i] ) + res
		a[i] = a[i-1]

print(res)

Submission status: Wrong Answer

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

list_ans = []

for j in range(1,n):
    stepping_stone = a[j] - a[j-1]
    if stepping_stone > 0:
        list_ans.append(stepping_stone)
        a[j] += stepping_stone
    else:
        list_ans.append(0)

ans = sum(list_ans)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

l_count = N
count = 0

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

print(count-sum(lst))

Submission status: Wrong Answer

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

h_list = [0]*(n+10)

for i in range(1,n):
    if a_list[i] < a_list[i-1]:
        h_list[i] = a_list[i-1] + h_list[i-1] - a_list[i] 
    elif a_list[i] >= a_list[i-1]:
        h_list[i] = 0
        
print(sum(h_list))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input()
a = a.split()
count = 0

boo = False

for i in a:
    if int(i) >= n:
        boo = True

if(boo):
    print(n - 1)
else:
    print(0)

Submission status: Wrong Answer

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

ans = 0
maxV = max(list)
maxI = list.index(maxV)

sumV = sum(list[maxI:len(list)])
maxM = maxV * (len(list)-maxI)
# print(sumV, maxM)
ans += maxM - sumV

if maxI != 0:
    for i in range(0,maxI):
        if i != maxI:
            x = list[i] - list[i+1]
            if x > 0:
                ans += x
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=1000000000
for i in range(len(A)):
  base=A[i]
  cc=0
  p=8
  for t in range(0, i):
    if base<A[t]:
      p=6
      break
  if p==6:
    continue
  for t in range(i, len(A)):
    if base>A[t]:
      cc+=base-A[t]
  if c > cc:
    c = cc
print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = input().replace(" ","")
Alist = list(A)
An = list(map(int,Alist))

sum = 0

for i in range(1,N):
  if An[i-1]<=An[i]:
    continue
  else:
    sum += (An[i-1]-An[i])
    An[i] = An[i-1]
    
print(sum)

Submission status: Wrong Answer

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

h_list = [0]*(n)

for i in range(1,n):
    if a_list[i] < a_list[i-1]:
        h_list[i] = a_list[i-1] + h_list[i-1] - a_list[i] 
    elif a_list[i] >= a_list[i-1]:
        h_list[i] = 0
        
print(sum(h_list))

Submission status: Wrong Answer

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

l_count = N
count = 0

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

print(count-sum(lst))

Submission status: Wrong Answer

Solution code:
def minimumStoolHeight(A):
    stool_tot = 0
    height_min = 0
    
    if len(A) == 1:
        return A
    
    height_min = A[0]
    for i in range(1, len(A)):
        if A[i] < A[i-1]:
            
            if A[i-1] > height_min:
                height_min = A[i-1]
            stool_tot += (height_min - A[i])
            
            print(i, height_min, stool_tot)
          
    return stool_tot        
        
N = int(input())
A = list(map(int, input().rstrip().split()))
print(minimumStoolHeight(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
sa = 0
i = 0
mx = max(A)
flg = 0


while i < len(A)-1:
    if (A[i] < mx) and A[i] > A[i+1] and flg != 1:
        sa += A[i]-A[i+1]
    elif A[i] == mx:
        sa += mx-A[i+1]
        flg = 1
    elif flg == 1:
        sa += mx-A[i+1]
    i += 1


print(sa)

Submission status: Wrong Answer

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

T = 0
for i in range(N):
    if int(H[i-1]) - int(H[i]) > 0:
        T += int(H[i-1]) - int(H[i])
        H[i-1] = H[i]

print(T)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input()
a = a.split()
count = 0

boo = False

for i in a:
    if int(i) >= n:
        boo = True
    else:
        count += 1

if(boo):
    print(count)
else:
    print(0)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

list_ans = []

for j in range(1,n):
    stepping_stone = a[j] - a[j-1]
    if stepping_stone > 0:
        list_ans.append(stepping_stone)
    else:
        list_ans.append(0)

ans = sum(list_ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
max_num = 0
for num in A:
  if num > max_num:
    max_num = num

border = A.index(max_num)
height = []
before_h = A[0]
for h in A[:border]:
  if before_h <= h:
    height.append(0)
  elif before_h > h:
    height.append(before_h-h)
  before_h = h

for h in A[border:]:
  height.append(max_num-h)

print(sum(height))

Submission status: Wrong Answer

Solution code:
n = input()
s = list(map(int, input().split()))
c = 1
for i in s:
    if max(s) - i == c:
        a = i
        break
    else:
        a = 0
print(a)

Submission status: Wrong Answer

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)
    if now_ind == 0:
        break
    count = count + sum(list(map(lambda x: max_value - x, tmp_list[now_ind:len(tmp_list)])))
    tmp_list = tmp_list[0:now_ind]
print(count)

Submission status: Wrong Answer

Solution code:
addNum = 0
n = input()
s = input().split()

for i in range(len(s)-1):
    if(s[i]>s[i+1]):
        a = int(s[i])
        b = int(s[i+1])
        c =  a-b
        s[i+1] = s[i]
        addNum += c

print(addNum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(sum(list))

Submission status: Wrong Answer

Solution code:
N = input()
A = []
A = input().split()
j = 0
t = A[0]
for i in A:
    if t>i:
        j += int(t)-int(i)
    if t<=i:
        t = i
print(j)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
  n = int(input())
  As = list(map(int, input().split()))
  r = 0
  max_a = max(As)
  for a in range(n-1, -1, -1):
    if a == 0:
      break
    if As[a] < As[a-1]:
      if(max_a not in As[0:a-1]):
        max_a = As[a-1]
      r += max_a - As[a]
  print(r)

main()

Submission status: Wrong Answer

Solution code:
def stepUp(val_list):
  counter = 0

  for i in range(len(val_list) - 1):
      current = val_list[i]
      up = val_list[i + 1]

      if (current > up):
          counter += current - up

  if (val_list[len(val_list) - 2] > val_list[len(val_list) -1]):
      counter += val_list[len(val_list) - 2] - val_list[len(val_list) -1]

  return counter

def userInput():
    a_count = int(input())
    a = list(map(int, input().rstrip().split()))
    
    return stepUp(a)
  
print(userInput())

Submission status: Wrong Answer

Solution code:

n = int(input())
a = list(map(int,input().split()))
m = max(a)
s = 0

ind = a.index(m)
for i in range(n):
    if i == n-1:
        break
    else:
        if a[i]>a[i+1] and i < ind:
            s += a[i]-a[i+1]

        else:
            a[i]>a[i+1]
            s += a[ind]-a[i+1]
            
print(s)

Submission status: Wrong Answer

Solution code:
N = int(input())
members = list(map(int, input().strip().split()))
stands = []

for i, member in enumerate(members):
  if i>0:
    if members[i] > members[i-1]:
      stands.append(0)
    else:
      stands.append(members[i-1] - members[i])
      
print(sum(stands))

Submission status: Wrong Answer

Solution code:
N = input()
A = []
A = input().split()
j = 0
t = A[0]
for i in A:
    if t>i:
        j += int(t)-int(i)
    if t<i:
        t = i
print(j)

Submission status: Wrong Answer

Solution code:
# dp[i][j] := 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(N):
            # A[i-1] + h[i-1] < A[i] + h[i]
            if dp[i-1] <= (tmp := A[i] + j):
                # 条件が破れた時に一つ前の最小になるjを記録したい
                dp[i] = tmp
                res += j
                break
    print(res)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

try:
    for i in range(n):
        if As[i] > As[i + 1]:
            step = As[i] - As[i + 1]
            print(step)
            As[i + 1] += step
            print("As["+str(i + 1)+"]:"+str(As[i + 1]))
            sum += step 
except IndexError:
    pass

print(sum)

Submission status: Wrong Answer

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

keep = 0
cnt =0
x=0


for a in A:
    if keep > a:
        cnt += keep -a
        x = keep -a
        print(x)
        keep = a + x
    else:
      keep = a
    
    
print(cnt)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as  np

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

if len(set(A)) == 1:
    print(0)
else:
    max_point = int(np.where(A == maximum)[0])
    ans = [A[0]]
    for i in range(1, max_point):
        ans.append(A[0])


    for j in range(max_point+1):
        ans.append(maximum)

    print(sum(ans) - sum(A))

Submission status: Wrong Answer

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

for i in range(N):
    if A==sorted(A):
        print(i)
        exit()
    
    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: Wrong Answer

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

m = A[0]
n = 0
s = 0
ans = 0
for i in range(N):
    s += A[i]
    n += 1
    if A[i] > m:
        # print(f"max!: {m}, {n}, {s}, {ans}")
        m = A[i]
    else:
        # print(f"ans!: {m}, {n}, {s}, {ans}")
        ans += (n-1)*m - (s-A[i])
        n = 1
        s = m = A[i]
    # print(f"result: {m}, {n}, {s}, {ans}")
ans += (n-1)*m - (s-A[i])

print(ans)

Submission status: Wrong Answer

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

ans = 0
maxV = max(list)
maxI = list.index(maxV)

sumV = sum(list[maxI+1:len(list)])
maxM = maxV * (len(list)-1-maxI)
ans += maxM - sumV

if maxI != 0:
    for i in range(0,maxI):
        if i != maxI:
            x = list[i] - list[i+1]
            if x > 0:
                ans += x


print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
for i in range(N-1):
  if A[i] > A[i+1]:
    S = A[i] - A[i+1]
    K = A[i+1] + S
    A[i+1] = K
print(A)

Submission status: Wrong Answer

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**3):
        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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding=utf-8

if __name__ == '__main__':
    N = int(input())

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

    M = max(li)
    # print(M)

    MV = li[0]
    C = 0
    hai = li.index(M)
    #print(hai)
    for i in range(N):
        if i < hai:
            if MV > li[i]:
                C += MV - li[i]

        else:
            C += M - li[i]
        #print(C)

    print(C)

Submission status: Wrong Answer

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

ans = 0
step = 0

for x in range(n):
  while a[x-1] > a[x]+step:
    step += 1
  else:
    ans = ans + step
    step = 0
    
print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

ans = 0
maxV = max(list)
maxI = list.index(maxV)

for i in range(maxI+1, len(list)):
    x = maxV - list[i]
    if x > 0:
        ans += x

if maxI != 0:
    for i in range(0,maxI):
        if i != maxI:
            x = list[i] - list[i+1]
            if x > 0:
                ans += x


print(ans)

Submission status: Wrong Answer

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

keep = 0
cnt =0
x=0


for a in A:
    if keep > a:
        cnt += keep -a
        x=0
        x += keep -a
    keep = a + x
    
    
print(cnt)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
l=list(map(int,input().split()))
u=max(l)
d=0
i=0
while l[i]!=u:
  if l[i]>l[i+1] and l[i]:
    d=d+abs(l[i+1]-l[i])
    l[i+1]=l[i]
  i=i+1
r=i
for b in range(r,len(l)):
    d=d+(u-l[b])
print(d)

Submission status: Wrong Answer

Solution code:
#中身の数
n = int(input())
#リスト
a = list(map(int,input().split()))
#過去最大
p = 0
#前が使った土
q = 0

for i in range(n-1):
  #前の人との差
  e = a[i+1]-a[i]-q
  if e>0:
    if e>q:
      q = 0
    else:
      q -= e
  else:
    if p < q-e:
      p = q-e
      q = p
    else:
      q = q-e
    
  
print(p)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = input().split()
list = copy.copy(a)
ans = 0

for i in range(1,n):
    if a[i] < a[i-1]:
        a[i] = a[i-1]

for j in range(n):
    if a[j] != list[j]:
        ans += abs(int(a[j]) - int(list[j]))
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
tot = int(input())
ls = list(map(int,input().split()))
ans = 0
mx = 0
for i in range(0,tot):
    if mx < ls[i]:
        mx = ls[i]
    if i != 0:
        if ls[i] < ls[i-1]:
            ans += mx - ls[i]
print(ans)

Submission status: Wrong Answer

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

try:
    for i in range(len(As)):
        if As[i] < As[i + 1]:
            step = As[i + 1] - As[i]
            As[i] += step
            sum += step 
except IndexError:
    pass

print(sum)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

keep = 0
cnt =0
cnt2=0


for a in A:
    if keep > a:
        cnt += keep -a
        cnt2=0
        cnt2 += keep -a
    keep = a + cnt2
    
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())

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


highest = 0
ans = 0

a = a[::-1]

for i in a:
    if i > highest:
        highest = i
    ans += highest - i

print(ans)

Submission status: Wrong Answer

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

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

print(cnt)

Submission status: Wrong Answer

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

    # 前の人が大きい時はa[i-1] - a[i]分の台が必要
    for i in range(1, len(a)):
        if i == 1:
            current_h = a[i]

        if a[i - 1] > a[i]:
            current_h = max(a[i - 1], current_h)
            ans += current_h - a[i]

    print(ans)

if __name__ == '__main__':
    abc176_c()

Submission status: Wrong Answer

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