Equal Total Scores

Description


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
s
1
s 2
...
s n
s n+1
s n+2
...
s n+m

The first line of a dataset contains two numbers n and m delimited by a space, wheren is the number of cards that Taro has and m is the number of cards that Hanako has. The subsequentn+m lines list the score for each of the cards, one score per line. The firstn scores (from s1 up tosn) are the scores of Taro's cards and the remainingm scores (from sn+1 up tosn+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

两个人的牌彼此交换一张使得两人牌的数字和相等,即使两人原先和相等也要进行交换一次


#include <iostream>
using namespace std;
int numn[100];
int numm[100];
int m,n;
int main()
{
	while(cin>>n>>m&&m!=0||n!=0)
	{
		int summ=0,sumn=0,mul;
		for(int i=0;i<n;i++)
		{
			cin>>numn[i];
			sumn+=numn[i];
		}
		for(int i=0;i<m;i++)
		{
			cin>>numm[i];
			summ+=numm[i];
		}
		mul=sumn-summ;
		if(mul%2!=0)
			cout<<"-1"<<endl;
		else
		{
			int k=0,l=0,min=0,flag=0,sum=250;
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
					if(numn[i]-numm[j]==mul/2)
					{
						flag=1;
						if(sum>numn[i]+numm[j])
						{
							sum=numn[i]+numm[j];
							k=i;
							l=j;
						}
					}
			}
			if(flag)
				cout<<numn[k]<<" "<<numm[l]<<endl;
			else
				cout<<"-1"<<endl;
		}		
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值