Problem:


Problem A: Equal Total Scores

 -->

Taro and Hanako have numbers of cards in their hands.  
Each of the cards has a score on it.
Taro and Hanako wish to make the total scores of their cards equal
by exchanging one card in one's hand with one card in the other's hand.
Which of the cards should be exchanged with which?

Note that they have to exchange their
cards even if they already have cards of the same total score.

Input

The input consists of a number of datasets. 
Each dataset is formatted as follows.

n m

s1 

s2 

...

sn 

sn+1 

sn+2 

...

sn+m 

The first line of a dataset contains two numbers
n and m delimited by a space, where n
is the number of cards that Taro has
and m is the number of cards that Hanako has. 
The subsequent n+m lines 
list the score for each of the cards,
one score per line. The first n scores
(from s1 up to sn)
are the scores of Taro's cards 
and the remaining m scores 
(from sn+1 up to sn+m)
are Hanako's.

The numbers n and m
are positive integers no greater than 100.  Each score
is a non-negative integer no greater than 100.

The end of the input is indicated by a line containing two zeros delimited
by a single space.

Output

For each dataset, output a single line containing two numbers delimited
by a single space, where the first number is the score of the card Taro
gives to Hanako and the second number is the score of the card Hanako gives
to Taro. 
If there is more than one way to exchange a pair of cards
that makes the total scores equal, 
output a pair of scores whose sum is the smallest.

In case no exchange can make the total scores equal, output a
single line containing solely -1.
The output must not contain any superfluous characters
that do not conform to the format. 

Sample Input

2 2
1
5
3
7
6 5
3
9
5
2
3
3
12
2
7
3
5
4 5
10
0
3
8
1
9
6
0
6
7 4
1
1
2
1
2
1
4
2
3
4
3
2 3
1
1
2
2
2
0 0

Output for the Sample Input

1 3
3 5
-1
2 2
-1



Sample input:
2 2
1
5
3
7
6 5
3
9
5
2
3
3
12
2
7
3
5
4 5
10
0
3
8
1
9
6
0
6
7 4
1
1
2
1
2
1
4
2
3
4
3
2 3
1
1
2
2
2
0 0


Sample output:
1 3
3 5
-1
2 2
-1

Submission status: Wrong Answer

Solution code:
def impossible(taro_cards, hanako_cards, average):
    if not isinstance(average, int):
        return True
    elif sum(taro_cards) + max(taro_cards) - min(hanako_cards) < average:
        return True
    elif sum(hanako_cards) + max(hanako_cards) - min(taro_cards) < average:
        return True
    else:
        return False


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

    taro_cards = []
    hanako_cards = []
    change_pairs = []

    for i in range(n_taro):
        taro_cards.append(int(input()))
    for i in range(n_hanako):
        hanako_cards.append(int(input()))

    average = (sum(taro_cards) + sum(hanako_cards)) / 2

    if impossible(taro_cards, hanako_cards, average):
        print(-1)
    else:
        # record pairs of cards to be changed
        for i in range(len(taro_cards)):
            for j in range(len(hanako_cards)):
                if sum(taro_cards[:i]) + sum(taro_cards[i+1:]) + hanako_cards[j] == average:
                    change_pairs.append((taro_cards[i], hanako_cards[j]))
                    # print("{0} {1}".format(taro_cards[i], hanako_cards[j]))

        # print the pair of cards to be changed
        pairs_leastsum = change_pairs[0]
        for i in range(1, len(change_pairs)):
            if sum(change_pairs[i]) <= sum(pairs_leastsum):
                pairs_leastsum = change_pairs[i]
        print(*pairs_leastsum)

Submission status: Wrong Answer

Solution code:
# def impossible(taro_cards, hanako_cards, average):
#     if (sum(taro_cards) + sum(hanako_cards)) % 2 == 1:
#         return True
#     elif sum(taro_cards) + max(taro_cards) - min(hanako_cards) < average:
#         return True
#     elif sum(hanako_cards) + max(hanako_cards) - min(taro_cards) < average:
#         return True
#     else:
#         return False


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

    taro_cards = []
    hanako_cards = []
    change_pairs = []

    for _ in range(n_taro):
        taro_cards.append(int(input()))
    for _ in range(n_hanako):
        hanako_cards.append(int(input()))

    average = (sum(taro_cards) + sum(hanako_cards)) // 2

    # if impossible(taro_cards, hanako_cards, average):
    #     print(-1)
    # else:
    # record pairs of cards to be changed
    for i in range(len(taro_cards)):
        for j in range(len(hanako_cards)):
            if sum(taro_cards[:i]) + sum(taro_cards[i+1:]) + hanako_cards[j] == average:
                change_pairs.append((taro_cards[i], hanako_cards[j]))
                # print("{0} {1}".format(taro_cards[i], hanako_cards[j]))

    # print the pair of cards to be changed
    if change_pairs == []:
        print(-1)
    else:
        pairs_leastsum = change_pairs[0]
        for i in range(1, len(change_pairs)):
            if sum(change_pairs[i]) <= sum(pairs_leastsum):
                pairs_leastsum = change_pairs[i]
        print(*pairs_leastsum)

Submission status: Wrong Answer

Solution code:

while True:
	n, m = map(int, input().split())
	if (n, m) == (0, 0):
		break
	res = None
	taro = [int(input()) for i in range(n)]
	hana = [int(input()) for i in range(m)]
	taros = sum(taro)
	hanas = sum(hana)
	diff = abs(hanas-taros)

	for i in range(n):
		for j in range(m):
			if diff == (abs(taro[i] - hana[j])*2):
				if res == None or sum(res) > (taro[i] + hana[j]):
					res = (taro[i], hana[j])

	if res == None:
		print(-1)
	else:
		print(' '.join(map(str, res)))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
while True:
    l = map(int,raw_input().split())
    if l == [0,0]:
        break
    else:
        check = 0
        l1 = []
        for i in range(l[0]):
            n = int(raw_input())
            l1.append(n)
        l1.sort()
        l2 = []
        for i in range(l[1]):
            n = int(raw_input())
            l2.append(n)
        l2.sort()
        if sum(l1) < sum(l2):
            l3 = l1
            l1 = l2
            l2 = l3
        if (sum(l1) - sum(l2))% 2 != 0:
            print -1
        else:
            a = (sum(l1) - sum(l2))/2
            for num in l1:
                if num - a in l2:
                    print num-a,num
                    check = 1
                    break
            if check == 0:
                print -1

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Here your code !
while True:
    l = map(int,raw_input().split())
    if l == [0,0]:
        break
    else:
        check = 0
        taro = 0
        l1 = []
        for i in range(l[0]):
            n = int(raw_input())
            l1.append(n)
        l1.sort()
        l2 = []
        for i in range(l[1]):
            n = int(raw_input())
            l2.append(n)
        l2.sort()
        if sum(l1) < sum(l2):
            l3 = l1
            l1 = l2
            l2 = l3
            taro = 1
        if (sum(l1) - sum(l2))% 2 != 0:
            print -1
        else:
            a = (sum(l1) - sum(l2))/2
            for num in l1:
                if num - a in l2:
                    if taro == 0:
                        print num-a,num
                    else:
                        print num,num-a
                    check = 1
                    break
            if check == 0:
                print -1

Submission status: Wrong Answer

Solution code:
while True:
    N, M = map(int, raw_input().split())
    if N == M == 0:
        break
    A = [int(raw_input()) for _ in xrange(N)]
    B = [int(raw_input()) for _ in xrange(M)]
    s = sum(A) - sum(B)
    A = set(A)
    B = set(B)
    if s % 2 != 0:
        print -1
        continue
    for a in A:
        if a - s / 2 in B:
            print a, a - s / 2
            break
    else:
        print -1

Submission status: Wrong Answer

Solution code:
while 1:
    n,m = map(int,raw_input().split())
    if n == 0 and m == 0: break
    t = []
    h = []
    pair = []
    score = []
    
    for i in range(n):
        t.append(int(raw_input()))
    for i in range(m):
        h.append(int(raw_input()))    
    for i in range(n):
        
        for j in range(m):
            #new copy
            tmp_t = list(t)
            tmp_h = list(h)

            #trade
            tmp = tmp_h[j]
            tmp_h[j] = tmp_t[i]
            tmp_t[i] = tmp
            
            #compare
            if sum(tmp_t) == sum(tmp_h):
                pair.append([t[i],h[j]])
                score.append(t[i] + h[j])
    if pair == []:
        print "-1"
    else:
        print pair[score.index(min(score))]

Submission status: Wrong Answer

Solution code:
import sys

while True:
    n, m = map(int, sys.stdin.readline().split())
    if n == 0 and m ==0:
        break
    
    taro = []
    for i in xrange(n):
        x = int(sys.stdin.readline())
        
        taro.append(x)
    hanako = []
    for i in xrange(m):
        x = int(sys.stdin.readline())
        
        hanako.append(x)
    
    sum_taro = sum(taro)
    sum_hanako = sum(hanako)
    
    diff = abs(sum_taro - sum_hanako) / 2.0
    if diff != int(diff):
        print -1
        continue
    MAX = max(taro + hanako)
    result = MAX
    for t in taro:
        if t + diff in hanako:
            if result != -1 and result > t:
                result = t
    if result != MAX:
        print result, int(result + diff)
    else:
        print -1

Submission status: Wrong Answer

Solution code:
import sys

while True:
    n, m = map(int, sys.stdin.readline().split())
    if n == 0 and m ==0:
        break
    
    taro = []
    for i in xrange(n):
        x = int(sys.stdin.readline())
        
        taro.append(x)
    hanako = []
    for i in xrange(m):
        x = int(sys.stdin.readline())
        
        hanako.append(x)
    
    sum_taro = sum(taro)
    sum_hanako = sum(hanako)
    
    diff = abs(sum_taro - sum_hanako) / 2.0
    if diff != int(diff):
        print -1
        continue
    MAX = max(taro + hanako) + 1
    result = MAX
    for t in taro:
        if t + diff in hanako:
            if result != -1 and result > t:
                result = t
    if result != MAX:
        print result, int(result + diff)
    else:
        print -1

Submission status: Wrong Answer

Solution code:
import sys

while True:
    n, m = map(int, sys.stdin.readline().split())
    if n == 0 and m ==0:
        break
    
    taro = []
    for i in xrange(n):
        x = int(sys.stdin.readline())
        
        taro.append(x)
    hanako = []
    for i in xrange(m):
        x = int(sys.stdin.readline())
        
        hanako.append(x)
    
    sum_taro = sum(taro)
    sum_hanako = sum(hanako)
    
    diff = (sum_taro - sum_hanako) / 2.0
    if diff != int(diff):
        print -1
        continue
    MAX = max(taro + hanako) + 1
    result = MAX
    for t in taro:
        if t + diff in hanako:
            if result != -1 and result > t:
                result = t
    if result != MAX:
        print result, int(result + diff)
    else:
        print -1

Submission status: Accepted

Solution code:
while True:
    n,m=map(int,input().split())
    if n==0:break
    a=[int(input())for _ in range(n)]
    b=[int(input())for _ in range(m)]
    sa=sum(a)
    sb=sum(b)
    f=0
    for i in a:
        for j in b:
            if sa-i+j==sb-j+i:
                print(i,j)
                f=1
                break
        if f:
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
# AOJ 1153: Equal Total Scores
# Python3 2018.7.15 bal4u

INF = 0x7fffff
while True:
	n, m = map(int, input().split())
	if n == 0: break
	a = [int(input()) for i in range(n)]
	b = [int(input()) for i in range(m)]
	sa = sum(a) - sum(b)
	ss = INF
	for i in range(n):
		for j in range(m):
			if ((a[i]-b[j])<<1) == sa:
				if a[i]+b[j] < ss:
					aa, bb = a[i], b[j]
					ss = aa + bb;
	if ss == INF: print(-1)
	else: print(aa, bb)

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break
    taro = [int(input()) for _ in range(n)]
    hanako = [int(input()) for _ in range(m)]
    trade_n = []
    trade_m = []
    for t in taro:
        for h in hanako:
            if (sum(taro) - t + h) == (sum(hanako) - h + t):
                trade_n.append(t)
                trade_m.append(h)
    if len(trade_n) >= 1:
        ans = [0, 100000]
        for i in range(len(trade_n)):
            ans[1] = min(trade_n[i] + trade_m[i], ans[1])
            if ans[1] == trade_n[i] + trade_m[i]:
                ans[0] = i
        print("{} {}".format(trade_n[ans[0]], trade_m[ans[0]]))
    elif len(trade_n) == 0:
        print(-1)

Submission status: Accepted

Solution code:
anslist = []
while True:
    ans = 0
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break

    taro = []
    hanako = []
    for i in range(n):
        s = int(input())
        taro.append(s)
    for i in range(m):
        s = int(input())
        hanako.append(s)
    ans = -1
    for i in range(n):
        for j in range(m):
            if sum(taro) - taro[i] + hanako[j] == sum(hanako) - hanako[j] + taro[i]:
                ans = [taro[i], hanako[j]]
                break
        if ans != -1:
            break

    anslist.append(ans)

for i in anslist:
    if i == -1:
        print(-1)
    else:
        print(i[0], i[1])

Submission status: Accepted

Solution code:
def answer(n, m, taro, hanako):
    for t in sorted(taro):
        for h in sorted(hanako):
            if sum(taro) - t + h == sum(hanako) - h + t:
                return f'{t} {h}'
    return -1


while True:
    n, m = map(int, input().split()) # 組み込み関数input()は標準入力から読み込まれた1行を返す．
    if n == 0 and m == 0:
        break
    taro = []
    hanako = []
    for i in range(n):
        taro.append(int(input()))
    for i in range(m):
        hanako.append(int(input()))
    print(answer(n, m, taro, hanako))

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

while True:
    n,m = inpl()
    if n == 0:
        break
    else:
        aa = [inp() for _ in range(n)]
        bb = [inp() for _ in range(m)]
        Sa = sum(aa)
        Sb = sum(bb)
        for a in aa:
            for b in bb:
                if Sa-a+b == Sb+a-b:
                    print(a,b)
                    break
            else:
                continue
            break
        else:
            print(-1)

Submission status: Accepted

Solution code:
while True:
  n,m = map(int,input().split())
  if n + m == 0:
    break
  Taro = [int(input()) for _ in range(n)]
  Hana = [int(input()) for _ in range(m)]
  st = sum(Taro)
  sh = sum(Hana)
  for t in range(n):
    for h in range(m):
      if st - 2*Taro[t] == sh - 2*Hana[h]:
        print(Taro[t],Hana[h])
        st = 114514
        break
      if t==n-1 and h==m-1:
        print(-1)
    if st==114514:
      break

Submission status: Accepted

Solution code:
while True:
    (n, m) = [int(x) for x in input().split()]
    if n==0 and m==0:
        exit()
    a = []
    b = []
    for i in range(n):
        a.append(int(input()))
    for i in range(m):
        b.append(int(input()))
    a_sum = sum(a)
    b_sum = sum(b)
    target = a_sum - b_sum
    found = False
    for ai in a:
        for bi in b:
            if (ai-bi)*2 == target:
                print(ai, bi)
                found = True
            if found:
                break
        if found:
            break
    if found == False:
        print(-1)

Submission status: Accepted

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

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        while True:
            n, m = map(int, raw_input().split())
            if n == 0 and m == 0:
                break
            sn = []
            for i in range(n):
                sn.append(input())
            sm = []
            for i in range(m):
                sm.append(input())
            diff = (sum(sn) - sum(sm)) * 1.0 / 2
            # if diff != int(diff):
            #     print -1
            #     continue
            ans = []
            flg = False
            for i in sn:
                for j in sm:
                    if i - j == diff:
                       flg = True
                       ans.append((i, j))
            if flg == False:
                print -1
                continue
            min_points = min(i + j for i, j in ans)
            for i, j in ans:
                if i + j == min_points:
                    print ' '.join([str(i), str(j)])
                    break
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Accepted

Solution code:

while 1:
    n, m = list(map(int, input().split()))
    if n == 0:break
    n_v = [int(input()) for i in range(n)]
    m_v = [int(input()) for i in range(m)]

    n_s = sum(n_v)
    m_s = sum(m_v)

    swap_min = 99999999

    x = n_s - m_s

    res = '-1'
    for nv in n_v:
        for mv in m_v:
            
            if x == 2 * (nv - mv) and swap_min > nv + mv:
                swap_min = nv + mv
                res = '%d %d' % (nv,mv)
    print(res)

Submission status: Accepted

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

while True:
    n,m = map(int,input().split())
    if n == 0 and m == 0:
        break
    taro = [int(input()) for i in range(0,n)]
    hanako = [int(input()) for i in range(0,m)]
    taro_hand = sum(taro)
    hanako_hand = sum(hanako)
    ans = -1
    for i in sorted(taro):
        for j in sorted(hanako):
            if ans != -1:
                break
            if taro_hand + (j - i) == hanako_hand + (i - j):
                ans = str(i) + ' ' + str(j)
    print(ans)

Submission status: Accepted

Solution code:
while 1:
	n, m = map(int, raw_input().split(" "))
	if (n,m) == (0,0):
		break
	tarou = [int(raw_input()) for i in range(n)]
	t_total = sum(tarou)
	hanako = [int(raw_input()) for i in range(m)]
	h_total = sum(hanako)
	pair = (101,101)
	for t in tarou :
		for h in hanako:
			if t_total - t + h == h_total - h + t:
				if t + h <= sum(pair):
					pair = (t,h)
	if pair == (101,101):
		print -1
	else :
		print pair[0], pair[1]

Submission status: Accepted

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

(n, m) = map(int, raw_input().split())
while n!=0 or m!=0:
    T = [int(raw_input()) for i in range(n)]
    H = [int(raw_input()) for i in range(m)]
    min = [1000, 1000]
    for i in range(n):
        for j in range(m):
            if(sum(T)-T[i]+H[j] == sum(H)+T[i]-H[j] and sum(min) > H[j] + T[i]):
                min = [T[i], H[j]];
    if min == [1000, 1000]:
        print -1
    else:
        print min[0], min[1]
    
    (n, m) = map(int, raw_input().split())

Submission status: Accepted

Solution code:
#!/usr/bin/python3
while True:
    n, m = list(map(int, input().split()))
    if n == 0 and m == 0: break

    taro = []
    hanako = []
    for i in range(n):
        taro.append(int(input()))
    for i in range(m):
        hanako.append(int(input()))

    sum_taro = sum(taro)
    sum_hana = sum(hanako)
    dif = sum_taro - sum_hana
#    print("taro:",taro)
#    print("hana:",hanako)
#    print("dif:",dif)

    ans_t, ans_h = 101, 101 # 0 <= point <= 100
    for t in taro:
        for h in hanako:
            if (sum_taro - t + h) == (sum_hana - h + t) \
                    and (t + h) < (ans_t + ans_h):
                        ans_t, ans_h = t, h
    if (ans_t + ans_h) == 202:
        print(-1)
    else:
        print(ans_t, ans_h)

#    sign_t = 1
#    sign_h = 1
#    if sum_taro < sum_hana:
#        sign_h = -1
#    elif sum_taro > sum_hana:
#        sign_t = -1
#    #if dif = 0 do nothing
#
#    print("signs(t,h):",sign_t, sign_h) 
#    for t in taro:
#        for h in hanako:
#            t *= sign_h
#            h *= sign_t
#            if (sum_taro + h) == (sum_hana + t):
#                print(abs(t), abs(h))
#                break
#        else:
#            continue
#        break
#    else:
#        print(-1)

Submission status: Accepted

Solution code:
def impossible(taro_cards, hanako_cards, average):
    if (sum(taro_cards) + sum(hanako_cards)) % 2 == 1:
        return True
    elif sum(taro_cards) + max(taro_cards) - min(hanako_cards) < average:
        return True
    elif sum(hanako_cards) + max(hanako_cards) - min(taro_cards) < average:
        return True
    else:
        return False


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

    taro_cards = []
    hanako_cards = []
    change_pairs = []

    for _ in range(n_taro):
        taro_cards.append(int(input()))
    for _ in range(n_hanako):
        hanako_cards.append(int(input()))

    average = (sum(taro_cards) + sum(hanako_cards)) // 2

    if impossible(taro_cards, hanako_cards, average):
        print(-1)
    else:
        # record pairs of cards to be changed
        for i in range(len(taro_cards)):
            for j in range(len(hanako_cards)):
                if sum(taro_cards[:i]) + sum(taro_cards[i+1:]) + hanako_cards[j] == average:
                    change_pairs.append((taro_cards[i], hanako_cards[j]))
                    # print("{0} {1}".format(taro_cards[i], hanako_cards[j]))

        # print the pair of cards to be changed
        if change_pairs == []:
            print(-1)
        else:
            pairs_leastsum = change_pairs[0]
            for i in range(1, len(change_pairs)):
                if sum(change_pairs[i]) <= sum(pairs_leastsum):
                    pairs_leastsum = change_pairs[i]
            print(*pairs_leastsum)

Submission status: Accepted

Solution code:

while True:
	n, m = map(int, input().split())
	if (n, m) == (0, 0):
		break
	res = None
	taro = [int(input()) for i in range(n)]
	hana = [int(input()) for i in range(m)]
	taros = sum(taro)
	hanas = sum(hana)
	diff = (hanas-taros)

	for i in range(n):
		for j in range(m):
			if diff == ((hana[j] - taro[i])*2):
				if res == None or sum(res) > (taro[i] + hana[j]):
					res = (taro[i], hana[j])

	if res == None:
		print(-1)
	else:
		print(' '.join(map(str, res)))

Submission status: Accepted

Solution code:
while True:
	n,m = raw_input().split(" ")
	n = int(n)
	m = int(m)
	if n*m==0:
		break
	a_all = 0
	b_all = 0
	A = []
	B = []
	for i in xrange(0,n):
		x = input()
		x = int(x)
		A.append(x)
		a_all += x
	for j in xrange(0,m):
		y = input()
		y = int(y)
		B.append(y)
		b_all += y
	A.sort()
	B.sort()
	sa = abs(a_all - b_all)
	if sa%2==1:
		print -1
	else:
		sa = sa / 2
		if(a_all < b_all):
			sa = sa * -1
		flag = False
		for i in xrange(0,n):
			for j in xrange(0,m):
				if (A[i]-B[j])==sa:
					print A[i],
					print B[j]
					flag = True
					break
			if flag==True:
				break
		if flag==False:
			print -1

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, raw_input().split())

    if n == 0:
        break

    taro = [int(raw_input()) for i in range(n)]
    taro_sum = sum(taro)
    hanako = [int(raw_input()) for j in range(m)]
    hanako_sum = sum(hanako)

    pair = (101, 101)

    for t in taro:
        for h in hanako:
            if taro_sum - t + h == hanako_sum - h + t:
                if h + t <= sum(pair):
                    pair = (t, h)

    if pair == (101, 101):
        print - 1
    else:
        print pair[0], pair[1]

Submission status: Accepted

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

import array
import heapq
import itertools


def solve(taro_cards, hanako_cards):
    sum_taro = sum(taro_cards)
    sum_hanako = sum(hanako_cards)
    candidates = []
    for t, h in itertools.product(taro_cards, hanako_cards):
        if sum_taro - t + h == sum_hanako - h + t:
            heapq.heappush(candidates, (t + h, [t, h]))
    if not candidates:
        return [-1]
    else:
        return candidates[0][1]


if __name__ == "__main__":
    while True:
        n, m = map(int, input().split())
        if n == 0 and m == 0:
            break
        else:
            taro_cards = array.array("B", (int(input()) for i in range(n)))
            hanako_cards = array.array("B", (int(input()) for i in range(m)))
            print(*solve(taro_cards, hanako_cards))

Submission status: Accepted

Solution code:
def solve1():
    N, M = [int(s) for s in input().split()]
    if N == M == 0:
        return False
    taro = [int(input()) for _ in range(N)]
    taro.sort()
    hana = [int(input()) for _ in range(M)]
    hana.sort()
    diff = sum(taro) - sum(hana)
    ans = compare(diff, taro, hana);
    if ans is None:
        print("-1")
    else:
        t, h = ans
        print("{0} {1}".format(t, h))
    return True
    
def compare(diff, taro, hana):
    if len(taro) == 0 or len(hana) == 0:
        return None
    t = taro[0]
    h = hana[0]
    d = (t-h)*2
    if d < diff:
        return compare(diff, taro[1:], hana)
    if d == diff:
        return t, h
    else:
        return compare(diff, taro, hana[1:])
    
if __name__ == "__main__":
    while(solve1()):
        pass

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1153&lang=jp "Equal Total Scores" ????§£??????

while True:
    n, m = map(int, input().split())
    if not n:
        break
    taro = [int(input()) for i in range(n)]
    hanako = [int(input()) for i in range(m)]
    dif = sum(taro) - sum(hanako)
    ans = [-1]
    for i in taro:
        for j in hanako:
            if (i - j) * 2 == dif:
                if sum(ans) < 0 or i + j < sum(ans):
                    ans = [i, j]
    print(*ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
while True:
    l = map(int,raw_input().split())
    if l == [0,0]:
        break
    else:
        check = 0
        taro = 0
        l1 = []
        for i in range(l[0]):
            n = int(raw_input())
            l1.append(n)
        l1.sort()
        l2 = []
        for i in range(l[1]):
            n = int(raw_input())
            l2.append(n)
        l2.sort()
        if sum(l1) < sum(l2):
            l3 = l1
            l1 = l2
            l2 = l3
            taro = 1
        if (sum(l1) - sum(l2))% 2 != 0:
            print -1
        else:
            a = (sum(l1) - sum(l2))/2
            for num in l1:
                if num - a in l2:
                    if taro == 1:
                        print num-a,num
                    else:
                        print num,num-a
                    check = 1
                    break
            if check == 0:
                print -1

Submission status: Accepted

Solution code:
while True:
    N, M = map(int, raw_input().split())
    if N == M == 0:
        break
    A = [int(raw_input()) for _ in xrange(N)]
    B = [int(raw_input()) for _ in xrange(M)]
    s = sum(A) - sum(B)
    A = set(A)
    B = set(B)
    if s % 2 != 0:
        print -1
        continue
    ans = []
    for a in A:
        if a - s / 2 in B:
            ans.append((a, a - s / 2))
    print ' '.join(map(str, min(ans, key=lambda x:x[0]+x[1]))) if len(ans) > 0 else -1

Submission status: Accepted

Solution code:
def search(t,st,h,sh):
    for ta in t:
        for ha in h:
            if st-ta+ha==sh-ha+ta: return ' '.join(map(str,[ta,ha]))
    return -1

while True:
    n,m = map(int,input().split())
    if n==m==0: break
    t = sorted([int(input()) for _ in range(n)])
    h = sorted([int(input()) for _ in range(m)])
    st,sh = sum(t),sum(h)
    r = search(t,st,h,sh)
    print(r)

Submission status: Accepted

Solution code:
# coding: utf-8
def ans(t,h,t_s,h_s):
    for i in t:
        for j in h:
            if t_s - i + j == h_s - j + i:
                return str(i) + " " + str(j)
    return -1
while True:
    n, m = [int(x) for x in input().split()]
    # print(n,m)
    if n == 0 and m == 0:
        break
    t = [int(input()) for i in range(n)]
    h = [int(input()) for i in range(m)]
    t_s = sum(t)
    h_s = sum(h)
    print(ans(t,h,t_s,h_s))

Submission status: Accepted

Solution code:
while 1:
    n,m = map(int,raw_input().split())
    if n == 0 and m == 0: break
    t = []
    h = []
    pair = []
    score = []
    lst = []
    for i in range(n):
        t.append(int(raw_input()))
    for i in range(m):
        h.append(int(raw_input()))    
    for i in range(n):
        
        for j in range(m):
            #new copy
            tmp_t = list(t)
            tmp_h = list(h)

            #trade
            tmp = tmp_h[j]
            tmp_h[j] = tmp_t[i]
            tmp_t[i] = tmp
            
            #compare
            if sum(tmp_t) == sum(tmp_h):
                pair.append([t[i],h[j]])
                score.append(t[i] + h[j])
    if pair == []:
        print "-1"
    else:
        lst = pair[score.index(min(score))]
        print lst[0],lst[1]

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    taro = [int(input()) for i in range(n)]
    hanako = [int(input()) for i in range(m)]

    #??????????????¨????¨?????\???°????????????
    if (sum(taro) + sum(hanako)) % 2 == 1:
        print(-1)
        continue
    half = (sum(taro) + sum(hanako)) // 2
    #???????????????????????????????????±???????????????????????????
    taro_ex = hanako_ex = half
    #??????????????°
    ex_flag = False

    for i in taro:
        for j in hanako:
            #??????????????????????????????????????§??????????°???????????????´??°
            if sum(taro) + j -i == half and sum(hanako) + i - j == half:
                if i + j < taro_ex + hanako_ex:
                    taro_ex = i
                    hanako_ex = j
                    ex_flag = True

    if ex_flag:
        print(taro_ex, hanako_ex)
    else:
        print(-1)

Submission status: Accepted

Solution code:
from sys import stdin, stdout, stderr

while 1:
    n, m = map(int, stdin.readline().split())

    if n + m == 0:
        break

    taro = [int(stdin.readline()) for i in range(n)]
    hana = [int(stdin.readline()) for i in range(m)]

    tsum = sum(taro)
    hsum = sum(hana)

    if (tsum - hsum) % 2:
        print(-1)
        continue

    smin = 10000
    ans1, ans2 = -1, -1

    for i in range(n):
        for j in range(m):
            dif = taro[i] - hana[j]

            if tsum - dif == hsum + dif and taro[i] + hana[j] < smin:
                ans1, ans2 = taro[i], hana[j]
                smin = taro[i] + hana[j]

    if ans1 + ans2 >= 0:
        print(ans1, ans2)
    else:
        print(-1)

Submission status: Accepted

Solution code:
while 1:
    n, m = map(int, input().split())
    if not n:
        break
        
    t_card = [int(input()) for i in range(n)]
    h_card = [int(input()) for i in range(m)]
    total = [-1]
    dif = sum(t_card) - sum(h_card)
    for i in t_card:
        for j in h_card:
            if (i - j) * 2 == dif:
                if sum(total) < 0 or i + j < sum(total):
                    total = [i, j]
   
    print(*total)