Problem:


Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74



Sample input:
1 2 2 2 1
2 4
5 1
3


Sample output:
12

Submission status: Runtime Error

Solution code:
, y, a, b, c = map(int, input().split())
p = sorted(list(map(int, input().split())), reverse=True)
q = sorted(list(map(int, input().split())), reverse=True)
r = sorted(list(map(int, input().split())), reverse=True)

p = p[:x]
q = q[:y]
a = p + q + r 
a = sorted(a, reverse=True)
print(sum(a[:x+y]))

Submission status: Runtime Error

Solution code:
import heapq

i = list(map(int, input().split()))
X = i[0]
Y = i[1]
A = i[2]
B = i[3]
C = i[4]
r = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

r = list(map(lambda x:x*(-1), r))
b = list(map(lambda x:x*(-1), b))
c = list(map(lambda x:x*(-1), c))
heapq.heapify(r)
heapq.heapify(b)
heapq.heapify(c)

r2 = []
b2 = []
a = []

for i in range(X):
    r2.append(heapq.heappop(r)*(-1))
for i in range(Y):
    b2.append(heapq.heappop(b)*(-1))

for i in range(len(r2)):
    a.append(heapq.heappop(r2))
for i in range(len(b2)):
    a.append(heapq.heappop(b2))
    
heapq.heapify(a)

while a[0] < c[0]*-1:
    heapq.heappop(a)
    a.append(heapq.heappop(c)*(-1))
    heapq.heapify(a)
    
sum(a)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
X, Y, A, B, C = [int(x) for x in input().strip().split()]
p = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:X]
q = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:Y]
r = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:min(C, X+Y)]

pi, qi, ri = X-1, Y-1, 0
tp, tq, tr = p[pi], q[qi], r[ri]
ans = 0
while pi >= 0 or qi >= 0:
    if tp >= tr and  tq >= tr:
        break
    elif tp < tr and tq < tr:
        if tp < tq:
            p[pi] = r[ri]
            pi -= 1
            ri += 1
        else:
            q[qi] = r[ri]
            qi -= 1
            ri += 1
    elif tp < tr or tq < tr:
        if tp < tr:
            p[pi] = r[ri]
            ri += 1
            pi -= 1
            tp = p[pi]
        else:
            q[qi] = r[ri]
            ri += 1
            qi -= 1
            tq = q[qi]
    else:
        break
    if ri == len(r):
        break

print(sum(p)+sum(q))

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A = sorted(A)
B = sorted(B)

A = A[len(A)-x:]
B = B[len(B)-y:]
C = list(map(lambda x: x*(-1), C))

import heapq
heapq.heapify(A)
heapq.heapify(B)
heapq.heapify(C)

flag = True
while flag:
  a = heapq.heappop(A)
  b = heapq.heappop(B)
  c = heapq.heappop(C)*(-1)
  if c < a and c < b:
    flag = False
  else:
    if a-c > b-c:
      heapq.heappush(A, c)
      heapq.heappush(B, b)
      if not C:
        break
    else:
      heapq.heappush(A, a)
      heapq.heappush(B, c)
      if not C:
        break
print(sum(A) + sum())

Submission status: Runtime Error

Solution code:
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()))
p.sort()
q.sort()
r.sort(reverse=True)
pcount=0
qcount=0
rcount=0
ans=[]
for i in range(x):
  ans.append(p[-1-i])
for i in range(y):
  ans.append(q[-1-i])
ans.sort()
for i in range(c):
  if ans[i]<=r[rcount]:
    ans[i]=r[rcount]
    rcount+=1
print(sum(ans))

Submission status: Runtime Error

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    x,y,a,b,c=map(int,input().split())
    p=sorted(list(map(int,input().split())), reverse=True)
    q=sorted(list(map(int,input().split())), reverse=True)
    r=sorted(list(map(int,input().split())), reverse=True)

    ptn1 = 0
    ptn2 = 0
    ptn1 = sum(q[:y])
    ptn2 = sum(p[:x])
    idxr = 0
    idxp = 0
    for i in range(x):
        if r[idxr] > p[idxp]:
            ptn1 += r[idxr]
            idxr += 1
        else:
            ptn1 += p[idxp]
            idxq += 1
    idxr = 0
    idxq = 0
    for i in range(y):
        if r[idxr] > q[idxq]:
            ptn2 += r[idxr]
            idxr += 1
        else:
            ptn2 += q[idxq]
            idxq += 1

    print(max(ptn1,ptn2))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
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()))
p.sort()
q.sort()
r.sort()
r = sorted(r,reverse = True)
p = p[A-X:]
q = q[B-Y:]
if X+Y<C:
    r = r[:X+Y+1]
px = 0
qx = 0
for i in range(X+Y):
    if px == len(p) or qx ==len(q):
        break
    if min(p[px],q[qx],r[i]) ==p[px]:
        p[px] = r[i]
        px +=1
    elif min(p[px],q[qx],r[i]) ==q[qx]:
        q[qx] = r[i]
        qx += 1
    else:
        px += 1
        qx += 1
print(sum(p)+sum(q))

Submission status: Runtime Error

Solution code:
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()))
p.sort()
q.sort()
r.sort(reverse=True)
pcount=0
qcount=0
rcount=0
ans=[]
for i in range(x):
  ans.append(p[-1-i])
for i in range(y):
  ans.append(q[-1-i])
ans.sort()
print(ans,r)
for i in range(c):
  if ans[i]<=r[rcount]:
    ans[i]=r[rcount]
    rcount+=1
print(sum(ans))

Submission status: Runtime Error

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [list(input().split()) for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    x,y,a,b,c=map(int,input().split())
    p=sorted(list(map(int,input().split())), reverse=True)
    q=sorted(list(map(int,input().split())), reverse=True)
    r=sorted(list(map(int,input().split())), reverse=True)

    ptn1 = 0
    ptn2 = 0
    ptn1 = sum(q[:y])
    ptn2 = sum(p[:x])
    idxr = 0
    idxp = 0
    for i in range(x):
        if r[idxr] > p[idxp]:
            ptn1 += r[idxr]
            idxr += 1
        else:
            ptn1 += p[idx]
            idxq += 1
    idxr = 0
    idxq = 0
    for i in range(y):
        if r[idxr] > q[idxq]:
            ptn2 += r[idxr]
            idxr += 1
        else:
            ptn2 += q[idxq]
            idxq += 1

    print(max(ptn1,ptn2))
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
X, Y, A, B, C = [int(x) for x in input().strip().split()]
p = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:X]
q = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:Y]
r = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:min(C, X+Y)]

pi, qi, ri = X-1, Y-1, 0
tp, tq, tr = p[pi], q[qi], r[ri]
ans = 0
while pi >= 0 or qi >= 0:
    if tp >= tr and  tq >= tr:
        break
    elif tp < tr and tq < tr:
        if tp < tq:
            p[pi] = r[ri]
            pi -= 1
            ri += 1
        else:
            q[qi] = r[ri]
            qi -= 1
            ri += 1
    elif tp < tr or tq < tr:
        if tp < tr:
            p[pi] = r[ri]
            ri += 1
            pi -= 1
            tp = p[pi]
        else:
            q[qi] = r[ri]
            ri += 1
            qi -= 1
            tq = q[qi]
    else:
        break

print(sum(p)+sum(q))

Submission status: Runtime Error

Solution code:
import numpy as np
X, Y, A, B, C = map(int, input().split(' '))

red = list(map(int, input().split(' ')))
green = list(map(int, input().split(' ')))
none_a = list(map(int, input().split(' ')))

all_apple = sorted(red + green + none_a)

eaten = all_apples[:X + Y]

print(sum(eaten))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

x, y, a, b, c  = map(int, input().split())
p = list(map(int, sys.stdin.readline().split()))
p.sort(reverse=True)
q = list(map(int, sys.stdin.readline().split()))
q.sort(reverse=True)
r = list(map(int, sys.stdin.readline().split()))
r.sort()

l = p[:x]
l.extend(q[:y])
l.sort(reverse=True)
n = x + y
ans = sum(l)
for i in range(len(r)):
    tmp = sum(l[:n-i]) + r[:i]
    if tmp > ans:
        ans = tmp
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
X, Y, A, B, C = [int(x) for x in input().strip().split()]
p = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:X]
q = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:Y]
r = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:min(C, X+Y)]

print(p, q, r)
pi, qi, ri = X-1, Y-1, 0
tp, tq, tr = p[pi], q[qi], r[ri]
ans = 0
while pi >= 0 or qi >= 0:
    if tp >= tr and  tq >= tr:
        break
    elif tp < tr and tq < tr:
        if tp < tq:
            p[pi] = r[ri]
            pi -= 1
            ri += 1
        else:
            q[qi] = r[ri]
            qi -= 1
            ri += 1
    elif tp < tr or tq < tr:
        if tp < tr:
            p[pi] = r[ri]
            ri += 1
            pi -= 1
            tp = p[pi]
        else:
            q[qi] = r[ri]
            ri += 1
            qi -= 1
            tq = q[qi]
    else:
        break

print(sum(p)+sum(q))

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int, input().split())
arrayP = list(map(int, input().split()))
arrayQ = list(map(int, input().split()))
arrayR = list(map(int, input().split()))
arrayP.sort(reverse=True)
arrayQ.sort(reverse=True)
arrayR.sort(reverse=True)
arrayAns = []
for i in range(x):
    arrayAns.append(int(arrayP[i]))
for i in range(y):
    arrayAns.append(int(arrayQ[i]))
arrayAns.sort()
for i in range(min(len(arrayR), len(arrayAns)):
    if arrayAns[i] <= int(arrayR[i]):
        arrayAns[i] = int(arrayR[i])
    else:
        break
print(sum(arrayAns))

Submission status: Runtime Error

Solution code:
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()))
p.sort()
q.sort()
r.sort()
pcount=0
qcount=0
rcount=0
ans=[]
for i in range(x):
  ans.append(p[-1-i])
for i in range(y):
  ans.append(q[-1-i])
ans.sort()
for i in range(c):
  if (rcount+1)<=len(r) and ans[i]<=r[-1-rcount]:
    ans[i]=r[-1-rcount]
    rcount+=1
print(sum(ans))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
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()))
p.sort()
q.sort()
r.sort()
ans = sum(p[-x:])+sum(q[-y:])
x_pos = -x
y_pos = -y
for i in range(c):
    #したから見ていく
    if min(p[x_pos],q[y_pos]) >= r[i]:
        continue
    elif p[x_pos] > q[y_pos]:
        ans += r[i] - q[y_pos]
        y_pos -= 1
    else:
        ans += r[i] - p[x_pos]
        x_pos -= 1
    if x_pos == -a:
        p.insert(0,10**5)
    if y_pos == -b:
        q.insert(0,10**5)
print(ans)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C=map(int,input().split())
p=sorted(map(int,input().split()))
q=sorted(map(int,input().split()))
r=sorted(map(int,input().split()))
ans=[]
ans+=p[A-X:]
ans+=q[B-Y:]
ans.sort()
j=0
for i in ans:
	if i<r[-1]:
		ans[j]=r.pop()
		j+=1
	else:
		break
print(sum(ans))

Submission status: Runtime Error

Solution code:
#200329E
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()))

P = sorted(p)
R = sorted(r)
Red = p + r
Red = sorted(Red)
Deli =[]

count = 0
for i in range(X):
    Deli.append(Red[len(Red)-1-i])
    if Red[len(Red)-1-i] == P[len(P)-1-count]:
        Red[len(Red)-1-i] = 0
        #print(Red)
        P[len(P)-1-count]= 0
        count +=1
    else:
        Red[len(Red)-1-i] = 0
        R[len(R)-1-i]= 0

Green = q + R
Green = sorted(Green)
for i in range(Y):
    Deli.append(Green[len(Green)-1-i])
    
#print(Deli)
print(int(sum(Deli)))

Submission status: Runtime Error

Solution code:
x,y,a,b,c = (int(x) for x in input().split())

from bisect import insort

p_list = []
q_list = []
r_list = []

for value in input().split():
  value = int(value)
  if len(p_list) >= x:
    if p_list[-1*(x-1)] < value:
      insort(p_list,value)
      p_list = p_list[-1*(x+1):]
  else:
    insort(p_list,value)

for value in input().split():
  value = int(value)
  if len(q_list) >= y:
    if q_list[-1*(y-1)] < value:
      insort(q_list,value)
      q_list = q_list[-1*(y+1):]
  else:
    insort(q_list,value)
  
for value in input().split():
  value = int(value)
  if len(r_list) >= x+y:
    if r_list[-1*(x+y)] < value:
      insort(r_list.value)
      r_list = r_list[-1*(x+y+1):]
  else:
    insort(r_list,value)
  
red = 0
green = 0
white = 0

sum_num = 0

for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1

print(sum_num)

Submission status: Runtime Error

Solution code:
X,Y,A,B,C = [int(x) for x in input().split()]
R = [int(x) for x in input().split()]
G = [int(x) for x in input().split()]
CL = [int(x) for x in input().split()]

dp = [[0]*(X+1) for d in range(Y+1)]
for i in range(X):
    for j in range(Y):
        MAX = max(max(R[j],CL[j]),max(G[i],CL[i]))
        dp[i][j] = MAX
        if MAX == R[j]:
            A.pop(0)
        elif MAX == G[i]:
            B.pop(0)
        else:
            C.pop(0)

print(dp[-1])

Submission status: Runtime Error

Solution code:
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()))
p.sort()
q.sort()
r.sort()
pcount=0
qcount=0
rcount=0
ans=[]
for i in range(x):
  ans.append(p[-1-i])
for i in range(y):
  ans.append(q[-1-i])
ans.sort()
for i in range(c):
  if rcount<len(r)-1 and ans[i]<=r[-1-rcount]:
    ans[i]=r[-1-rcount]
    rcount+=1
print(sum(ans))

Submission status: Runtime Error

Solution code:
#200329E
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()))

P = sorted(p)
R = sorted(r)
Red = p + r
Red = sorted(Red)
Deli =[]

count = 0
for i in range(X):
    Deli.append(Red[len(Red)-1-i])
    if Red[len(Red)-1-i] == P[len(P)-1-i]:
        Red[len(Red)-1-i] = 0
        #print(Red)
        P[len(P)-1-count]= 0
        count +=1
    else:
        Red[len(Red)-1-i] = 0
        R[len(R)-1-i]= 0

Green = q + R
Green = sorted(Green)
for i in range(Y):
    Deli.append(Green[len(Green)-1-i])
    
#print(Deli)
print(int(sum(Deli)))

Submission status: Runtime Error

Solution code:
x,y,a,b,c = (int(x) for x in input().split())

from bisect import insort

p_list = []
q_list = []
r_list = []

for value in input().split():
  value = int(value)
  if len(p_list) >= x:
    if p_list[-1*(x-1)-1] < value:
      insort(p_list,value)
      p_list = p_list[-1*(x+1):]
  else:
    insort(p_list,value)

for value in input().split():
  value = int(value)
  if len(q_list) >= y:
    if q_list[-1*(y-1)-1] < value:
      insort(q_list,value)
      q_list = q_list[-1*(y+1):]
  else:
    insort(q_list,value)
  
for value in input().split():
  value = int(value)
  if len(r_list) >= x+y:
    if r_list[-1*(x+y)-1] < value:
      insort(r_list.value)
      r_list = r_list[-1*(x+y+1):]
  else:
    insort(r_list,value)
  
red = 0
green = 0
white = 0

sum_num = 0

for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1

print(sum_num)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

X, Y, num_red_apple, num_green_apple, num_apple = map(int, input().strip().split())
red_apples = list(map(int, input().strip().split()))
green_apples = list(map(int, input().strip().split()))
apples = list(map(int, input().strip().split()))

good_apples = []
for i in range(1, X+1):
    good_apples.append(sorted(red_apples)[-i])

for i in range(1, Y+1):
    good_apples.append(sorted(green_apples)[-i])

while(1):
    if max(apples) > min(good_apples):
        good_apple = apples.pop(apples.index(max(apples)))
        good_apples.remove(min(good_apples))
        good_apples.append(good_apple)
    else:
        break
    
print(sum(good_apples))

Submission status: Runtime Error

Solution code:
for i in range(c):
    # print(p[x - 1 - cnt_p], q[y - 1 - cnt_q], cnt_q, cnt_p, cumsum_q, cumsum_p)
    if x - 1 - cnt_p < 0 and y - 1 - cnt_q < 0:
        break
    if x - 1 - cnt_p < 0:
        if r[i] >= q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    if y - 1 - cnt_q < 0:
        if r[i] >= q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    if p[x - 1 - cnt_p] > q[y - 1 - cnt_q]:
        if r[i] >= q[y - 1 - cnt_q]:
            cumsum_q[y - 1] += r[i] - q[y - 1 - cnt_q]
            cnt_q += 1
        else:
            break
    else:
        if r[i] >= p[x - 1 - cnt_p]:
            cumsum_p[x - 1] += r[i] - p[x - 1 - cnt_p]
            cnt_p += 1
        else:
            break
print(cumsum_p[x - 1] + cumsum_q[y - 1])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
class fact():
    def __init__(self, i, mod):
        self.l = [1] * (i+1)
        for i in range(1, i+1):
            self.l[i] = self.l[i-1] * i
        self.mod = mod
        self.g1 = [1, 1]
        self.g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, 2 * (10 ** 5) + 1):
            self.g1.append((self.g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            self.g2.append((self.g2[-1] * inverse[-1]) % mod)


def main():
    N = int(input())
    d = {}
    mod = 10**9 + 7
    f = fact(N, mod)
    for i in range(N-1):
        a, b = map(int, input().split())
        if a in d:
            d[a].append(b)
        else:
            d[a] = [b]
        if b in d:
            d[b].append(a)
        else:
            d[b] = [a]
    for i in range(1, N+1):
        v = set()
        r, t = solve(i, N, v, f, d)
        print(r % mod)
def solve(n, a, v, f, d):
    v.add(n)
    x = 0
    unv = []
    for i in d[n]:
        if i not in v:
            x += 1
            unv.append(i)
    if x == 1:
        r, t = solve(unv[0], a-1, v, f, d)
        return r, t + 1
    if x == 0:
        return 1, 1
    t = 1
    r = 1
    for i in unv:
        rr, tt = solve(i, a-1, v, f, d)
        t += tt
        r *= rr
        r *= f.g2[tt]
    r *= f.l[t-1]
    return r, t
main()

Submission status: Runtime Error

Solution code:
X,Y,A,B,C=map(int,input().split())
p=sorted(map(int,input().split()))
q=sorted(map(int,input().split()))
r=sorted(map(int,input().split()))
ans=[]
ans+=p[A-X:]
ans+=q[B-Y:]
ans.sort()
i=0
for i in ans:
	if i<r[-1]:
		ans[i]=r.pop()
	else:
		break
print(sum(ans))

Submission status: Runtime Error

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from heapq import heappop, heappush, heapify
import heapq

 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

X, Y, A, B, C = MAP()
P = LIST()
Q = LIST()
R = LIST()

p = []
q = []
r = []

for a in P:
    p.append((-1) * a)

for b in Q:
    q.append((-1) * b)

for c in R:
    r.append((-1) * c)

heapify(p)
heapify(q)
heapify(r)

a = []
b = []
c = []
ans = 0
for i in range(X):
    x = (heappop(p)) * (-1)
    y = (heappop(r)) * (-1)
    if x >= y:
        ans += x
        heappush(r, -1 * y)
    else:
        ans += y
        heappush(p, -1 * x)

for j in range(Y):
    x = (heappop(q)) * (-1)
    y = (heappop(r)) * (-1)
    if x >= y:
        ans += x
        heappush(r, -1 * y)
    else:
        ans += y
        heappush(q, -1 * x)

# for k in range(X + Y):
#     x = heappop()
#     c.append(x)
print(ans)
# dp = [INF] * 
# print(max(P) + max(Q) + max(R))

Submission status: Runtime Error

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

p.sort()
q.sort()
p.reverse()
q.reverse()

a=p[:]
a.extend(q)
a.extend(r)
a.sort()
a.reverse()
b=q[:]
b.extend(r)
b.sort()
b.reverse()
c=q[:]
c.extend(r)
c.sort()
c.reverse()
red=0
green=0
sum=0
sum1=0
sum2=0
d=0
for i in range(X+Y):
  if(a[i] in p):
    red+=1
  elif(a[i] in q):
    green+=1
  d+=a[i]
 
  if(i<X):
      sum1+=p[i]
      sum2+=b[i]
    if(i<Y):
      sum1+=b[i]
      sum2+=q[i]
    
if(red<=X and green<=Y):
  print(d)
elif(red>X):
  print(sum1)
elif(green>Y):
  print(sum2)

Submission status: Runtime Error

Solution code:
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()))
p.sort()
q.sort()
r.sort()
pcount=0
qcount=0
rcount=0
ans=[]
for i in range(x):
  ans.append(p[-1-i])
for i in range(y):
  ans.append(q[-1-i])
ans.sort()
for i in range(c):
  if ans[i]<=r[-1-rcount]:
    ans[i]=r[-1-rcount]
    rcount+=1
print(sum(ans))

Submission status: Runtime Error

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

p.sort()
q.sort()
r.sort(reverse = True)

red = p[A - X : A]
green = q[B - Y : B]
rl = 0
gl = 0

for val in r:
  if val <= red[rl] and val <= green[gl]:
    break
  elif red[rl] > green[gl]:
    green[gl] = val
    gl += 1
  else:
    red[rl] = val
    rl += 1

print(sum(red) + sum(green))

Submission status: Runtime Error

Solution code:
X, Y, A, B, C = map(int, input().split())
p = sorted([int(i) for i in input().split()], reverse=True)
q = sorted([int(i) for i in input().split()], reverse=True)
r = sorted([int(i) for i in input().split()])

p = p[:X]
q = q[:Y]
plus_r = []

while r != [] and (p != [] and r[-1] > p[-1]) or (q != [] and r[-1] > q[-1]):
    if p[-1] < q[-1]:
        p.pop()
    else:
        q.pop()
    plus_r.append(r.pop())

print(sum(p) + sum(q) + sum(plus_r))

Submission status: Runtime Error

Solution code:
import sys,math,copy,queue,itertools
from fractions import gcd
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(0,1),(-1,0),(0,-1))
X,Y,A,B,C = LI()
p = LI()
q = LI()
r = LI()
p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)
pa =  [0] + list(itertools.accumulate(p))
qa =  [0] + list(itertools.accumulate(q))
p = [0] + p
q = [0] + q
red = X
green = Y
t = 0
pl = 0
while p[red] < r[t] or q[green] < r[t]:
    pl += r[t]
    t += 1
    if red == 0:
        green -= 1
    elif green == 0:
        red -= 1

    elif p[red] < q[green] :
        red -= 1
    else:
        green -= 1
    if t < 0 or (green ==0 and red ==0): break
print (pa[red] + qa[green] + pl)

Submission status: Runtime Error

Solution code:
x,y,a,b,c = map(int,input().split())
lst_r = list(map(int,input().split()))
lst_g = list(map(int,input().split()))
lst_n = list(map(lambda x:int(x)*(-1),input().split()))
from heapq import *

heapify(lst_r)
for i in range(a-x):
    _ = heappop(lst_r)

heapify(lst_g)
for i in range(b-y):
    _ = heappop(lst_g)
    
heapify(lst_n)

flg = lst_r > lst_g


while len(lst_n)>=0:
    cur = -1 * heappop(lst_n)
    if flg and cur > lst_g[0]:
        _ = heappushpop(lst_g,cur)
        flg = lst_r > lst_g
    elif (not flg) and cur > lst_r[0]:
        _ = heappushpop(lst_r,cur)
        flg = lst_r > lst_g
    else:
        break
print(sum(lst_g)+sum(lst_r))

Submission status: Runtime Error

Solution code:

eat_RED,eat_GR,hav_RED,hav_GR,hav_CL=map(int, input().split())
RED_yam=list(map(int, input().split()))
GR_yam=list(map(int, input().split()))
CL_yam=list(map(int, input().split()))


RED_sub=[]
GR_sub=[]
for i in range(eat_RED):
    RED_sub.append(max(RED_yam))
    RED_yam.remove(max(RED_yam))
for i in range(eat_GR):
    GR_sub.append(max(GR_yam))
    GR_yam.remove(max(GR_yam))

Apple=sorted(RED_sub+GR_sub)
if len(CL_yam)>0:
    for i in range(len(Apple)):
        if Apple[i]<max(CL_yam):
            Apple[i]=max(CL_yam)
            CL_yam.remove(max(CL_yam))
        else:
            break


print(sum(Apple))

Submission status: Runtime Error

Solution code:
x,y,a,b,c = (int(x) for x in input().split())

from bisect import insort

p_list = []
q_list = []
r_list = []

for value in input().split():
  value = int(value)
  if len(p_list) >= x:
    if p_list[-1*(x-1)-1] < value:
      insort(p_list,value)
      if len(p_list) % 50 == 0:
      	p_list = p_list[-1*(x+1)-1:]
  else:
    insort(p_list,value)

for value in input().split():
  value = int(value)
  if len(q_list) >= y:
    if q_list[-1*(y-1)-1] < value:
      insort(q_list,value)
      if len(q_list) % 50 == 0:
      	q_list = q_list[-1*(y+1)-1:]
  else:
    insort(q_list,value)
  
for value in input().split():
  value = int(value)
  if len(r_list) >= x+y:
    if r_list[-1*(x+y)-1] < value:
      insort(r_list.value)
      if len(r_list) % 50 == 0:
      	r_list = r_list[-1*(x+y+1)-1:]
  else:
    insort(r_list,value)
  
red = 0
green = 0
white = 0

sum_num = 0

for i in range(1,x+y+1):
  if red < x:
  	red_num = p_list[(red*-1)-1]
  else:
    red_num = 0
  if green < y:
  	green_num = q_list[(green*-1)-1]
  else:
    green_num = 0
  if white < c:
  	white_num = r_list[(white*-1)-1]
  else:
    white_num = 0
  
  max_p = max([red_num,green_num,white_num])
  sum_num += max_p
  if max_p == white_num:
    white += 1
  elif max_p == red_num:
    red += 1
  elif max_p == green_num:
    green += 1

print(sum_num)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
X, Y, A, B, C = [int(x) for x in input().strip().split()]
p = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:X]
q = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:Y]
r = np.array(sorted([int(x) for x in input().strip().split()], reverse=True))[:X+Y]

pi, qi, ri = X-1, Y-1, 0
tp, tq, tr = p[pi], q[qi], r[ri]
ans = 0
while pi >= 0 or qi >= 0:
    if tp >= tr and  tq >= tr:
        break
    elif tp < tr and tq < tr:
        if tp < tq:
            p[pi] = r[ri]
            pi -= 1
            ri += 1
        else:
            q[qi] = r[ri]
            qi -= 1
            ri += 1
    elif tp < tr or tq < tr:
        if tp < tr:
            p[pi] = r[ri]
            ri += 1
            pi -= 1
            tp = p[pi]
        else:
            q[qi] = r[ri]
            ri += 1
            qi -= 1
            tq = q[qi]
    else:
        break

print(sum(p)+sum(q))

Submission status: Runtime Error

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

p.sort(reverse=True)
q.sort(reverse=True)
r.sort(reverse=True)

alist = []
ans = 0

for i in range(X):
    alist.append(p[i])
    
for j in range(Y):
    alist.append(q[j])
    
alist.sort(reverse=True)
ans = sum(alist)

a2 = [alist[0]]
r2 = [r[0]]

for i in range(1,len(alist)):
    a2.append(alist[i]+a2[i-1])
    r2.append(r[i]+r2[i-1])

ans = a2[-1]

for i in range(C-1):
    ans = max(ans,a2[i]+r2[C-i-1])
    
print(ans)

Submission status: Runtime Error

Solution code:
x, y, a, b, c = map(int,input().split())
p = sorted(list(map(int,input().split())))[a-x:]
q = sorted(list(map(int,input().split())))[b-y:]
r = sorted(list(map(int,input().split())), reverse = True)
P=0
Q=0
for i in range(c):
  if p[P] < r[i] or q[Q] < r[i]:
    if p[P] >= q[Q]:
      q[Q]=r[i]
      if Q < b-1:
        Q += 1
    else:
      p[P]=r[i]
      if P < a-1:
        P += 1
  else:
    break
print(sum(p)+sum(q))

Submission status: Runtime Error

Solution code:
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()))
p.sort()
q.sort()
r.sort()
pcount=0
qcount=0
rcount=0
ans=[]
for i in range(x):
  ans.append(p[-1-i])
for i in range(y):
  ans.append(q[-1-i])
ans.sort()
for i in range(r):
  if ans[i]<=r[-1-rcount]:
    ans[i]=r[-1-rcount]
    rcount+=1
print(sum(ans))