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:
#heapq p,qの最小値側にrの最大値入れていく。
import heapq 

#初期入力
X,Y,A,B,C = (int(x) for x in 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)

ans_sum =sum(p[-X:]) + sum(q[-Y:])
p_hq = p[-X:]
q_hq = q[-Y:]
heapq.heapify(p_hq)
heapq.heapify(q_hq)
p_change =heapq.heappop(p_hq)
q_change =heapq.heappop(q_hq)
count_p =0
count_q =0
for i in r: #rは大きい順
    if p_change <= q_change and p_change < i:
        if  count_p < X:
            heapq.heappush(p_hq,i)
            count_p +=1
            ans_sum = ans_sum -p_change +i
            p_change =heapq.heappop(p_hq)
    elif p_change > q_change and q_change < i:
        if  count_q < Y:
            heapq.heappush(q_hq,i)
            count_q +=1
            ans_sum = ans_sum -q_change +i
            q_change =heapq.heappop(q_hq)
print(ans_sum)

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+=p[:x]+q[:y]
r.sort(reverse=True)
ans=0
for i in range(x+y):
    ans+=r[i]
print(ans)

Submission status: Accepted

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

mu += sorted(r)[-x:]
mu += sorted(b)[-y:]
print(sum(sorted(mu)[-(x+y):]))

Submission status: Accepted

Solution code:
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=True)
    q.sort(reverse=True)
    r.sort(reverse=True)

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

    apple = list()
    apple.extend(p)
    apple.extend(q)
    apple.extend(r)

    apple.sort(reverse=True)
    ans = 0
    for i in range(X+Y):
        ans += apple[i]

    print(ans)


if __name__ == "__main__":
    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.sort()
Q.sort()
R = R + P[a-x:] + Q[b-y:]
R.sort()
print(sum(R[-(x+y):]))

Submission status: Accepted

Solution code:
# N = int(input())
import heapq

X, Y, A, B, C = [int(i) for i in 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 = sorted(p, reverse=True)[:X]
q = sorted(q, reverse=True)[:Y]
heapq.heapify(p)
heapq.heapify(q)
r_asc = sorted(r, reverse=True)

for r in r_asc:
    p_min = heapq.heappop(p)
    q_min = heapq.heappop(q)
    if p_min > q_min and r > q_min:
        heapq.heappush(q, r)
        heapq.heappush(p, p_min)
    elif q_min >= p_min and r > p_min:
        heapq.heappush(p, r)
        heapq.heappush(q, q_min)
    else:
        heapq.heappush(q, q_min)
        heapq.heappush(p, p_min)
        break
print(sum(p) + sum(q))

Submission status: Accepted

Solution code:
# E - Red and Green Apples
from collections import deque

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)
P = deque(P)
Q = deque(Q)
R = deque(R)
red,green = 0,0
apple = [P.popleft(), Q.popleft(), R.popleft()]
ans = 0

for _ in range(X+Y):
    mapple = max(apple)
    ans += mapple
    if apple[2]==mapple:
        if len(R)>0:
            apple[2] = R.popleft()
        else:
            apple[2] = -1
    elif apple[0]==mapple:
        red += 1
        if red<X:
            apple[0] = P.popleft()
        else:
            apple[0] = -1
    else:
        green += 1
        if green<Y:
            apple[1] = Q.popleft()
        else:
            apple[1] = -1

print(ans)

Submission status: Accepted

Solution code:
def main():
    import bisect
    x,y,a,b,c = map(int,input().split())
    p = sorted(list(map(int,input().split())))[-1*x:]
    q = sorted(list(map(int,input().split())))[-1*y:]
    r = sorted(list(map(int,input().split())))
    ans = p+q+r
    ans = sorted(ans,reverse=True)[0:x+y]
    print(sum(ans))

    
if __name__ == "__main__":
    main()

Submission status: Accepted

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()), reverse=True)
pi = len(p) - x
qi = len(q) - y
ri = 0
for _ in range(len(r)):
    if r[ri] <= min(p[min(pi, len(p)-1)], q[min(qi, len(q)-1)]):
        break
    else:
        if pi >= len(p) and qi >= len(q):
            break
        else:
            if pi < len(p) and qi < len(q):
                if p[pi] < q[qi]:
                    p[pi] = r[ri]
                    pi += 1
                else:
                    q[qi] = r[ri]
                    qi += 1
            elif pi < len(p) and qi >= len(q):
                p[pi] = r[ri]
                pi += 1
            else:
                q[qi] = r[ri]
                qi += 1
            ri += 1
print(sum(p[-x:]) + sum(q[-y:]))

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


def solve():
    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())))

    ans = []
    ans += p[:X]
    ans += q[:Y]
    ans.sort(reverse=True)

    #print(ans, r)

    cl = 0
    while len(ans) and len(r):
        if ans[-1] < r[-1]:
            cl += r[-1]
            ans.pop(-1)
            r.pop(-1)
        else:
            break

    #print(ans, cl)
    print(sum(ans) + cl)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from heapq import heapify, heappop, heappush

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()))
r = [-x for x in r]

p.sort()
q.sort()
r.sort()

p = p[-x:]
q = q[-y:]

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

is_p = True
is_q = True

while is_p and is_q and r:
    pp = heappop(p)
    qq = heappop(q)
    rr = heappop(r)

    if pp <= qq:
        heappush(q, qq)
        if pp < -rr:
            heappush(p, -rr)
        else:
            is_p = False
            heappush(p, pp)
            heappush(r, rr)
    else:
        heappush(p, pp)
        if qq < -rr:
            heappush(q, -rr)
        else:
            is_q = False
            heappush(q, qq)
            heappush(r, rr)

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

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)
p = p[:x]
q = q[:y]
w = p+q+r
w.sort(reverse=True)
print(sum(w[:x+y]))

Submission status: Accepted

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

red = sorted(P[:X])
gre = sorted(Q[:Y])
m = min((X+Y),C)
n = R[:m]
red.extend(gre)
red.extend(n)
red = sorted(red, reverse=True)

print(sum(red[: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)

p=p[:x]
q=q[:y]

p.extend(q)
p.extend(r)

p.sort(reverse=True)
print(sum(p[:x+y]))

Submission status: Accepted

Solution code:
x,y,a,b,c= map(int,input().split())
p = sorted(list(map(int, input().split())), reverse=True)[:x]
q = sorted(list(map(int, input().split())), reverse=True)[:y]
r = sorted(list(map(int, input().split())), reverse=True)
ans = p+q+r
ans.sort(reverse=True)
print(sum(ans[:x+y]))

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
ls_a=list(map(int,input().split()))
ls_b=list(map(int,input().split()))
ls_c=list(map(int,input().split()))

ls_a.sort(reverse=True)
ls_b.sort(reverse=True)
ls_c.sort(reverse=True)

ls=sorted(ls_a[:X]+ls_b[:Y])
ans=sum(ls)
for i in range(min(X+Y, C)):
    if ls[i] < ls_c[i]:
        ans += ls_c[i] - ls[i]
print(ans)

Submission status: Accepted

Solution code:
X,Y,A,B,C=map(int,input().split())
ls_a=sorted(list(map(int,input().split())))
ls_b=sorted(list(map(int,input().split())))
ls_c=sorted(list(map(int,input().split())))
ls_x=ls_a[A-X:A]
ls_y=ls_b[B-Y:B]
ls_c.reverse()

ans=sum(ls_x)+sum(ls_y)
a=b=c=0
for _ in range(min([X+Y,C])):
  if a==len(ls_x):
    m=min([ls_y[b],ls_c[c]])
    if m==ls_y[b]:
      ans+=(-ls_y[b]+ls_c[c])
      b+=1
      c+=1
    else:
      break
  elif b==len(ls_y):
    m=min([ls_x[a],ls_c[c]])
    if m==ls_x[a]:
      ans+=(-ls_x[a]+ls_c[c])
      a+=1
      c+=1
    else:
      break
  else:    
    m=min([ls_x[a],ls_y[b],ls_c[c]])
    if m==ls_x[a]:
      ans+=(-ls_x[a]+ls_c[c])
      a+=1
      c+=1
    elif m==ls_y[b]:
      ans+=(-ls_y[b]+ls_c[c])
      b+=1
      c+=1
    else:
      break
print(ans)

Submission status: Accepted

Solution code:
import bisect
def bisect_left_reverse(a, x):
    if a[0] <= x:
        return 0
    if x < a[-1]:
        return len(a)
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] <= x:
            ok = mid
        else:
            ng = mid
    return ok
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 = p[:X]+q[:Y]
B = []
A.sort(reverse=True)
for i in range(len(r)):
    n = r[i]
    if A[0] == 0:
      break
    if A[-1] < n:
        index = bisect_left_reverse(A,n)
        A.pop(-1)
        B.append(n)
    else:
        break
print(sum(A)+sum(B))

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math

MOD = 10 ** 9 + 7
# MOD = 998244353
# sys.setrecursionlimit(10**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 = sorted(P, reverse=True)[:x]
Q = sorted(Q, reverse=True)[:y]
concat = P + Q + R
concat.sort(reverse=True)
print(sum(concat[: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()
p.reverse()
q.sort()
q.reverse()
while len(p)>X:
  p.pop()
while len(q)>Y:
  q.pop()
s=p+q+r
s.sort()
s.reverse()
c=0
for i in range(X+Y):
  c+=s[i]
print(c)

Submission status: Accepted

Solution code:
def solve(x, y, a, b, c, p, q, r):
    p = sorted(p)[::-1]
    q = sorted(q)[::-1]
    r = sorted(r)[::-1]
    v = sorted(p[:x] + q[:y])
    n = len(v)
    t = sum(v)
    for i in range(min(n,c)):
        if v[i] < r[i]:
            t += r[i] - v[i]
    return t

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()))
print(solve(x, y, a, b, c, p, q, r))

Submission status: Accepted

Solution code:
def solve(x, y, a, b, c, p, q, r):
    p = sorted(p)[::-1]
    q = sorted(q)[::-1]
    r = sorted(r)[::-1]
    v = sorted(p[:x] + q[:y])
    n = len(v)
    t = sum(v)
    i = 0
    for k in range(c):
        if (i < n) and (v[i] < r[k]):
            t += r[k] - v[i]
            i += 1
    return t

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()))
print(solve(x, y, a, b, c, p, q, r))

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)

eat_list = []

for i in range(x):
    eat_list.append(p[i])
for i in range(y):
    eat_list.append(q[i])
for i in range(c):
    eat_list.append(r[i])
    
eat_list.sort(reverse=True)

ans = 0

for i in range(x+y):
    ans += eat_list[i]

print(ans)

Submission status: Accepted

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
import math
from copy import deepcopy
import queue
import numpy as np
# sympy as syp(素因数分解とか)
Mod = 1000000007
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[y] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main():  #startline-------------------------------------------
    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]
    q = sorted(q)[::-1][:y]
    A = p + q + r
    A.sort(reverse=True)
    print(sum(A[:x+y]))
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from heapq import heapify, heappop, heappush
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

# ここから書き始める
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)

# print("r =", r)
r_ruiseki = [0 for i in range(c)]
for i in range(c):
    if i == 0:
        r_ruiseki[i] = r[i]
    else:
        r_ruiseki[i] = r[i] + r_ruiseki[i - 1]
# print("r_ruiseki =", r_ruiseki)

ans = sum(p[:x]) + sum(q[:y])
# print("ans =", ans)
s = sorted(p[:x] + q[:y], reverse=True)
s_ruiseki = [0 for i in range(x + y)]
# print(type(s_ruiseki))
for i in range(x + y):
    if i == 0:
        s_ruiseki[i] = s[i]
    else:
        s_ruiseki[i] = s[i] + s_ruiseki[i - 1]

s_ruiseki.reverse()
# print("s_ruiseki =", s_ruiseki)
for i in range(1, min(x + y, c + 1)):
    ans = max(ans, r_ruiseki[i - 1] + s_ruiseki[i])
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
input = sys.stdin.readline
input = sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')


#import numpy as np

def main():
	x,y,a,b,c=MI()
	p=LI()
	q=LI()
	r=LI()

	p.sort()
	q.sort()
	r.sort()

	p=p[a-x:]
	q=q[b-y:]

	li=p+q+r
	li.sort()
	li=li[len(li)-x-y:]
	print(sum(li))




if __name__ == "__main__":
	main()

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()
ql.sort()
rl.sort(reverse=True)
pl_x_larges = pl[A-X:A]
ql_y_larges = ql[B-Y:B]

pq_xy = pl_x_larges + ql_y_larges
pq_xy.sort()

for i, r in enumerate(rl):
    if r > pq_xy[i]:
        pq_xy[i] = r
    else:
        break

print(sum(pq_xy))

Submission status: Accepted

Solution code:
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


def zz():
    return list(map(int, input().split()))


def z():
    return int(input())


def S():
    return input()


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

p = sorted(p)
q = sorted(q)
r = sorted(r, reverse=True)
# 透明なしのとき
p = p[-X:]
q = q[-Y:]

heapq.heapify(p)
heapq.heapify(q)
min_p = heapq.heappop(p)
min_q = heapq.heappop(q)
for i in range(C):

    if (min_p <= min_q):
        if (min_p < r[i]):
            # pと入れ替える
            heapq.heappush(p, r[i])
            min_p = heapq.heappop(p)
        else:
            break
    else:
        if (min_q < r[i]):
            heapq.heappush(q, r[i])
            min_q = heapq.heappop(q)
        else:
            break


print(sum(p) + sum(q) + min_p + min_q)

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


#隣接リスト 1-order
def make_adjlist_d(n, edges):
    res = [[] for _ in range(n + 1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
        res[edge[1]].append(edge[0])
    return res


def make_adjlist_nond(n, edges):
    res = [[] for _ in range(n + 1)]
    for edge in edges:
        res[edge[0]].append(edge[1])
    return res


#nCr
def cmb(n, r):
    return math.factorial(n) // math.factorial(r) // math.factorial(n - r)


def main():
    X, Y, A, B, C = NMI()
    P = NLI()
    Q = NLI()
    R = NLI()
    reds = [[1, p] for p in P]
    greens = [[2, q] for q in Q]
    skels = [[0, r] for r in R]
    apples = reds + greens + skels
    apples.sort(key=lambda x: x[1], reverse=True)

    colors = [0, 0, 0]
    limits = [10**9, X, Y]
    ans = 0
    for color, a in apples:
        if sum(colors) >= X + Y:
            break

        if colors[color] <= limits[color] - 1:
            colors[color] += 1
            ans += a
            continue

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop

x,y,a,b,c=n1()
P=n1()
Q=n1()
R=n1()

P.sort()
Q.sort()

A=P[-x:]+Q[-y:]+R

A.sort()
print(sum(A[-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)

S=P[:X]+Q[:Y]+R
S.sort(reverse=True)

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

Submission status: Accepted

Solution code:
x,y,a,b,c=map(int,input().split())
R=sorted(map(int,input().split()),reverse=True)
G=sorted(map(int,input().split()),reverse=True)
N=sorted(map(int,input().split()),reverse=True)
L=sorted(R[:x]+G[:y]+N[:(x+y)],reverse=True)
print(sum(L[:(x+y)]))

Submission status: Accepted

Solution code:
import sys 
x,y,A,B,C=map(int,sys.stdin.readline().split())
R=list(map(int,sys.stdin.readline().split()))
G=list(map(int,sys.stdin.readline().split()))
N=list(map(int,sys.stdin.readline().split()))
arr=[]
for i in range(A):
    arr.append([R[i],-1])
for i in range(B):
    arr.append([G[i],-2]) 
for i in range(C):
    arr.append([N[i],-3])    
arr.sort(reverse=True)
ans=0
adv=0
for i in range(len(arr)):
    if x+y==adv:
        break
    if arr[i][1]==-1:
        if x>0:
            ans+=arr[i][0]
            x-=1 
    elif arr[i][1]==-2:
        if y>0:    
            ans+=arr[i][0]
            y-=1
    else:
        ans+=arr[i][0]
        adv+=1 
        
print(ans)

Submission status: Accepted

Solution code:
x,y,A,B,C=map(int,input().split())
R=list(map(int,input().split()))
G=list(map(int,input().split()))
N=list(map(int,input().split()))
arr=[]
for i in range(A):
    arr.append([R[i],-1])
for i in range(B):
    arr.append([G[i],-2]) 
for i in range(C):
    arr.append([N[i],-3])    
arr.sort(reverse=True)
ans=0
adv=0
for i in range(len(arr)):
    if x+y==adv:
        break
    if arr[i][1]==-1:
        if x>0:
            ans+=arr[i][0]
            x-=1 
    elif arr[i][1]==-2:
        if y>0:    
            ans+=arr[i][0]
            y-=1
    else:
        ans+=arr[i][0]
        adv+=1 
        
print(ans)

Submission status: Accepted

Solution code:
X, Y, A, B, C = map(int, input().split())
p, q, r = [list(map(int, input().split())) for _ in range(3)]

p = sorted(p, reverse=True)[:X]
q = sorted(q, reverse=True)[:Y]

l = p+q+r

l.sort(reverse=True)

print(sum(l[: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()

R = p[:X]
G = q[:Y]
i = X - 1
j = Y - 1
con = True
while con and len(r) > 0:
    rr = r.pop()
    if R[i] < G[j]:
        if R[i] < rr:
            R[i] = rr
            i -= 1
            continue
        else:
            con = False
    else:
        if G[j] < rr:
            G[j] = rr
            j -= 1
            continue
        else:
            con = False

print(sum(R) + sum(G))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def main():
    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 = list(map(int,input().split()))

    print(sum(sorted(P[:X]+Q[:Y]+R,reverse=True)[:X+Y]))

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
def red_and_green_apples(X, Y, A, B, C, P, Q, R):
    P.sort(reverse=True)
    Q.sort(reverse=True)
    R.sort(reverse=True)

    P = P[:X]
    Q = Q[:Y]
    q = P + Q + R
    q.sort(reverse=True)

    return sum(q[:X + Y])


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

print(red_and_green_apples(X, Y, A, B, C, P, Q, R))

# red_and_green_apples(
#     1, 2, 2, 2, 1,
#     [2, 4],
#     [5, 1],
#     [3],
# )
#
# red_and_green_apples(
#     2, 2, 2, 2, 2,
#     [8, 6],
#     [9, 1],
#     [2, 1],
# )

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import copy

# 検索用タグ、バージョンによる相違点(pypy)

def resolve():
    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(key=lambda xx: -xx)
    q.sort(key=lambda xx: -xx)
    r.sort(key=lambda xx: -xx)

    # 着色を一切行わない場合の値
    # pypy3(2.4.0)ではlist.copy()が未実装。copy をインポートする必要がある。
    def_red = copy.copy(p[0:x])
    def_grren = copy.copy(q[0:y])

    # 無職のリンゴに置換していく・・
    redcnt = -1
    grncnt = -1
    def_red_cnt = len(def_red)
    def_grren_cnt = len(def_grren)

    for i in range(len(r)):
        if not (r[i] > def_red[redcnt] or r[i] > def_grren[grncnt]):
            continue

        if def_red[redcnt] > def_grren[grncnt] and r[i] > def_grren[grncnt]:
            # 透明のリンゴを緑に着色して、置き換える。
            def_grren[grncnt] = r[i]

            if grncnt is not -def_grren_cnt:
                grncnt -= 1

        else:
            # 透明のリンゴを赤に着色して、置き換える。
            def_red[redcnt] = r[i]

            if redcnt is not -def_red_cnt:
                redcnt -= 1


    print(sum(def_red) + sum(def_grren))


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """1 2 2 2 1
2 4
5 1
3"""
        output = """12"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """2 2 2 2 2
8 6
9 1
2 1"""
        output = """25"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4"""
        output = """74"""
        self.assertIO(input, output)
#     def test_自作成_1(self):
#         input = """1 1 1 1 1
# 10
# 20
# 90"""
#         output = """110"""
#         self.assertIO(input, output)
    # このパターンで「list index out of range」が発生。
#     def test_自作成_2(self):
#         input = """1 1 1 1 2
# 10
# 20
# 90 110"""
#         output = """200"""
#         self.assertIO(input, output)

if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
from collections import Counter

X,Y,A,B,C=map(int,input().split())
#3logN
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p.sort()
q.sort()
p_list=p[-X:]
q_list=q[-Y:]
r.extend(p_list)
r.extend(q_list)
r.sort()
print(sum(r[-(X+Y):]))