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

Solution code:
if __name__ == "__main__":
    while True:
        n,m = list(map(int,input().strip().split()))
        if n ==0 and m ==0:break
        tarou = []
        hanako = []
        ans = (0,0)
        for _ in range(n): tarou.append(int(input()))
        for _ in range(m): hanako.append(int(input()))

        for i in range(n):
            for j in range(m):
                if sum(tarou[:i])+sum(tarou[i + 1:])+hanako[j] == sum(hanako[:j]) + sum(hanako[j+1:]) + tarou[i]:
                    if sum(ans) == 0:ans = (tarou[i],hanako[j])
                    elif sum(ans) > tarou[i]+ hanako[j]: ans = (tarou[i],hanako[j])
        if ans == (0,0):print(-1)
        else:print("%d %d" % (ans[0],ans[1]))

Submission status: Accepted

Solution code:
Answer = []

while True:
    inp = input().split()
    n = int(inp[0])
    m = int(inp[1])
    c = n**2 + m**2
    if c == 0:
        break

    T = []
    for t in range(n):
        inp = int(input())
        T.append(inp)

    H = []
    for h in range(m):
        inp = int(input())
        H.append(inp)

    Temp = []
    for i in range(n):
        for j in range(m):
            if sum(T)-2*T[i] == sum(H)-2*H[j]:
                TTemp = [T[i],H[j]]
                Temp.append(TTemp)
    if len(Temp) == 0:
        Answer.append(-1)
    else:
        S = sum(Temp[0])
        flg = 0
        for k in range(len(Temp)):
            if sum(Temp[k]) < S:
                flg = k
            S = min(S,sum(Temp[k]))
        Answer.append(str(Temp[flg][0])+" "+str(Temp[flg][1]))

for l in range(len(Answer)):
    print(Answer[l])

Submission status: Accepted

Solution code:
while True:
    n,m = map(int,input().split())
    if n == m == 0: break
    c1 = [int(input()) for i in range(n)]
    c2 = [int(input()) for i in range(m)]
    diff = sum(c1) - sum(c2)
    if diff % 2 == 1:
        print(-1)
        continue
    for v1 in sorted(c1):
        v2 = v1 - diff//2
        if v2 in c2:
            print('{0} {1}'.format(v1, v2))
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
while True:
    N,M=map(int,input().split())
    if N==0 and M==0:
        break
    allCards=[]
    for i in range(N+M):
        allCards.append(int(input()))
                        
    Taro=allCards[:N]
    Hanako=allCards[N:]
    Tsum=sum(Taro)
    Hsum=sum(Hanako)
    diff=Tsum-Hsum
    tempsum=1e8
    Tchange=-1
    Hchange=-1
    for i in range(N):
        for j in range(M):
            tempdiff=(Taro[i]-Hanako[j])*2
            if diff==tempdiff:
                if(tempsum>Taro[i]+Hanako[j]):
                    tempsum=Taro[i]+Hanako[j]
                    Tchange=Taro[i]
                    Hchange=Hanako[j]
    if Tchange==-1:
        print(-1)
    else:
        print(Tchange,Hchange)

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if n == m == 0:
        quit()
    s1, s2 = [], []
    for i in range(n):
        s1.append(int(input()))
    for i in range(m):
        s2.append(int(input()))
    s1.sort()
    s2.sort(reverse=True)
    sum1 = sum(s1)
    sum2 = sum(s2)
    temp = 101
    for i in s1:
        for j in s2:
            if sum1 - i + j == sum2 - j + i:
                if temp > abs(i-j):
                    temp = abs(i-j)
                    ans = [i, j]
    if temp == 101:
        print(-1)
    else:
        print(ans[0], ans[1])

Submission status: Accepted

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

    if n == 0 and m == 0:
        break

    taro = sorted([int(raw_input()) for _ in range(n)])
    hanako = sorted([int(raw_input()) for _ in range(m)])

    diff = sum(taro) - sum(hanako)

    flag = False

    def fnc():
        global flag
        for i in taro:
            for j in hanako:
                if (i - j) * 2 == diff:
                    print i, j
                    flag = True
                    return

    fnc()

    if not flag:
        print -1

Submission status: Accepted

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

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

    taro.sort()
    hanako.sort()

    diff = sum(taro) - sum(hanako)

    if diff % 2 != 0:
        print(-1)
        continue

    diff /= 2

    ti = 0
    hi = 0

    flag = False
    while ti < n and hi < m:
        if taro[ti] - hanako[hi] == diff:
            print(taro[ti], hanako[hi])
            flag = True
            break
        else:
            if taro[ti] - hanako[hi] < diff:
                ti += 1
            else:
                hi += 1

    if not flag:
        print(-1)

Submission status: Accepted

Solution code:
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:
        n,m = LI()
        if n == 0:
            break

        a = [I() for _ in range(n)]
        b = [I() for _ in range(m)]
        sa = sum(a)
        sb = sum(b)
        r = -1
        for ai in sorted(a, reverse=True):
            for bi in sorted(b, reverse=True):
                if sa - ai + bi == sb - bi + ai:
                    r = '{} {}'.format(ai, bi)
                    break
        rr.append(r)

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


print(main())

Submission status: Accepted

Solution code:
while 1:
    n,m = list(map(int,input().split()))
    if not n and not m:break

    ln=sorted([int(input()) for _ in range(n)])
    lm=sorted([int(input()) for _ in range(m)])
    cd_n=set(ln)
    cd_m=set(lm)

    g=(sum(lm)-sum(ln))
    f=False
    if g%2==1:f=True
    else:g=g/2
    bf=False
    for i in cd_m:
        if f:break
        for j in cd_n:
            if i-j==g:bf=True
            if bf:break
        if bf:break
    if bf and not f:print(j,i)
    else:print("-1")

Submission status: Accepted

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

    for i in range(n):
        st.append(int(input()))

    for i in range(m):
        sh.append(int(input()))

    # st.sort()
    # sh.sort()

    ans = [100000, 100000]

    if (sum(st)-sum(sh))%2 == 1:
        print(-1)
        continue

    diff = (sum(st)-sum(sh))//2
    for i in range(len(st)):
        for j in range(len(sh)):
            if st[i]-sh[j] == diff and st[i]+sh[j] < sum(ans):
                ans = [st[i], sh[j]]

    if(ans == [100000, 100000]):
        print(-1)
    else:
        print(str(ans[0])+" "+str(ans[1]))

Submission status: Accepted

Solution code:
while True:
  n, m = map(int, input().split())
  if n == 0:
    break
  alst = [int(input()) for _ in range(n)]
  blst = [int(input()) for _ in range(m)]
  alst.sort()
  blst.sort()
  suma = sum(alst)
  sumb = sum(blst)
  def search():
    for a in alst:
      for b in blst:
        if (a - b) * 2 == suma - sumb:
          print(a, b)
          return
    print(-1)
  search()

Submission status: Accepted

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 > 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,raw_input().split())
    if n==m==0:break
    taro=[input() for i in range(n)]
    hana=[input() for i in range(m)]
    L=[]
    sumta=sum(taro)
    sumha=sum(hana)
    for i in range(n):
        temp1=sumta-taro[i]
        temp2=sumha+taro[i]
        for j in range(m):
            if temp1+hana[j]==temp2-hana[j]:
                L.append((taro[i]+hana[j],taro[i],hana[j]))
    if L<>[]:
        L.sort()
        print L[0][1],L[0][2]
    else:
        print -1

Submission status: Accepted

Solution code:
while 1:
	n,m = map(int,raw_input().split())
	if n == 0: break
	taro = sorted([int(raw_input()) for i in range(n)])
	hana = [int(raw_input()) for i in range(m)]
	d = sum(hana) - sum(taro)
	t,h = 101,101
	for i in taro:
		if d/2.0 + i in hana:
			t,h = i,d/2 + i
			break
	print "%d %d"%(t,h) if t < 101 else -1

Submission status: Accepted

Solution code:
while True:
    n, m = map(int,input().split())
    if n==0 and m==0:
        break
    a = [int(input()) for i in range(n)]
    b = [int(input()) for i in range(m)]
    a.sort()
    b.sort()
    c=0
    d=0
    for i in range(len(a)):
        if d==1:
            break
        for j in range(len(b)):
            if sum(a)-a[i]+b[j]==sum(b)+a[i]-b[j]:
                print(a[i],b[j])
                d=1
                break
            else:
                c+=1
    if c==len(a)*len(b):
        print('-1')

Submission status: Accepted

Solution code:
while True:
    n,m = map(int,input().split())
    if n==0 and m==0: break
    taro = [int(input()) for i in range(n)]
    hanako = [int(input()) for i in range(m)]
    d = sum(taro)-sum(hanako)
    def search():
       for i in taro:
           for j in hanako:
               if (i-j)*2 == d: 
                   print(i,j)
                   return
       print(-1)
    search()

Submission status: Accepted

Solution code:
while True:
    n,m=map(int,input().split())
    if n==m==0:
        break
    ta=[int(input()) for i in range(n)]
    ha=[int(input()) for i in range(m)]
    taro=sorted(ta)
    hana=sorted(ha)
    taha=(sum(ta)-sum(ha))/2
    kai=0
    if taha<0:
        for i in range(len(taro)):
            if taro[i]+abs(taha) in hana:
                kai=1
                print(taro[i],int(taro[i]+abs(taha)))
                break
    else:
        for i in range(len(taro)):
            if taro[i]-abs(taha) in hana:
                kai=1
                print(taro[i],int(taro[i]-abs(taha)))
                break
    if kai!=1:
        print('-1')

Submission status: Accepted

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

    k=0
    for i in range(n):
        for j in range(m):
            cn[i],cm[j]=cm[j],cn[i]
            if(sum(cn)==sum(cm)):
                print(cm[j],cn[i])
                k+=1
                break
            else:
                cn[i],cm[j]=cm[j],cn[i]
        else:
            continue
        break
    
    if(k!=1):
        print(-1)

Submission status: Accepted

Solution code:
while True:
    n,m = map(int,input().split())
    if n == 0:
        exit()
    t = []
    h = []
    num = []
    for i in range(n):
        t.append(int(input()))
    for i in range(m):
        h.append(int(input()))
    for i in range(n):
        for j in range(m):
            if (sum(t) - t[i] + h[j]) == (sum(h) - h[j] + t[i]):
                num.append([t[i] + h[j], t[i], h[j]])
    num = sorted(num)
    if len(num) != 0:
        print(str(num[0][1]) + " " + str(num[0][2]))
    else:
        print(-1)

Submission status: Accepted

Solution code:
while True:
    m,n=map(int,input().split())
    if m==0 and n==0:
        break
    A=[]
    B=[]
    S=0
    T=0
    V=0
    for i in range(m):
        a=int(input())
        A.append(a)
        S=S+a
    for i in range(n):
        b=int(input())
        B.append(b)
        T=T+b
    for j in range(m):
        for k in range(n):
            J=A[j]
            K=B[k]
            X=S-J+K
            Y=T-K+J
            if X==Y and V==0:
                print(J,K)
                V=1
    if V==0:
        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)]
    a.sort()
    b.sort()
    sumA=sum(a)
    sumB=sum(b)
    def search():
        for i in a:
            for j in b:
                if (i-j)*2==sumA-sumB:
                    print(i,j)
                    return
        print(-1)
    search()

Submission status: Accepted

Solution code:
#入力はn, mの2行
#0 0が入力の終わりなのでwhile Trueのif
#for(1, n)は太郎、for(n+1, n+m)は花子、sum+=でそれぞれ合計を出す
#交換するとき(差/2)を小さい方から出したxに足すと大きい方から出す数に等しい
#ソートして同じi番目で差がsa/2だったら答え
#見つからんのやったらprint(-1)
while True:
    n, m = map(int, input().split())
    
    if n == 0 and m == 0:
        break
    
    else:
        taro = []
        hana = []
        
        for i in range(n):
            taro.append(int(input()))
        for i in range(m):
            hana.append(int(input()))
        
        taro.sort
        hana.sort
            
        sum_t = sum(taro)
        sum_h = sum(hana)

        sa = sum_t - sum_h #taroが多かったら正
        
        f = 0
        
        for i in range(n):
            if f == 1:
                break
            for j in range(m):
                if taro[i] == hana[j] + sa/2:
                    print(taro[i],hana[j])
                    f = 1
                    break
        
        if f == 0:
            print(-1)

Submission status: Accepted

Solution code:
while True:
   n,m = map(int, input().split())
   if n == 0 and m == 0:
      break
   T = []
   H = []
   for i in range(n):
      t = int(input())
      T.append(t)
   for j in range(m):
      h = int(input())
      H.append(h)
      
   T.sort()
   H.sort()
   min = []
   
   if (sum(T) - sum(H))%2!=0 or (sum(H) - sum(T))%2!=0:
      pass
   elif sum(T) > sum(H):
      s = (sum(T) - sum(H))//2
      for p in range(n):
         for q in range(m):
            if min == []:
               if T[p] - s == H[q]:
                  min = [T[p], H[q]]
            else:
               break
   elif sum(T) < sum(H):
      s = (sum(H) - sum(T))//2
      for p in range(n):
         for q in range(m):
            if min == []:
               if T[p] + s == H[q]:
                  min = [T[p], H[q]]
            else:
               break
   else:
      for p in range(n):
         for q in range(m):
            if T[p] == H[q]:
               if sum(min) > T[p]+H[q] or min == []:
                  min = [T[p], H[q]]

   if min == []:
      print('-1')
   else:
      print(*min)

Submission status: Accepted

Solution code:
while True:
    n,m = map(int,input().split())
    if n==0 and m==0:
        break
    lt=[]
    lh= []
    for i in range(n):
        sn=int(input())
        lt.append(sn)
    for j in range(m):
        sm=int(input())
        lh.append(sm)
    s = (sum(lt)+sum(lh))/2
    st= abs(s-sum(lt))
    lt.sort()  
    lh.sort()
    if sum(lt) < sum(lh):
        ans = -1
        for i in lt:
            if ans==-1:
                for j in lh:
                    if i + st == j:
                        print(i,j)
                        ans = 1
                        break
        if ans==-1:
            print(ans)
    else:
        ans = -1
        for j in lh:
            if ans==-1:
                for i in lt:
                    if j + st == i:
                        print(i,j)
                        ans = 1
                        break 
        if ans==-1:
            print(ans)

Submission status: Accepted

Solution code:
#23:52

while True:
    taro_cards,hanako_cards = map(int,input().split())
    if taro_cards==0 and hanako_cards==0:
        break
    taros_card = [int(input()) for _ in range(taro_cards)]
    hanakos_card = [int(input()) for _ in range(hanako_cards)]

    diff = sum(taros_card) - sum(hanakos_card)
    taros_card.sort()
    hanakos_card.sort()

    answer_i = 0
    answer_j = 0
    answer_total = 99999
    
    for i in range(taro_cards):
        for j in range(hanako_cards):
            if sum(taros_card) - 2*taros_card[i] == sum(hanakos_card) - 2*hanakos_card[j]  and taros_card[i] + hanakos_card[j] <= answer_total:
                answer_i = taros_card[i] 
                answer_j = hanakos_card[j]
                answer_total = taros_card[i] + hanakos_card[j]
    
    if answer_total == 99999:
        print(-1)
    else:
        print(str(answer_i)  + " "+str(answer_j) )

Submission status: Accepted

Solution code:
while True:
    n,m=map(int,input().split())
    if n==0 and m==0:
        break
    st=[int(input()) for t in range(n)]
    sh=[int(input()) for h in range(m)]
    st.sort()
    sh.sort()
    x=0
    y=0
    for i in range(len(st)):
        if y==1:
            break
        for j in range(len(sh)):
            if sum(st)-st[i]+sh[j]==sum(sh)+st[i]-sh[j]:
                print(st[i],sh[j])
                y=1
                break
            else:
                x+=1
    if x==len(st)*len(sh):
        print('-1')

Submission status: Accepted

Solution code:
def f():
    for i in t:
        for j in h:
            if (i-j)*2==sumt-sumh:
                print(i,j)
                return
    print(-1)
            

while True:
    n,m=map(int,input().split())
    if n==0:
        break
    t=[int(input()) for i in range(n)]
    h=[int(input()) for j in range(m)]
    t.sort()
    h.sort()
    sumt=sum(t)
    sumh=sum(h)
    f()

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

    ans1 = []; ans2 = []
        
    for i in sorted(taro):
        for j in sorted(hanako):
            if sum(taro)-i+j == sum(hanako)-j+i:
                ans1.append(i); ans2.append(j)
    
    if len(ans1)==len(ans2)==0: print(-1)
    else: print(ans1[0],ans2[0])

Submission status: Accepted

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

while True:
   n, m = map(int, input().split())
   if n + m == 0: 
      break
   
   sums = 0 #太郎のカードの和
   TARO = []
   for i in range(n):
      taro = int(input())
      TARO.append(taro)
      sums += taro

   sumh = 0 #花子のカードの
   HANAKO = []
   for i in range(m):
      hanako = int(input())
      HANAKO.append(hanako)
      sumh += hanako

   #print(sums, sumh)
   #print(HANAKO)
   
   mid = (sums + sumh) / 2  #合計点が等しくなるときの点数
   dif = abs(sums - mid)
   #print(mid, dif)
   
   TARO.sort()
   HANAKO.sort()
   
   c = 0
   for i in range(n):
      for j in range(m):
         if (sums + sumh) % 2 == 1:
            c += 0
         else:
            if sums == sumh:
               if TARO[i] == HANAKO[j]:
                  print(TARO[i], HANAKO[j])
                  c += 1
                  break
            if TARO[i] - HANAKO[j] == dif:
               if sums > sumh:
                  print(TARO[i], HANAKO[j])
                  c += 1
                  break
            if HANAKO[j] - TARO[i] == dif:
               if sumh > sums:
                  print(TARO[i], HANAKO[j])
                  c += 1
                  break
      else:#上記のどれかに当てはまったら２重ループを抜ける
         continue
      break

   if c == 0:
      print(-1)

Submission status: Accepted

Solution code:
while True:
    n,m=map(int,input().split())
    if n==0 and m==0:
        break
    a=[int(input()) for i in range(n)]
    b=[int(input()) for i in range(m)]
    ans=[-1]
    va=100000
    for i in range(n):
        for j in range(m):
            if sum(a)-a[i]+b[j]==sum(b)-b[j]+a[i]:
                if (a[i]+b[j])<va:
                    va=a[i]+b[j]
                    ans=a[i],b[j]
    print(*ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
input = sys.stdin.readline
##

while True:
    n, m = input().rstrip().split()
    n, m = int(n), int(m)
    if n + m == 0:
        sys.exit()
    
    taro = []
    hanako = []
    for _ in range(n):
        taro.append(int(input().rstrip()))
    for _ in range(m):
        hanako.append(int(input().rstrip()))

    card_diff = sum(taro) - sum(hanako)
    if card_diff % 2 == 1:
        print("-1")
        continue
    else:
        card_diff = card_diff // 2

    found_flag = False
    for piv_taro, piv_hanako in itertools.product(range(0, n), range(0, m)):
        if (taro[piv_taro] - hanako[piv_hanako]) == card_diff:
            found_flag = True
            break
    if found_flag == True:
        print("{} {}".format(taro[piv_taro], hanako[piv_hanako]))
    else:
        print("-1")

Submission status: Accepted

Solution code:
while True:
    m,n = map(int,input().split())
    
    if  m== 0 and n == 0:
        break
    
    ta=[]
    ha=[]
    for i in range(m):
        ta.append(int(input()))
        
    for i in range(n):
        ha.append(int(input()))
    
    ta=sorted(ta)
    ha=sorted(ha)
    ans1=[]
    ans2=[]
    for i in sorted(ta):
        for j in sorted(ha):
            if sum(ta) - i + j == sum(ha) - j + i:
                ans1.append(i)
                ans2.append(j)
    
    if len(ans1)==0 and len(ans2)==0:
        print(-1)
    
    else:
        print(ans1[0],ans2[0])

Submission status: Accepted

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

    tarou_cards = []
    hanako_cards = []
    cards_size = n + m

    total = 0
    t_total = 0
    h_total = 0

    for i in range(0,cards_size):
        point = int(input())
        total += point
        if n >= i+1:
            tarou_cards.append(point)
            t_total += point
        else:
            hanako_cards.append(point)
            h_total += point
    tarou_cards.sort()
    hanako_cards.sort()

    if total%2 != 0:
        print("-1")
        continue
    
    exchangeFlg = False
    target = t_total - total/2
    for t_card in tarou_cards:
        for h_card in hanako_cards:
            diff = t_card - h_card
            if diff == target:
                print("{} {}".format(t_card,h_card))
                exchangeFlg = True
                break
        if exchangeFlg:
            break
    if exchangeFlg != True:
        print("-1")

Submission status: Accepted

Solution code:
while True:
    try:
        n, m = map(int, input().split())
        
        if n==0 and m==0:
            break
    except:
        break
    
    Taro = []
    for i in range(n):
        s = int(input())
        Taro.append(s)
    Hana = []
    for i in range(m):
        s = int(input())
        Hana.append(s)
    
    koukan = []
    kknsum = []
    for i in range(n):
        taro = Taro
        tarou = taro[i]
        taro.pop(i)
        tsum = sum(taro)
        taro.insert(i,tarou)
        for j in range(m):
            hana = Hana
            hanak = hana[j]
            hana.pop(j)
            hsum = sum(hana)
            hana.insert(j,hanak)
            sumTa = tsum + hanak
            sumHa = hsum + tarou
            if sumTa==sumHa:
                koukan.append([tarou, hanak])
                kknsum.append(tarou+hanak)
    
    if koukan==[]:
        print('-1')
    
    else:
        kknmin = min(kknsum)
        index = kknsum.index(kknmin)
        kk = koukan[index]
        print(kk[0], kk[1])

Submission status: Accepted

Solution code:
while True :
    n, m = map(int, input().split())
    
    if n == 0 :
        break
    
    else :
        T = []
        H = []
        
        for i in range(n) :
            T.append(int(input()))
            
        for i in range(m) :
            H.append(int(input()))
        
        Ts = sorted(T)
        Hs = sorted(H)
        
        sumT = sum(Ts)
        sumH = sum(Hs)
        
        d = (sumT - sumH) / 2
        D = abs(d)
        
        f = 0
        # flag
        
        if d < 0 :
            for i in range(len(Ts)) :
                if (Ts[i] + D) in Hs :
                    print(Ts[i], int(Ts[i] + D))
                    f = 1
                    break
        else :
            for i in range(len(Ts)) :
                if (Ts[i] - D) in Hs :
                    print(Ts[i], int(Ts[i] - D))
                    f = 1
                    break
        
        if f != 1 :
            print(-1)

Submission status: Accepted

Solution code:
while True:
    s=[]
    t=[]
    n,m=map(int,input().split())
    if n==0 and m==0:
        break
    else:
        for i in range(n):
            x=int(input())
            s.append(x)
        for i in range(m):
            y=int(input())
            t.append(y)
        s.sort()
        t.sort()
        def search():
            for a in s:
                 for b in t:
                    if (a-b)*2==sum(s)-sum(t):
                        print(a,b)
                        return
            print(-1)
        search()

Submission status: Accepted

Solution code:
while True :
    n, m = map(int, input().split())
    if n == 0 and m == 0 :
        break
    
    list_n = list(int(input()) for i in range(n))
    list_m = list(int(input()) for i in range(m))
    list_n.sort
    list_m.sort
    sum_n = sum(list_n)
    sum_m = sum(list_m)
    if sum_n >= sum_m :
        sa = sum_n - sum_m
        result = -1
        for i in range(n) :
            if result == 1 :
                break
            for j in range(m):
                if list_n[i] == list_m[j] + sa / 2 :
                    print(list_n[i], list_m[j])
                    result = 1
                    break
        if result == -1 :
            print(-1)
            
            
    elif sum_n <= sum_m :
        sa = sum_m - sum_n
        result = -1
        for i in range(n) :
            if result == 1 :
                break
            for j in range(m):
                if list_m[j] == list_n[i] + sa / 2:
                    print(list_n[i], list_m[j])
                    result = 1
                    break
        if result == -1 :
            print(-1)

Submission status: Accepted

Solution code:
def get_two_int():
    two_int = input().split()
    for i in range(2):
        two_int[i] = int(two_int[i])
    return two_int

def make_sorted_num_list(num):
    num_list = [None] * num
    for i in range(num):
        num_list[i] = int(input())
    sorted_num_list = sorted(num_list)
    return sorted_num_list

def shuffle(first_list, second_list):
    flag = False
    dif_value = sum(first_list) - sum(second_list)
    if abs(dif_value) % 2 != 0:
        return flag, -1
    for i in first_list:
        for j in second_list:
            if i - j == (dif_value // 2):
                flag = True
                return flag, i, j
    return flag, -1


if __name__ == "__main__":
    while True:
        taro_num, hanako_num = get_two_int()
        if taro_num == hanako_num == 0:
            break
        taro_list = make_sorted_num_list(taro_num)
        hanako_list = make_sorted_num_list(hanako_num)
        ans = shuffle(taro_list, hanako_list)
        if ans[0] == False:
            print(ans[1])
        else:
            print(ans[1], ans[2])

Submission status: Accepted

Solution code:
while 1:
    n,m = map(int,input().split())
    if n == 0 and m == 0:
        break
    taro = [int(input()) for i in range(n)]
    hanako = [int(input()) for i in range(m)]
    ans = []
    for i in range(n):
        for j in range(m):
            taro[i],hanako[j] = hanako[j],taro[i]
            if sum(taro) == sum(hanako):
                ans.append([hanako[j],taro[i]])
            taro[i],hanako[j] = hanako[j],taro[i]
    Min = 9999999999
    for i,j in ans:
        if Min > (i+j):
            Min = (i+j)
            ans_taro = i
            ans_hanako = j
    if len(ans) != 0:
        print(ans_taro,ans_hanako)
    else:
        print(-1)

Submission status: Accepted

Solution code:
while (True):
    n,m=map(int,input().split())
    if n==m==0:
        exit()
    lst_a=[int(input()) for _ in range(n)]
    lst_b=[int(input()) for _ in range(m)]
    a_sum=sum(lst_a)
    b_sum=sum(lst_b)
    min_lst=[]
    
    for a in lst_a:
        keisan_a=a_sum
        keisan_b=b_sum
        for b in lst_b:
            if keisan_a -a+b == keisan_b +a-b:
                min_lst.append((a+b,a,b))
    
    
    if min_lst:
        ans=sorted(min_lst)    
        print(ans[0][1],ans[0][2])
                
    else:print(-1)