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

Solution in C (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdbool.h>

int main(void){
  int work,c,i,j,n,m,Nsum=0,Msum=0,sa,hf,ns[100]={0},ms[100]={0};
  bool Nlow=false;

  while(1){
    scanf("%d %d",&n,&m);
    if(n==0&&m==0) break;
    for(i=0;i<n;i++){
      scanf("%d",&ns[i]);
      Nsum+=ns[i];
    }
    for(i=0;i<m;i++){
      scanf("%d",&ms[i]);
      Msum+=ms[i];
    }
    if(Nsum>Msum){
      sa=Nsum-Msum;
    }else{
      sa=Msum-Nsum;
      Nlow=true;
    }
    
    for(i=0;i<n;i++){
      for(j=0;j<n-1;j++){
	if(ns[i]<ns[j]){
	  work = ns[i];
	  ns[i] = ns[j];
	  ns[j] = work;
	}
      }
    }

    for(i=0;i<m;i++){
      for(j=0;j<m-1;j++){
	if(ms[i]<ms[j]){
	  work = ms[i];
	  ms[i] = ms[j];
	  ms[j] = work;
	}
      }
    }

    if(sa>=2&&sa%2==0){
      hf=sa/2;
      while(1){//hf to 1
	if(hf==1) break;
	hf--;
	sa--;
      }
      switch(Nlow){
      case true:
	while(1){
	  for(i=0;i<n;i++){
	    if(ns[i]==hf){
	      for(j=0;j<m;j++){
		if(ms[j]==sa){
		  printf("%d %d\n",hf,sa);
		  goto RESET;
		}
	      }
	    }else if(ns[i]>hf){
	      hf++;
	      sa++;
	      break;
	    }
	    hf++;
	    sa++;
	  }
	}
	break;
      case false:
	while(1){
	  for(i=0;i<m;i++){
	    if(ms[i]==hf){
	      for(j=0;j<n;j++){
		if(ns[j]==sa){
		  printf("%d %d\n",sa,hf);
		  goto RESET;
		}
	      }
	    }else if(ns[i]>hf){
	      hf++;
	      sa++;
	      break;
	    }
	    hf++;
	    sa++;
	  }
	}
	break;
      default:break;
      }
    }else puts("-1");

  RESET:
    for(i=0;i<100;i++){
      ns[i]-=ns[i];
      ms[i]-=ms[i];
    }
    sa-=sa;
    Nlow=false;
    Nsum-=Nsum;
    Msum-=Msum;
    c-=c;
  }
  return 0;
}