Problem p02727 (python)

Fixed submission:
X,Y,A,B,C=map(int, input().split())
p=list(map(int, input().split()))
q=list(map(int, input().split()))
r=list(map(int, input().split()))

x=X-1
y=Y-1

p=sorted(p,reverse=True)
q=sorted(q,reverse=True)
r=sorted(r,reverse=True)

#print(p[:X],q[:Y])

if p[x]>=q[y]:
  a=q[y]
  f=1
else:
  a=p[x]
  f=2
while len(r)>=1 and a<r[0]:
#  print(a,r[0])
  if f==1 :
#    print('f=1')
    q[y]=r[0]
    y-=1
    r.pop(0)
  elif f==2 :
#    print('f=2')
    p[x]=r[0]
    x-=1
    r.pop(0)
  else:
#    print('break1')
    break
    
#  print(x,y,p[x],q[y])
    
  if (y==-1 and x>=0) or (x>=0 and p[x]<q[y]):
#    print('a=p[x]')
    a=p[x]
    f=2
  elif (x==-1 and y>=0) or (y>=0 and p[x]>=q[y]):
#    print('a=q[y]')
    a=q[y]
    f=1
  else:
#    print('break2')
    break

ans=0
#print(p[:X],q[:Y])
for i in p[:X]:
  ans+=i
for i in q[:Y]:
  ans+=i

print(ans)

Buggy submission (verdict: Runtime Error):
X,Y,A,B,C=map(int, input().split())
p=list(map(int, input().split()))
q=list(map(int, input().split()))
r=list(map(int, input().split()))

x=X-1
y=Y-1

p=sorted(p,reverse=True)
q=sorted(q,reverse=True)
r=sorted(r,reverse=True)

#print(p[:X],q[:Y])

if p[x]>=q[y]:
  a=q[y]
  f=1
else:
  a=p[x]
  f=2
while a<r[0] and len(r)>=1:
#  print(a,r[0])
  if f==1 :
#    print('f=1')
    q[y]=r[0]
    y-=1
    r.pop(0)
  elif f==2 :
#    print('f=2')
    p[x]=r[0]
    x-=1
    r.pop(0)
  else:
#    print('break1')
    break
    
#  print(x,y,p[x],q[y])
    
  if (y==-1 and x>=0) or (x>=0 and p[x]<q[y]):
#    print('a=p[x]')
    a=p[x]
    f=2
  elif (x==-1 and y>=0) or (y>=0 and p[x]>=q[y]):
#    print('a=q[y]')
    a=q[y]
    f=1
  else:
#    print('break2')
    break

ans=0
#print(p[:X],q[:Y])
for i in p[:X]:
  ans+=i
for i in q[:Y]:
  ans+=i

print(ans)

Buggy submission (verdict: Runtime Error):
from functools import reduce

X,Y,A,B,C=map(int, input().split())
p=list(map(int, input().split()))
q=list(map(int, input().split()))
r=list(map(int, input().split()))

x=X-1
y=Y-1

p=sorted(p,reverse=True)
q=sorted(q,reverse=True)
r=sorted(r,reverse=True)


if p[x]>=q[y]:
  a=q[y]
  f=1
else:
  a=p[x]
  f=2
while a<r[0] and len(r)>=1:
  if f==1 :
    q[y]=r[0]
    y-=1
    r.pop(0)
  elif f==2 :
    p[x]=r[0]
    x-=1
    r.pop(0)
  else:
    break
  if (y==-1 and x>=0) or (x>=0 and p[y]>q[x]):
    a=p[x]
    f=2
  elif (x==-1 and y>=0) or (y>=0 and p[x]>=q[y]):
    a=q[y]
    f=1
  else:
    break

ans=0
for i in p[:X]:
  ans+=i
for i in q[:Y]:
  ans+=i

print(ans)

Buggy submission (verdict: Runtime Error):
#from typing import MutableSequence

def qsort(a: int, left: int, right: int) -> None:
  pl=left
  pr=right
  x=a[(left+right)//2]

  while pl<=pr:
    while a[pl]>x: pl+=1    # 降順
    while a[pr]<x: pr-=1
    if pl <= pr:
      a[pl],a[pr] = a[pr],a[pl]
      pl+=1
      pr-=1
  
  if left  <pr: qsort(a,left,pr)
  if right >pl: qsort(a,pl,right)
    
def quick_sort(a: int) -> None:
  qsort(a,0,len(a)-1)
  
if __name__ == '__main__':
  X,Y,A,B,C = map(int, input().split())
  p=list(map(int,input().split()))
  q=list(map(int,input().split()))
  r=list(map(int,input().split()))

  quick_sort(p)
  quick_sort(q)
  quick_sort(r)
  print(p)
  print(q)

  j=0
  k=0
  for i in range(C):
    if p[X-j-1] < r[i] and  q[Y-k-1] < r[i]:
      if q[Y-k-1] < p[X-j-1]:
        q[Y-k-1]=r[i]
        k+=1
      else:
        p[X-j-1]=r[i]
        j+=1
    elif p[X-j-1] < r[i]:
      p[X-j-1]=r[i]
      j+=1
    elif q[Y-k-1] < r[i]:
      q[Y-k-1] = r[i]
      k+=1
    
    print(i)
    print(p)
    print(q)
      
  x=0
  for i in range(X):
    x+=p[i]
  for i in range(Y):
    x+=q[i]
  print(x)

Buggy submission (verdict: Runtime Error):
def qsort(a: int, left: int, right: int) -> None:
  pl=left
  pr=right
  x=a[(left+right)//2]

  while pl<=pr:
    while a[pl]>x: pl+=1    # 降順
    while a[pr]<x: pr-=1
    if pl <= pr:
      a[pl],a[pr] = a[pr],a[pl]
      pl+=1
      pr-=1
  
  if left  <pr: qsort(a,left,pr)
  if right >pl: qsort(a,pl,right)
    
def quick_sort(a: int) -> None:
  qsort(a,0,len(a)-1)
  
if __name__ == '__main__':
  X,Y,A,B,C = map(int, input().split())
  p=list(map(int,input().split()))
  q=list(map(int,input().split()))
  r=list(map(int,input().split()))

  quick_sort(p)
  quick_sort(q)
  
  for i in range(C):
    if p[X-i-1] < r[i]:
      if q[Y-i-1] < p[X-i-1]:q[Y-i-1] = r[i]
      else:p[X-i-1] = r[i]
    else:  
      if q[Y-i-1] < r[i]:q[Y-i-1] = r[i]
  
  x=0
  for i in range(X):
    x+=p[i]
  for i in range(Y):
    x+=q[i]
  print(x)