Problem:
Problem A: Cubist Artwork  International Center for Picassonian Cubism is a Spanish national museum of cubist artworks,
dedicated to Pablo Picasso. The center held a competition for an artwork that will be displayed
in front of the facade of the museum building. The artwork is a collection of cubes that are
piled up on the ground and is intended to amuse visitors, who will be curious how the shape of
the collection of cubes changes when it is seen from the front and the sides. The artwork is a collection of cubes with edges of one foot long and is built on a flat ground
that is divided into a grid of one foot by one foot squares. Due to some technical reasons, cubes
of the artwork must be either put on the ground, fitting into a unit square in the grid, or put
on another cube in the way that the bottom face of the upper cube exactly meets the top face
of the lower cube. No other way of putting cubes is possible. You are a member of the judging committee responsible for selecting one out of a plenty of
artwork proposals submitted to the competition. The decision is made primarily based on
artistic quality but the cost for installing the artwork is another important factor. Your task is
to investigate the installation cost for each proposal. The cost is proportional to the number of
cubes, so you have to figure out the minimum number of cubes needed for installation. Each design proposal of an artwork consists of the front view and the side view (the view seen
from the right-hand side), as shown in Figure 1.   Figure 1: An example of an artwork proposal  The front view (resp., the side view) indicates the maximum heights of piles of cubes for each
column line (resp., row line) of the grid. There are several ways to install this proposal of artwork, such as the following figures.   In these figures, the dotted lines on the ground indicate the grid lines. The left figure makes use
of 16 cubes, which is not optimal. That is, the artwork can be installed with a fewer number
of cubes. Actually, the right one is optimal and only uses 13 cubes. Note that, a single pile of
height three in the right figure plays the roles of two such piles in the left one. Notice that swapping columns of cubes does not change the side view. Similarly, swapping
rows does not change the front view. Thus, such swaps do not change the costs of building the
artworks. For example, consider the artwork proposal given in Figure 2.   Figure 2: Another example of artwork proposal  An optimal installation of this proposal of artwork can be achieved with 13 cubes, as shown
in the following figure, which can be obtained by exchanging the rightmost two columns of the
optimal installation of the artwork of Figure 1.   Input The input is a sequence of datasets. The end of the input is indicated by a line containing two
zeros separated by a space. Each dataset is formatted as follows.  w d  h 1  h 2 ... h w  h' 1  h' 2 ... h' d  The integers w and d separated by a space are the numbers of columns and rows of the grid,
respectively. You may assume 1 ≤ w ≤ 10 and 1 ≤ d ≤ 10. The integers separated by a space
in the second and third lines specify the shape of the artwork. The integers h i (1 ≤ h i ≤ 20,
1 ≤ i ≤ w ) in the second line give the front view, i.e., the maximum heights of cubes per each
column line, ordered from left to right (seen from the front). The integers h i (1 ≤ h i ≤ 20,
1 ≤ i ≤ d ) in the third line give the side view, i.e., the maximum heights of cubes per each row
line, ordered from left to right (seen from the right-hand side). Output For each dataset, output a line containing the minimum number of cubes. The output should
not contain any other extra characters. You can assume that, for each dataset, there is at least one way to install the artwork. Sample Input 5 5
1 2 3 4 5
1 2 3 4 5
5 5
2 5 4 1 3
4 1 5 3 2
5 5
1 2 3 4 5
3 3 3 4 5
3 3
7 7 7
7 7 7
3 3
4 4 4
4 3 4
4 3
4 2 2 4
4 2 1
4 4
2 8 8 8
2 3 8 3
10 10
9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9
10 9
20 1 20 20 20 20 20 18 20 20
20 20 20 20 7 20 20 20 20
0 0 Output for the Sample Input 15
15
21
21
15
13
32
90
186

Solution (Python):
while True:
    w,d = [int(i) for i in input().split()]
    if w == 0:
        break
    h = [int(i) for i in input().split()]
    h_ = [int(i) for i in input().split()]
    h = sorted(h)
    h_ = sorted(h_)
    hdic = {}
    h_dic = {}

    for i in h:
        if i in hdic.keys():
            hdic[i]+= 1
        else:
            hdic[i] = 1
    
    for i in h_:
        if i in h_dic.keys():
            h_dic[i]+= 1
        else:
            h_dic[i] = 1
    ans  = sum(h)+ sum(h_)
    for num in hdic.keys():
        if num in h_dic.keys():
            ans -= num*min(hdic[num],h_dic[num])
    print(ans)

Solution (Python):
while 1 :
	w, d = map(int,raw_input().split(" "))
	if w == d == 0: break
	front = map(int,raw_input().split(" "))
	side = map(int,raw_input().split(" "))
	number =  sum(front)
	for h in side:
		if h in front:
			front.remove(h)
		else :
			number += h
	print number

Solution (Python):
while 1:
    w,d = map(int,raw_input().split())
    if w == 0: break
    front = map(int,raw_input().split())
    side = map(int,raw_input().split())
    for i in front:
        if i in side: side.remove(i)
    ans = sum(front)+sum(side)
    print ans

Solution (Python):
while 1:
    w,d = map(int,raw_input().split())
    if w == 0: break
    f = map(int,raw_input().split())
    s = map(int,raw_input().split())
    for i in f:
        if i in s:s.remove(i)
    print sum(f)+sum(s)

Solution (Python):
while 1:
    w,d=map(int,input().split())
    if w==d==0:break
    a=[0]*21
    ans=0
    b=list(map(int,input().split()))
    for i in range(w):
        a[b[i]]+=1
    ans=sum(b)
    b=list(map(int,input().split()))
    for i in range(d):
        if a[b[i]]:a[b[i]]-=1
        else: ans+=b[i]
    print(ans)

Solution (Python):
from collections import Counter
while True:
    W, D = map(int, input().split())
    if not (W | D):
        break
    hw = [int(x) for x in input().split()]
    hd = [int(x) for x in input().split()]

    cnt_hw = Counter(hw)
    cnt_hd = Counter(hd)
    print(min(sum(hw) + sum(max(0, v - cnt_hw[k]) * k for k, v in cnt_hd.items()),
              sum(hd) + sum(max(0, v - cnt_hd[k]) * k for k, v in cnt_hw.items())))

Solution (Python):
from collections import Counter
while True:
    W, D = map(int, input().split())
    if not (W | D):
        break
    hw = [int(x) for x in input().split()]
    hd = [int(x) for x in input().split()]
    print(sum(hw) + sum(hd) - sum(k * v for k, v in (Counter(hw) & Counter(hd)).items()))

Solution (Python):
from collections import Counter
while True:
    W, D = map(int, input().split())
    if not (W | D):
        break
    hw = [int(x) for x in input().split()]
    hd = [int(x) for x in input().split()]
    print(sum(hw) + sum(hd) - sum(k * v for k, v in (Counter(hw) & Counter(hd)).items()))

Solution (Python):
from collections import Counter
import sys
if sys.version[0] == '2':
    range, input = xrange, raw_input
while True:
    W, D = map(int, input().split())
    if not (W | D):
        break
    hw = [int(x) for x in input().split()]
    hd = [int(x) for x in input().split()]
    print(sum(hw) + sum(hd) - sum(k * v for k, v in (Counter(hw) & Counter(hd)).items()))

Solution (Python):
while True:
	n,m=map(int,raw_input().split())
	if n==0:break
	a=raw_input().split()
	b=raw_input().split()
	out=0
	for i in range(21):out+=i*max(a.count(str(i)),b.count(str(i)))
	print(out)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**3
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []
    while True:
        w,d = LI()
        if w == 0:
            break
        a = sorted(LI())
        b = sorted(LI())
        ai = 0
        bi = 0
        r = 0
        while ai < w or bi < d:
            if ai >= w:
                r += sum(b[bi:])
                break
            if bi >= d:
                r += sum(a[ai:])
                break
            if a[ai] == b[bi]:
                r += a[ai]
                ai += 1
                bi += 1
            elif a[ai] < b[bi]:
                r += a[ai]
                ai += 1
            else:
                r += b[bi]
                bi += 1
        rr.append(r)

    return '\n'.join(map(str, rr))


print(main())

Solution (Python):


while True:
    w,d = map(int,input().split())
    if w == 0 and d == 0:
        break

    h = list(map(int,input().split()))
    m = list(map(int,input().split()))

    hl = [0] * 30
    ml = [0] * 30

    for i in h:
        hl[i] += 1
    for i in m:
        ml[i] += 1

    ans = 0
    for i in range(30):

        ans += i * max(hl[i],ml[i])

    print (ans)

Solution (Python):
def solve():
    from sys import stdin
    from collections import Counter
    f_i = stdin
    
    while True:
        w, d = map(int, f_i.readline().split())
        if w == 0:
            break
        
        h_w = Counter(map(int, f_i.readline().split()))
        h_d = Counter(map(int, f_i.readline().split()))
        
        for k, v in h_w.items():
            if v > h_d.setdefault(k, v):
                h_d[k] = v
        
        print(sum(k * v for k, v in h_d.items()))

solve()

Solution (Python):
from collections import defaultdict

while True:
    a, b = [int(i) for i in input().split()]
    if a == 0 and b == 0:
        exit()
    H = [int(i) for i in input().split()]
    W = [int(i) for i in input().split()]

    h_cnt = defaultdict(int)
    w_cnt = defaultdict(int)

    for h in H:
        h_cnt[h] += 1
    for w in W:
        w_cnt[w] += 1

    ans = 0

    for k, v in h_cnt.items():
        ans += k * max(v, w_cnt[k])
        w_cnt[k] = 0

    for k, v in w_cnt.items():
        ans += k * v

    print(ans)

Solution (Python):
from collections import defaultdict

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


def solve():
    H, W = map(int, input().split())
    if H == 0 and W == 0:
        exit()

    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    d_a = defaultdict(int)
    for a in A:
        d_a[a] += 1

    d_b = defaultdict(int)
    for b in B:
        d_b[b] += 1

    ans = 0
    for v in set(A + B):
        ans += v*max(d_a[v], d_b[v])
    print(ans)


while True:
    solve()

Solution (Python):
while 1:
    w,d=map(int,input().split())
    if w==0:
        break
    W=list(map(int,input().split()))
    D=list(map(int,input().split()))
    ans=0
    table=[0 for i in range(21)]
    for n in W:
        ans+=n
        table[n]+=1
    for n in D:
        if table[n]>0:
            table[n]-=1
        else:
            ans+=n
    print(ans)