Problem p00734 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;
#define N 100
#define INF 10000
int main() {
  int n, m, s;
  int hanako[N];
  int taro[N];
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> taro[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> hanako[i];
    }
    int sum_taro = 0;
    int sum_hanako = 0;
    for (int i = 0; i < n; i++) {
      sum_taro += taro[i];
    }
    for (int i = 0; i < m; i++) {
      sum_hanako += hanako[i];
    }
    int ans_t = INF, ans_h = INF;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int tmp1 = sum_taro + hanako[j] - taro[i];
        int tmp2 = sum_hanako + taro[i] - hanako[j];
        if (tmp1 != tmp2) {
          continue;
        }
        if (taro[i] + hanako[j] < ans_t + ans_h) {
          ans_t = taro[i];
          ans_h = hanako[j];
        }
      }
    }
    if (ans_t == INF) {
      cout << -1 << endl;
    } else {
      cout << ans_t << " " << ans_h << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>

using namespace std;

int main() {
  int n, m, a;

  while (cin >> n >> m) {
    if (n == 0 && m == 0) {
      break;
    }

    int t[100], h[100];
    priority_queue<int, vector<int>, greater<int>> minpq;
    int sumt = 0, sumh = 0;

    for (int i = 0; i < n; i++) {
      cin >> a;
      t[i] = a;
      sumt += a;
    }
    /*
    cout << sumt << endl;
    for(int i=0; i<n;i++){
        cout << t[i] << " ";
    }
    cout <<endl;*/
    for (int i = 0; i < m; i++) {
      cin >> a;
      h[i] = a;
      sumh += a;
    } /*
     cout << sumh << endl;
     for(int i=0; i<m;i++){
         cout << h[i] << " ";
     }*/

    if (sumh == sumt) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (t[i] == h[j]) {
            t[n + minpq.size()] = t[i];
            h[m + minpq.size()] = h[j];
            minpq.push(t[i] + h[j]);
          }
        }
      }
    } else if ((sumt + sumh) % 2 == 0) {
      // cout << "true"<<endl;
      int ans = (sumt + sumh) / 2;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (abs(t[i] - h[j]) == abs(ans - sumt)) {
            //     cout << "true2" << endl;
            t[n + minpq.size()] = t[i];
            h[m + minpq.size()] = h[j];
            //         cout << "1= " << t[i] << endl;
            //      cout << "2=" << h[i] <<endl;
            minpq.push(t[i] + h[j]);
          }
        }
      }
    }

    // cout << "min="<<minpq.size()<< endl;
    if (minpq.size() == 0) {
      cout << "-1" << endl;
    } else {
      //     cout<< t[n-1]<<t[n]<<endl;
      for (int i = 0; i < minpq.size(); i++) {
        // cout << "top=" << minpq.top()<< endl;
        // cout << " error=" << t[n+i] << " " << h[m+i] << endl;
        if (minpq.top() == t[n + i] + h[m + i]) {
          cout << t[n + i] << " " << h[m + i] << endl;
          break;
        }
      }
    }
  }
  return 0;
}