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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int N, Q;
typedef pair<unsigned int, pair<unsigned int, string>> P;
vector<P> que;
int main() {
  while (cin >> N >> Q, N || Q) {
    que.clear();
    unsigned int A[1010] = {0};
    for (int i = 0; i < 1010; i++) {
      A[i] = 0;
    }

    for (int i = 0; i < N; i++) {
      string str;
      unsigned int eray, wesy;
      cin >> str >> eray >> wesy;
      que.push_back(P(wesy, mp(eray, str)));
      A[i] = wesy;
    }
    sort(que.begin(), que.end());
    sort(A, A + N);
    for (int i = 0; i < Q; i++) {
      unsigned wes;
      cin >> wes;
      bool flag = true;
      // cout<<que[k].first-wes<<endl;
      rep(j, N) {
        if (wes > que[j].first - que[j].second.first && wes <= que[j].first) {
          cout << que[j].second.second << " "
               << wes - que[j].first + que[j].second.first << endl;
          flag = false;
          break;
        }
      }
      if (flag) {
        cout << "Unknown" << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, b, n) for (int i = b; i < n; i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int N, Q;
typedef pair<int, pair<int, string>> P;
vector<P> que;
int main() {
  while (cin >> N >> Q, N || Q) {
    que.clear();
    int A[1010] = {0};

    for (int i = 0; i < N; i++) {
      string str;
      int eray, wesy;
      cin >> str >> eray >> wesy;
      que.push_back(P(wesy, mp(eray, str)));
      A[i] = wesy;
    }
    sort(que.begin(), que.end());
    sort(A, A + N);
    for (int i = 0; i < Q; i++) {
      int wes;
      cin >> wes;
      int k = lower_bound(A, A + N, wes) - A;
      // cout<<que[k].first-wes<<endl;
      if (que[k].second.first > que[k].first - wes) {
        cout << que[k].second.second << " "
             << que[k].second.first - (que[k].first - wes) << endl;
      } else {
        cout << "Unknown" << endl;
      }
    }
    /*(!que.empty()){
            cout <<que.top().first<<endl;
            que.pop();
    }*/
  }
  return 0;
}