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: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
a_list=list(map(int,input().split()))
b_list=list(map(int,input().split()))
c_list=list(map(int,input().split()))
A_list=sorted(a_list,reverse=True)
B_list=sorted(b_list,reverse=True)
apples=[]
for i in range(X):
    apples.append(A_list[i])
for i in range(Y):
    apples.append(B_list[i])
D_list= apples + c_list
ans=0
E_list=sorted(D_list,reverse=True)
for i in range(X+Y):
   ans+=E_list[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq
from operator import itemgetter
 
MAX_INT = int(10e15)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

X,Y,A,B,C = IL()
p = IL()
q = IL()
r = IL()

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

d = []
heappop = heapq.heappop
heappush = heapq.heappush
for i in range(A):
  heappush(d, (-p[i],0))
for i in range(B):
  heappush(d, (-q[i],1))
for i in range(C):
  heappush(d, (-r[i],2))

acnt ,bcnt, ccnt = 0,0,0
cnt = 0
ans = 0
while acnt+bcnt+ccnt != X+Y:
  score,num = heappop(d)
  if num == 0:
    if acnt < X:
      acnt += 1
      ans -= score
  elif num == 1:
    if bcnt < Y:
      bcnt += 1
      ans -= score
  else:
    ccnt += 1
    ans -= score
print(ans)

Submission status: Accepted

Solution code:
import sys
from heapq import *

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    x,y,a,b,c=MI()
    pp=LI()
    qq=LI()
    rr=LI()
    hp=[]
    for p in pp:heappush(hp,(-p,0))
    for q in qq:heappush(hp,(-q,1))
    for r in rr:heappush(hp,(-r,2))
    cnt=[0]*3
    lim=[x,y,10**9]
    ans=0
    while hp and sum(cnt)<x+y:
        d,i=heappop(hp)
        if cnt[i]==lim[i]:continue
        ans-=d
        cnt[i]+=1
    print(ans)



main()

Submission status: Accepted

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 = sorted(p,reverse=True)
q = sorted(q,reverse=True)
r = sorted(r,reverse=True)

l = p[0:x] + q[0:y] + r[0:(x+y)]
l = sorted(l, reverse=True)
ans = 0
for i in range (0,(x+y)):
    ans = ans + l[i]

print(ans)

Submission status: Accepted

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()))
unchip = sorted(p)[::-1]
dsrxqp = unchip[0:x]
unchiq = sorted(q)[::-1]
dsrxqq = unchiq[0:y]
unchipqr = sorted(r+dsrxqq+dsrxqp)[::-1]
print(sum(unchipqr[0:x+y]))

Submission status: Accepted

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)

a = [10**10]*(10**5)
a.extend(p[:X])
b = [10**10]*(10**5)
b.extend(q[:Y])
p_c = -1
q_c = -1
r_c = 0
k = 0

ans = sum(p[:X])+sum(q[:Y])
while(True):
    if a[p_c] >= b[q_c]:
        if b[q_c] < r[r_c] and q_c >= - Y:
            ans += ( r[r_c] - b[q_c] )
            r_c += 1
            q_c -= 1
        else:
            k = 1

    else:
        if a[p_c] < r[r_c]:
            ans += (r[r_c] - a[p_c])
            r_c += 1
            p_c -= 1
        else:
            k = 1

    if k == 1 or r_c >= C:
        break

print(ans)

Submission status: Accepted

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)
x=0
y=0
R=0
z=0
s=0
while (x<X or y<Y) and z<X+Y:
  if (max(p[0],q[0],r[0])==p[0] or (y==Y and r[0]<p[0])) and x<X:
    s+=p[0]
    x+=1
    if x<A:
      p[0]=p[x]
    else:
      p[0]=0
    z+=1
  elif (max(p[0],q[0],r[0])==q[0] or (x==X and r[0]<q[0])) and y<Y:
    s+=q[0]
    y+=1
    if y<B:
      q[0]=q[y]
    else:
      q[0]=0
    z+=1
  else:
    s+=r[0]
    R+=1
    if R<C:
      r[0]=r[R]
    else:
      r[0]=0
    z+=1
print(s)

Submission status: Accepted

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

ri=sorted(r)[::-1]

qi=q[::]
pi=p[::]
pi=sorted(pi)[::-1]
pi=pi[:x]
qi=sorted(qi)[::-1]
qi=qi[:y]

l=pi+qi
l=sorted(l)
k=0
for i in range(min(c,x+y)):
    if l[i]>ri[i]:
        break
    else:
        k=i+1
print(sum(l[k:])+sum(ri[:k]))

Submission status: Accepted

Solution code:
import heapq

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

p2 = heapq.nlargest(x, p)
q2 = heapq.nlargest(y, q)
r2 = heapq.nlargest(x+y, r)
p2.append(0)
q2.append(0)
r2.append(0)

pi = 0
qi = 0
ri = 0

val = 0

for _ in range(x+y):
    if p2[pi] >= q2[qi] and p2[pi] >= r2[ri]:
        val += p2[pi]
        pi += 1
    elif p2[pi] <= q2[qi] and q2[qi] >= r2[ri]:
        val += q2[qi]
        qi += 1
    else:
        val += r2[ri]
        ri += 1

print(val)

Submission status: Accepted

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 =sorted(p)[::-1][:x][::-1]
q =sorted(q)[::-1][:y][::-1]
r =sorted(r)[::-1][:x+y]

pp = 0
qq = 0
pf = True
qf = True
for rr in r:
    lll = False
    if pf == True and qf == True:
        if p[pp]<rr and q[qq]<rr:
            lll = True
            if p[pp]<q[qq] and pf == True:
                p[pp] = rr
                pp += 1
                if pp == x:
                    pf = False
            elif qf == True:
                q[qq] = rr
                qq +=1
                if qq == y:
                    qf = False
    if pf == True and lll == False:
        if p[pp]<rr:
            lll = True
            if pf == True:
               p[pp] = rr
               pp += 1
               if pp == x:
                  pf = False
    if qf == True and lll == False:
        if q[qq]<rr:
            lll = True
            if qf == True:
                q[qq] = rr
                qq +=1
                if qq == y:
                    qf = False

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

P = P[:X]
Q = Q[:Y]

ans = sum(P[:X]) + sum(Q[:Y])

p = P.pop()
q = Q.pop()
r = R.pop()

INF = 10 ** 10


while True:
    m = min(p, q, r)

    if m == r or m == INF:
        break

    if m == p:
        ans += (r - p)

        if P:
            p = P.pop()
        else:
            p = INF
    elif m == q:
        ans += (r - q)

        if Q:
            q = Q.pop()
        else:
            q = INF

    if R:
        r = R.pop()
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
X, Y, A, B, C = map(int, input().split())
p_list = list(map(int, input().split()))
p_list.sort(reverse=True)

q_list = list(map(int, input().split()))
q_list.sort(reverse=True)

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

red_list = p_list[:X]
green_list = q_list[:Y]
min_num = min(red_list[-1], green_list[-1])
white_list = [i for i in r_list if i > min_num]

ans_list = red_list + green_list + white_list
ans_list.sort(reverse=True)
ans_list = ans_list[:X + Y]
print(sum(ans_list))

Submission status: Accepted

Solution code:
# coding: utf-8
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)
ans = 0
L = P[:X] + Q[:Y]
L.sort()
idx = 0
for i in range(X+Y):
    if idx >= C:
        break
    if L[i] < R[idx]:
        L[i] = R[idx]
        idx += 1
print(sum(L))

Submission status: Accepted

Solution code:
import heapq

x, y, a, b, c = map(int, input().split())
pp = list(map(lambda x: (-int(x), 1), input().split()))
qq = list(map(lambda x: (-int(x), 2), input().split()))
rr = list(map(lambda x: (-int(x), 3), input().split()))

xx = pp + qq + rr
heapq.heapify(xx)
t = x + y
ans = 0
for _ in range(t):
    while len(xx) > 0:
        v = heapq.heappop(xx)
        if v[1] == 1 and x <= 0:
            continue
        if v[1] == 2 and y <= 0:
            continue
        break

    if v[1] == 1:
        x -= 1
    if v[1] == 2:
        y -= 1
    ans += v[0]

print(-ans)

Submission status: Accepted

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()
p.reverse()
q.sort()
q.reverse()
P = p[:x]
Q = q[:y]
R = P+Q+r
R.sort()
R.reverse()
T = R[:x+y]
ans = sum(T)
print(ans)

Submission status: Accepted

Solution code:
from heapq import heapify, heappush, heappop

X, Y, A, B, C = map(int, input().split())
P = list(map(int, input().split()))
Q = list(map(int, input().split()))
rev = lambda x: -int(x)
R = list(map(int, input().split()))
P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)
red = []
green = []
heapify(red)
heapify(green)

red_sum = 0
for i in range(X):
    red_sum += P[i]
    heappush(red, P[i])

green_sum = 0
for i in range(Y):
    green_sum += Q[i]
    heappush(green, Q[i])

min_p = heappop(red)
min_q = heappop(green)
for i in range(C):
    r = R[i]
    if min_p < min_q:
        if r > min_p:
            heappush(red, r)
            red_sum -= min_p
            red_sum += r
            min_p = heappop(red)
    else:
        if r > min_q:
            heappush(green, r)
            green_sum -= min_q
            green_sum += r
            min_q = heappop(green)
print(green_sum + red_sum)

Submission status: Accepted

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

ans = 0
p = p[:x]
q = q[:y]
ans += sum(p) + sum(q)
pq = p+q
pq.sort()

for i in range(c):
    if pq[i] < r[i]:
        ans = ans - pq[i] + r[i]
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    readline=stdin.readline
    X,Y,A,B,C=map(int,readline().split())
    p=list(map(int,readline().split()))
    q=list(map(int,readline().split()))
    r=list(map(int,readline().split()))

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

    res=sum(p[:X])+sum(q[:Y])
    i=X-1
    j=Y-1
    k=0
    while True:
        if k==C:
            break
        if i!=-1 and j!=-1:
            if q[j]<=p[i]:
                if q[j]<r[k]:
                    res-=q[j]
                    res+=r[k]
                    j-=1
                    k+=1
                else:
                    break
            else:
                if p[i]<r[k]:
                    res-=p[i]
                    res+=r[k]
                    i-=1
                    k+=1
                else:
                    break
        elif i==-1:
            if q[j]<r[k]:
                res-=q[j]
                res+=r[k]
                j-=1
                k+=1
            else:
                break
        elif j==-1:
            if p[i]<r[k]:
                res-=p[i]
                res+=r[k]
                i-=1
                k+=1
            else:
                break
        else:
            break

    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
p_A = list(map(int, input().split()))
q_B = list(map(int, input().split()))
r_C = list(map(int, input().split()))
p_A2 = sorted(p_A, reverse=True)
q_B2 = sorted(q_B, reverse=True)
r_C2 = sorted(r_C, reverse=True)

ans = []
ans += p_A2[:X]
ans += q_B2[:Y]
ans = sorted(ans)

cnt = 0
for i in range(len(ans)):
    if cnt <= C-1:
        if ans[i] < r_C2[cnt]:
            ans[i] = r_C2[cnt]
            cnt += 1
print(sum(ans))

Submission status: Accepted

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)
Z = X+Y
P = p[:X]
Q = q[:Y]
R = r[:Z]
ans = 0
if X <= Y:
  S = P + R
  S.sort(reverse=True)
  a = S[:X]
  b = S[X:]
  ans += sum(a)
  T = Q + b
  T.sort(reverse=True)
  c = T[:Y]
  ans += sum(c)
else:
  S = Q + R
  S.sort(reverse=True)
  a = S[:Y]
  b = S[Y:]
  ans += sum(a)
  T = P + b
  T.sort(reverse=True)
  c = T[:X]
  ans += sum(c)
print(ans)

Submission status: Accepted

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)
P = P[:X]
Q.sort(reverse=True)
Q = Q[:Y]
sp = sum(P)
sq = sum(Q)
ans = sp+sq
R.sort(reverse=True)
import heapq  # heapqライブラリのimport
heapq.heapify(P)  # リストを優先度付きキューへ
heapq.heapify(Q)
p = heapq.heappop(P) # 最小値の取り出し
q = heapq.heappop(Q)
r = R.pop(0)
while p<r or q<r:
  #print(p,q,r,ans)
  if r-p > r-q:
    heapq.heappush(P, r)
    ans += r-p
    p = heapq.heappop(P)
  else:
    heapq.heappush(Q, r)
    ans += r-q
    q = heapq.heappop(Q)
  if len(R)==0:
    break
  r = R.pop(0)
  #print(p,q,r,ans)
print(ans)

#print(*ans[1:], sep='\n')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-*-

from sys import stdin


def main():
    X, Y, A, B, C = list(map(int, stdin.readline().split()))
    p = list(map(int, stdin.readline().split()))
    q = list(map(int, stdin.readline().split()))
    r = list(map(int, stdin.readline().split()))

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

    oisii = []
    oisii.extend(p[0:X])
    oisii.extend(q[0:Y])
    oisii.extend(r)
    oisii.sort(reverse=True)
    print( sum( oisii[0:X+Y]) )


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from heapq import *
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)
X=p[:x]
Y=q[:y]
heapify(X)
heapify(Y)

for i in r:
    if X[0]>=i and Y[0]>=i:
        break
    if X[0]<Y[0]:
        heappushpop(X,i)
    else:
        heappushpop(Y,i)

print(sum(X)+sum(Y))

Submission status: Accepted

Solution code:
import heapq
def E():
    X, Y, A, B, C = map(int, input().split())
    reds = []
    heapq.heapify(reds)
    for n in input().split():
        heapq.heappush(reds, int(n))
    greens = []
    heapq.heapify(greens)
    for n in input().split():
        heapq.heappush(greens, int(n))
    colorless = []
    heapq.heapify(colorless)
    for n in input().split():
        heapq.heappush(colorless, -int(n))
    
    redx = heapq.nlargest(X, reds)
    greeny = heapq.nlargest(Y, greens)
    colorless_val = 0
    while True:
        if not colorless:
            break
        cur = abs(heapq.heappop(colorless))
        if redx and cur > redx[-1]:
            if greeny and redx[-1] > greeny[-1]:
                greeny.pop()
            else:
                redx.pop()
            colorless_val += cur
        elif greeny and cur > greeny[-1]:
            if redx and greeny[-1] > redx[-1]:
                redx.pop()
            else:
                greeny.pop()
            colorless_val += cur
        else:
            break
    print(sum(redx)+sum(greeny)+colorless_val)

E()

Submission status: Accepted

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.sort(reverse=True)
b.sort(reverse=True)
c.sort(reverse=True)

ans = 0
at = a[:X]
bt = b[:Y]
ap = X - 1
bp = Y - 1
cp = 0
cl = len(c)
while cp < cl and (at[ap] < c[cp] or bt[bp] < c[cp]):
    if at[ap] < bt[bp]:
        if at[ap] < c[cp]:
            at[ap] = c[cp]
            ap -= 1
            cp += 1
    else:
        if bt[bp] < c[cp]:
            bt[bp] = c[cp]
            bp -= 1
            cp += 1
print(sum(at) + sum(bt))

Submission status: Accepted

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

#for i in range(c):
#    maze[a + b + i] = ["n",r[i]]

#maze.sort(reverse=True,key = lambda x:x[1])
r.sort(reverse=True)
p.sort(reverse=True)
q.sort(reverse=True)
maze = p[:x] + q[:y]
ans = sum(maze)
dpc = [0]*(c+1)
dpa = [0]*(a+b+1)
maze.sort()
for i in range(c):
    dpc[i+1] = dpc[i] + r[i] 
for i in range(x+y):
    dpa[i+1] = dpa[i]+maze[i]
maxans = ans
for i in range(min(x+y,c)):
    ans = max(ans,maxans - dpa[i+1] + dpc[i+1])
print(ans)

Submission status: Accepted

Solution code:
import heapq

x,y,a,b,c = map(int, input().split())
pl = list(map(int, input().split()))
ql = list(map(int, input().split()))
rl = list(map(int, input().split()))

pl.sort(reverse=True)
pl = pl[:x]
heapq.heapify(pl)
ql.sort(reverse=True)
ql = ql[:y]
heapq.heapify(ql)
rl.sort(reverse=True)

for r in rl:
    dp = r-pl[0]
    dq = r-ql[0]
    if dp<=0 and dq<=0:
        break
    if dp>dq:
        heapq.heappushpop(pl,r)
    else:
        heapq.heappushpop(ql,r)

print(sum(pl)+sum(ql))

Submission status: Accepted

Solution code:
'''
Created on 2020/03/28

@author: fanta
'''
X,Y,A,B,C = map(int,input().split())
RED = [int(x) for x in input().split()]
GREEN = [int(x) for x in input().split()]
WHITE = [int(x) for x in input().split()]
out = 0
apples = []

RED.sort(key=None, reverse=True)
GREEN.sort(key=None, reverse=True)
WHITE.sort(key=None, reverse=True)

apples = RED[:X] + GREEN[:Y] + WHITE
apples.sort(key=None, reverse=True)


print(sum(apples[:X+Y]))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(X: int, Y: int, A: int, B: int, C: int, p: "List[int]", q: "List[int]", r: "List[int]"):
    p.sort(reverse=True)
    q.sort(reverse=True)
    r.sort(reverse=True)
    p.append(0)
    q.append(0)
    r.append(0)
    pp = 0
    qq = 0
    rr = 0
    ans = 0
    while pp + qq + rr != X + Y:
        if qq == Y:
            if p[pp] >= r[rr]:
                ans += p[pp]
                pp += 1
            else:
                ans += r[rr]
                rr += 1
            continue
        if pp == X:
            if q[qq] >= r[rr]:
                ans += q[qq]
                qq += 1
            else:
                ans += r[rr]
                rr += 1
            continue

        
        if r[rr] >= q[qq] and r[rr] >= p[pp]:
            ans += r[rr]
            rr += 1
        elif q[qq] >= p[pp]:
            ans += q[qq]
            qq += 1
        else:
            ans += p[pp]
            pp += 1


    print(ans)


        

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    X = int(next(tokens))  # type: int
    Y = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    p = [int(next(tokens)) for _ in range(A)]  # type: "List[int]"
    q = [int(next(tokens)) for _ in range(B)]  # type: "List[int]"
    r = [int(next(tokens)) for _ in range(C)]  # type: "List[int]"
    solve(X, Y, A, B, C, p, q, r)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def 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()))
    p.sort(reverse=1)
    q.sort(reverse=1)
    l=p[:X]+q[:Y]+r
    l.sort(reverse=1)
    print(sum(l[:X+Y]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
x,y,a,b,c = map(int, input().split())			# "5 7" -> ["5", "7"] -> 5, 7 => N=5,K=7

A = sorted(list( map(int, input().split()) ), reverse=True)	# 1 2 3 4 5 ...(降順(逆順)ソート付き)
B = sorted(list( map(int, input().split()) ), reverse=True)	# 1 2 3 4 5 ...(降順(逆順)ソート付き)
C = sorted(list( map(int, input().split()) ), reverse=True)	# 1 2 3 4 5 ...(降順(逆順)ソート付き)

#p("A",A)
#p("B", B)
#p("C", C)

A = A[:x]
B = B[:y]

#p("A",A)
#p("B", B)

ans=a=b=c=0

for _ in range(x+y):
	an = 0 if len(A)<=a else A[a]
	bn = 0 if len(B)<=b else B[b]
	cn = 0 if len(C)<=c else C[c]
#	p("a,b,c、an,bn,cn = ",a,b,c, "、", an,bn,cn)
	
	m = max(an,bn,cn)
	ans += m

#	p("ans,m = ",ans,m)

	if m == an:   a+=1
	elif m == bn: b+=1
	elif m == cn: c+=1
#	p("a,b,c = ",a,b,c)

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
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)
ans = 0
L = P[:X] + Q[:Y]
Rt = [False for _ in range(C)]
L.sort()
idx = 0
for i in range(X+Y):
    if idx >= C:
        break
    if L[i] < R[idx]:
        L[i] = R[idx]
        idx += 1
print(sum(L))

Submission status: Accepted

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

out = P[-X:] + Q[-Y:]

out.sort()
for i in range(min(C, X+Y)):
  if R[-i-1] > out[i]:
    out[i] = R[-i-1]
  else:
    break
print(sum(out))

Submission status: Accepted

Solution code:
X,Y,A,B,C = list(map(int,input().split()))
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
R = list(map(int,input().split()))
A = [[i,'p'] for i in P]
for i in Q:
    A.append([i,'q'])
for i in R:
    A.append([i,'r'])
As = list(sorted(A,reverse=True))
qN = 0
pN = 0
rN = 0
ans = 0
for i in As:
    if i[1] == 'q' and qN < Y:
        qN += 1
        ans += i[0]
    elif i[1] == 'p' and pN < X:
        pN += 1
        ans += i[0]
    elif i[1] == 'r':
        rN += 1
        ans += i[0]
    if qN + pN + rN == X + Y:
        break
print(ans)

Submission status: Accepted

Solution code:
import math



red,green,x,y,z = map(int,input().split())
red_data = list(map(int,input().split()))
green_data = list(map(int,input().split()))
blank_data = list(map(int,input().split()))


red_data.sort(reverse=True)
green_data.sort(reverse=True)
blank_data.sort(reverse=True)


# check if colourless apples are better
red_index = red-1
green_index = green-1


for blank in blank_data:
    red_gap = -1
    if red_index >= 0:
        red_gap = blank - red_data[red_index]
    
    green_gap = -1
    if green_index >=0:
        green_gap = blank - green_data[green_index]
    
    if max(red_gap,green_gap) <= 0:
        break
    
    if red_gap >= green_gap:
        red_data[red_index] = blank
        red_index -= 1
    
    if green_gap > red_gap:
        green_data[green_index] = blank
        green_index -= 1






total = sum(red_data[0:red]) + sum(green_data[0:green])

print(total)

Submission status: Accepted

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()))
rlen = len(R)
if (rlen < x + y):
    max = rlen
else:
    max = x + y

P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

p = P[:x]
q = Q[:y]
r = R[:max]

p.extend(q)
p.sort()

ans = sum(p)

for i in range(max):
    diff = r[i] - p[i]
    if (diff > 0):
        ans = ans + diff
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
P = [int(i) for i in input().split()]
Q = [int(i) for i in input().split()]
R = [int(i) for i in input().split()]
P.sort()
Q.sort()
R.sort()

p, q, r = A - X, B - Y, C - 1
ans = 0

Amin = P[p]
Bmin = Q[q]
Cmax = R[r]
for i in range(C):
    if Cmax <= min(Amin, Bmin):
        break
    else:
        if Amin <= Bmin and p < A:
            ans += Cmax
            r -= 1
            p += 1
            if p < A:
                Amin = P[p]
            else:
                Amin = float("inf")
        elif Amin >= Bmin and q < B:
            ans += Cmax
            r -= 1
            q += 1
            if q < B:
                Bmin = Q[q]
            else:
                Bmin = float("inf")
        Cmax = R[r]
ans += sum(P[p:])+sum(Q[q:])
print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C = map(int,input().split())
*P, = map(int,input().split())
*Q, = map(int,input().split())
*R, = map(int,input().split())

P.sort(reverse=True)
Q.sort(reverse=True)
R.sort(reverse=True)

SP = [0]*(A+1)
SQ = [0]*(B+1)
SR = [0]*(C+1)
for i,a in enumerate(P):
    SP[i+1] = SP[i] + a
for i,b in enumerate(Q):
    SQ[i+1] = SQ[i] + b
for i,c in enumerate(R):
    SR[i+1] = SR[i] + c

ans = 0
for i in range(max(0,X-C),X+1):
    d = C-(X-i)
    l0 = max(0,Y-d)
    l = l0
    r = Y
    while l+1<r:
        j = (l+r)//2
        k = (X-i) + (Y-j)
        if (Q[j] < R[k]):
            r = j
        else:
            l = j
    tmp = SP[i] + SQ[r] + SR[X+Y-i-r]
    tmp = max(tmp,SP[i]+SQ[l]+SR[X+Y-i-l])
    ans = max(ans,tmp)
print(ans)

Submission status: Accepted

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)
import heapq
eat=p+q
heapq.heapify(eat)
for i in r:
	j=heapq.heappop(eat)
	if j<i:
		heapq.heappush(eat,i)
	else:
		heapq.heappush(eat,j)
#print(list(eat))
print(sum(list(eat)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 28 20:51:51 2020

@author: naoki
"""

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,reverse = True)
q = sorted(q,reverse = True)
r = sorted(r,reverse = True)

p_selected = p[:X]
q_selected = q[:Y]

#%%
All_selected = []
All_selected.extend(p_selected)
All_selected.extend(q_selected)
All_selected = sorted(All_selected)


for i in range(min(C,len(All_selected))):
    if All_selected[i] < r[i]:
        All_selected[i] = r[i]
print(sum(All_selected))

Submission status: Accepted

Solution code:
def main():
    from collections import deque

    X, Y, A, B, C = map(int, input().split())
    *p, = sorted(map(int, input().split()), reverse=True)
    *q, = sorted(map(int, input().split()), reverse=True)
    *r, = sorted(map(int, input().split()), reverse=True)

    p = deque(p[:X])
    q = deque(q[:Y])
    ret = sum(p) + sum(q)
    for x in r:
        if p:
            if q:
                if p[-1] < q[-1]:
                    if p[-1] >= x:
                        break
                    ret += x - p.pop()
                elif p[-1] == q[-1]:
                    popp = True
                    ofs = 1
                    while (1 + ofs) <= len(p) and (1 + ofs) <= len(q):
                        if p[-1 - ofs] == q[-1 - ofs]:
                            ofs += 1
                            continue

                        if p[-1 - ofs] < q[-1 - ofs]:
                            popp = True
                            break

                        if p[-1 - ofs] > q[-1 - ofs]:
                            popp = False
                            break
                    if popp:
                        if p[-1] >= x:
                            break
                        ret += x - p.pop()
                    else:
                        if q[-1] >= x:
                            break
                        ret += x - q.pop()
                else:
                    if q[-1] >= x:
                        break
                    ret += x - q.pop()

            else:
                if p[-1] >= x:
                    break
                ret += x - p.pop()
        else:
            if q:
                if q[-1] >= x:
                    break
                ret += x - q.pop()
            else:
                break

    print(ret)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

Solution code:
# input
X,Y,A,B,C = map(int, input().split())
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]
c_list = [int(i) for i in input().split()]
a_list.sort(reverse = True)
b_list.sort(reverse = True)
c_list.sort(reverse = True)

#solve
d_list = a_list[:X] + b_list[:Y] + c_list
d_list.sort(reverse = True)
print(sum(d_list[:X+Y]))

Submission status: Accepted

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=sorted(P)[::-1]
Q=sorted(Q)[::-1]
R=sorted(R)

if A+B+C==X+Y:
  print(sum(P)+sum(Q)+sum(R))
  exit()
if X<A:
  P=P[:X]
if Y<B:
  Q=Q[:Y]
l=0
if X>A:
  l+=(X-A)
if Y>B:
  l+=(Y-B)
h=C-l
S=sorted(P+Q+R[:h])[::-1]
ans=sum(S[:X+Y])+sum(R[h:])
print(ans)