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:
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline()[:-1]
def MI(): return map(int, sys.stdin.readline().split())
def MI1(): return map(int1, sys.stdin.readline().split())
def MF(): return map(float, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LF(): return list(map(float, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
dij = [(0, 1), (1, 0), (0, -1), (-1, 0)]

def main():
    while 1:
        n,m=MI()
        if n==0:break
        aa=[II() for _ in range(n)]
        sa=sum(aa)
        sb=0
        bb=set()
        for _ in range(m):
            b=II()
            sb+=b
            bb.add(b)
        d=sa-sb
        if d%2:
            print(-1)
            continue
        d//=2
        for a in aa:
            if a-d in bb:
                print(a,a-d)
                break
        else:
            print(-1)

main()

Submission status: Accepted

Solution code:
while 1:
    n,m = map(int,input().split())
    if n==0: break
    t = sorted([int(input()) for _ in range(n)])
    h = sorted([int(input()) for _ in range(m)])
    d = sum(t) - sum(h)
    if d%2 != 0: print(-1);continue
    for i in range(n):
        if t[i] - d//2 in h:
            print(t[i], t[i] - d//2)
            break
    else:
        print(-1)

Submission status: Accepted

Solution code:
while 1:
    n, m = [int(i) for i in 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)

    sum_taro = sum(taro)
    sum_hanako = sum(hanako)

    ex_min = max(taro) + max(hanako)
    min_taro = 0
    min_hanako = 0

    for i in range(n):
        for j in range(m):
            ex_sum_taro = sum_taro - taro[i] + hanako[j]
            ex_sum_hanako = sum_hanako + taro[i] - hanako[j]
            if ex_sum_taro == ex_sum_hanako:
                if (min_taro == 0 and min_hanako == 0) or \
                   (min_taro + min_hanako < ex_min):
                    min_taro = taro[i]
                    min_hanako = hanako[j]
                    ex_min = min_taro + min_hanako

    if min_taro == 0 and min_hanako == 0:
        print(-1)
    else:
        print(min_taro, min_hanako)

Submission status: Accepted

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

    L = []
    M = []
    for _ in range(n):
        L.append(int(input()))

    for _ in range(m):
        M.append(int(input()))

    diff = sum(M) - sum(L)
    if diff %2:
        print(-1)
        continue
    else:
        diff = diff//2

    f = 0
    setm = set(M)
    for i in sorted(set(L)):
        j = i + diff
        if j in setm:
            f += 1
            break

    if f > 0:
        print(i,j)
    else:
        print(-1)

Submission status: Accepted

Solution code:
ans_list = []

while True:
    n,m = map(int,input().split())
    if (n,m) == (0,0):
        break
    S = [int(input()) for _ in range(n)]
    T = [int(input()) for _ in range(m)]
    ss = sum(S)
    tt = sum(T)
    ans = (101,101)
    for s in S:
        for t in T:
            if ss - s + t == tt - t + s and s + t < sum(ans):
                ans = (s,t)
    if ans == (101,101):
        ans = -1
    else:
        ans = "{} {}".format(ans[0],ans[1])
    ans_list.append(ans)

for ans in ans_list:
    print(ans)

Submission status: Accepted

Solution code:
#2009_A
"""
def solve(n,p):
    a = [0]*n
    for _ in range(1000000):
        for i in range(n):
            if p:
                p -= 1
                a[i] += 1
                if p == 0:
                    if sum(a)-a[i] == 0:
                        return i
            else:
                p += a[i]
                a[i] = 0

while 1:
    n,p = [int(x) for x in input().split()]
    if n == 0:
        break
    print(solve(n,p))
"""

#2008_A
def solve(n,m):
    s = [int(input()) for i in range(n)]
    t = [int(input()) for i in range(m)]
    s_s = sum(s)
    s_t = sum(t)
    ans = (-1,float("inf"))
    for i in range(n):
        for j in range(m):
            a = s_t-s_s-2*t[j]+2*s[i]
            if a == 0:
                if s[i]+t[j] < sum(ans):
                    ans = (s[i],t[j])
    if ans[0] < 0:
        print(-1)
    else:
        print(*ans)
while 1:
    n,m = [int(x) for x in input().split()]
    if n == 0:
        break
    solve(n,m)

Submission status: Accepted

Solution code:
while True:
    n,m = map(int,input().split())
    if n == 0:
        exit()
    t = []
    h = []
    ans = []
    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]):
                ans.append([t[i] + h[j], t[i], h[j]])
    ans = sorted(ans)
    if len(ans) != 0:
        print(str(ans[0][1]) + " " + str(ans[0][2]))
    else:
        print(-1)

Submission status: Accepted

Solution code:
from itertools import product
while True:
  N, M = map(int, input().split())
  if N==M==0:
    break
  A = [int(input()) for _ in range(N)]
  B = [int(input()) for _ in range(M)]
  sumA = sum(A)
  sumB = sum(B)
  ans = [-1]
  wa = float("inf")
  for (ia, a), (ib, b) in product(enumerate(A), enumerate(B)):
    if sumA - a + b == sumB - b + a:
      if wa > a + b:
        wa = a + b
        ans = a, b
  print(*ans)

Submission status: Accepted

Solution code:
def main(n, m):
    a = [int(input()) for i in range(n)]
    b = [int(input()) for i in range(m)]
    suma = sum(a)
    sumb = sum(b)
    ans = []
    for ai in a:
        for bi in b:
            if suma - ai + bi == sumb - bi + ai:
                ans.append((ai, bi))
    if ans == []:
        print(-1)
        return
    ans.sort(key=lambda x: x[0] + x[1])
    print(ans[0][0],ans[0][1])


while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    main(n, m)

Submission status: Accepted

Solution code:
while True:
  n,m=map(int,input().split())
  if n==0 and m==0:
    break
  tarou=[]
  hanako=[]
  kumit=[]
  kumih=[]
  wa=[]
  for i in range(n):
    tarou.append(int(input()))
  for i in range(m):
    hanako.append(int(input()))
  for i in range(n):
    a=tarou[i]
    for j in range(m):
      b=hanako[j]
      x=sum(tarou)-a+b
      y=sum(hanako)-b+a
      if x==y:
        kumit.append(tarou[i])
        kumih.append(hanako[j])
        wa.append(tarou[i]+hanako[j])
  if wa!=[]:
    for i in range(len(wa)):
      if wa[i]==min(wa):
        print(kumit[i],kumih[i])
        break
  else:
    print(-1)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AOJ A

while 1:
    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)]
    change = []
    for i in range(n):
        for j in range(m):
            taro[i],hanako[j] = hanako[j],taro[i]
            if sum(taro) == sum(hanako):
                change.append([hanako[j],taro[i],taro[i]+hanako[j]])
            taro[i],hanako[j] = hanako[j],taro[i]
    #print(change)
    change = sorted(change, key = lambda x:x[2])
    if len(change) == 0:
        print(-1)
    else:
        print(change[0][0],change[0][1])

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if n == 0:
        break
    a = [0] * n
    b = [0] * m
    sum_a = 0
    sum_b = 0
    for i in range(n):
        a[i] = int(input())
        sum_a += a[i]
    for i in range(m):
        b[i] = int(input())
        sum_b += b[i]
    a = sorted(a)
    b = sorted(b)
    d = sum_a - sum_b
    if d % 2 != 0:
        print(-1)
    else:
        d /= 2
        ok = False
        for i in range(n):
            for j in range(m):
                if a[i] - b[j] == d:
                    print(a[i], b[j])
                    ok = True
                    break
            if ok:
                break
        if not ok:
            print(-1)

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if n == 0:
        break
    ans = False
    taro = sorted([int(input()) for _ in range(n)])
    hana = [int(input()) for _ in range(m)]
    diff = sum(taro) - sum(hana)
    hana = set(hana)
    for k in taro:
        if k - diff / 2 in hana:
            print(k, k - diff // 2)
            ans = True
            break
    if ans is False:
        print(-1)

Submission status: Accepted

Solution code:
def fun(n, m):
        n_total = 0
        N = []
        for _ in range(n):
                s = int(input())
                N.append(s)
                n_total += s
        m_total = 0
        M = []
        for _ in range(m):
                s = int(input())
                M.append(s)
                m_total += s
        flag = False
        for i in N:
                for j in M:
                        if n_total -i+j==m_total+i-j:
                                print(i,j)
                                flag = True
                                break
                if flag:
                        break
        if not flag:
                print(-1)
n, m = map(int, input().split())
while n != 0:
        fun(n, m)
        n, m = map(int, input().split())

Submission status: Accepted

Solution code:
while True:
  n, m = list(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)
  sb = sum(b)
  a.sort()
  b.sort()

  ta, tb = 1e9, 1e9
  for i in range(n):
    for j in range(m):
      if (a[i]-b[j])*2==sa-sb:
        if ta+tb>a[i]+b[j]:
          ta, tb = a[i], b[j]

  if ta==1e9:
    print(-1)
  else:
    print(ta, tb)

Submission status: Accepted

Solution code:
while True:
    n,m=map(int,input().split())
    if n==0:
        break
    l1=[int(input()) for _ in range(n)]
    l2=[int(input()) for z in range(m)]
    sum1=sum(l1)
    sum2=sum(l2)
    l1.sort()
    l2.sort()
    a1=0
    a2=0
    while True:
        if a1+1>n or a2+1>m:
            print("-1")
            break
        else:
            if sum1-l1[a1]+l2[a2]==sum2+l1[a1]-l2[a2]:
                print("{} {}".format(l1[a1],l2[a2]))
                break
            elif sum1-l1[a1]+l2[a2]>sum2+l1[a1]-l2[a2]:
                a1+=1
            else:
                a2+=1

Submission status: Accepted

Solution code:
# Equal Total Scores

ans = []

while True:
    flag = False
    n, m = map(int, input().split())
    if n == 0 and m == 0:
        break
    s = [int(input()) for i in range(n)]
    t = [int(input()) for i in range(m)]

    sum_s = sum(s)
    sum_t = sum(t)

    for i in range(n):
        for j in range(m):
            #print("sum_s = {}, s[i] = {}, t[j] = {}".format(sum_s, s[i], t[j]))
            if (sum_s - s[i] + t[j]) == (sum_t - t[j] + s[i]):
                flag = True
                ans.append([s[i], t[j]])
                break
        if flag:
            break

    if not flag:
        ans.append([-1, -1])

for i in ans:
    if -1 in i:
        print(-1)
    else:
        print("{} {}".format(i[0], i[1]))

Submission status: Accepted

Solution code:
CARDMAX = 100
POINTMAX = 100
    
def main():
    head = []
    c_taro = []
    c_hana = []

    while True:
        # 初期化
        head.clear()
        c_taro.clear()
        c_hana.clear()

        # 先頭情報
        head = input()
        head = head.split(" ")

        # 終了処理
        if head == ['0', '0']:
            return 0

        for j in range(int(head[0])):
            c_taro.append(int(input()))

        for j in range(int(head[1])):
            c_hana.append(int(input()))

        sum_taro = showsum(c_taro)
        sum_hana = showsum(c_hana)
        avee = showave(sum_taro, sum_hana)
        toave = avee - sum_taro

        print(showchcard(c_taro, c_hana, toave))
"""
        print("headdata::")
        print(head)
        print("c_taro::")
        print(c_taro)
        print("c_hana::")
        print(c_hana)
        print("sum_taro::")
        print(sum_taro)
        print("sum_hana::")
        print(sum_hana)
        print("average::")
        print(avee)
        print("")
"""

def showsum(array):
    summ = 0
    for item in array:
        summ += item
    return summ

def showave(a, b):
    return (a + b) / 2

def showchcard(tarray, harray, toave):
    wa = CARDMAX * 2 + 1
    for tnum in tarray:
        for hnum in harray:
            if hnum - tnum == toave:
                if wa >= (hnum + tnum):
                    tans = tnum
                    hans = hnum
                    wa = tans + hans

    if wa != CARDMAX * 2 + 1:
        result = str(tans) + " " + str(hans)
        return result
    else:
        return -1

main()

Submission status: Accepted

Solution code:
while 1:
    n,m=map(int,input().split())
    if n==m==0:break
    taro=[]
    hana=[]
    for i in range(n):taro.append(int(input()))
    for i in range(m):hana.append(int(input()))
    s_t=sum(taro)
    s_h=sum(hana)
    if (s_t+s_h)%2==1:print(-1)
    else:
        if s_t==s_h:
            taro.sort()
            for i in taro:
                if i in hana:
                    print(i,i)
                    break
            else:print(-1)
        elif s_t>s_h:
            i=(s_t-s_h)//2
            hana.sort()
            for j in hana:
                if i+j in taro:
                    print(i+j,j)
                    break
            else:print(-1)
        elif s_t<s_h:
            i=(s_h-s_t)//2
            taro.sort()
            for j in taro:
                if i+j in hana:
                    print(j,i+j)
                    break
            else:print(-1)

Submission status: Accepted

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

    t_sum = sum(t)
    h_sum = sum(h)

    ans_sum = 10e9
    ans = [0, 0]
    for ti in t:
        for hi in h:
            if t_sum - ti + hi == h_sum - hi + ti and ti + hi < ans_sum:
                ans_sum = ti + hi
                ans = [ti, hi]

    if ans == [0, 0]:
        print(-1)
    else:
        print(ans[0], ans[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 change():
    for i in a:
      for j in b:
        if(i-j) * 2 == suma - sumb:
          print(i, j)
          return
    print(-1)
  
  change()

Submission status: Accepted

Solution code:
# coding: utf-8
while 1:
    n,m=map(int,input().split())
    if n==0:
        break
    taro=[]
    hana=[]
    for i in range(n):
        taro.append(int(input()))
    for i in range(m):
        hana.append(int(input()))
    t_sum=sum(taro)
    h_sum=sum(hana)
    d=t_sum-h_sum
    mn=99999999
    ans=[-1]
    for i in range(n):
        for j in range(m):
            if (t_sum-taro[i]+hana[j])==(h_sum-hana[j]+taro[i]):
                if mn>taro[i]+hana[j]:
                    mn=taro[i]+hana[j]
                    ans=(taro[i],hana[j])
    print(*ans)

Submission status: Accepted

Solution code:
def solve(a, b):
    a.sort()
    b.sort()
    d = sum(a) - sum(b)
    if d % 2 != 0:
        return -1
    for x in a:
        for y in b:
            if x - y == d/2:
                return " ".join(map(str,[x, y]))
            if x - y <  d/2:
                break
    return -1

while True:
    n, m = map(int, raw_input().split())
    if n == 0 and m == 0:
        break
    s = [int(raw_input()) for _ in range(n+m)]
    #print s[:n], s[n:], sum(s[:n]), sum(s[n:])
    print solve(s[:n], s[n:])

Submission status: Runtime Error

Solution code:
def answer(n, m, taro, hanako):
    for t in sorted(taro):
        fot h in (hanako):
            if sum(taro) - t + h == sum(hanako) - h + t:
                return f'{t} {h}'
    return -1
    
    
while True:
    n, m = 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()))
    print(answer(n, m, taro, hanako))

Submission status: Runtime Error

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

while True:
    n, m = 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()))
    print(answer(n, m, taro, hanako)

Submission status: Runtime Error

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 '{t} {h}'

while True:
    n, m = 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()))
    print(answer(n, m, taro, hanako)

Submission status: Runtime Error

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 '{t} {h}'
    return -1

while True:
    n, m = 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()))
    print(answer(n, m, taro, hanako)

Submission status: Runtime Error

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 '{t} {h}'
    return -1

while True:
    n, m = 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()))
    print(answer(n, m, taro, hanako)

Submission status: Runtime Error

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 '{t} {h}'
    return -1

while True:
    n, m = 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()))
    print(answer(n, m, taro, hanako))

Submission status: Runtime Error

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 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: Runtime Error

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 pairs_leastsum == []:
        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: Runtime Error

Solution code:
import numpy as np
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=np.array(allCards[:N])
    Hanako=np.array(allCards[N:])
    Tsum=np.sum(Taro)
    Hsum=np.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: Runtime Error

Solution code:
#include<iostream>
#include<vector>
using namespace std;
int main(){
    while(1){
        int n,m,x,sum1=0,sum2=0,ans=100000,num,ax=0,ay=0;
        vector<int> N,M;
        cin >> n >> m;
        if(n==0&&m==0)  break;
        for(int i=n;i--;){
            cin >> x;
            sum1+=x;
            N.push_back(x);
        }
        for(int i=m;i--;){
            cin >> x;
            sum2+=x;
            M.push_back(x);
        }
        num = sum1 - sum2;
        //cout << sum1 << " " <<sum2 << endl;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(num == 2*(N[i]-M[j])&&ans>N[i]+M[j]){
                    ans = N[i]+M[j];
                    ax=N[i];
                    ay=M[j];
                }
            }
        }
        if(ax==0&&ay==0)    cout << "-1\n";
        else cout << ax << " " << ay << "\n";
    }
}