Problem p00465 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j, k;
  int n;
  while (cin >> n && n) {
    int a[2][502][502], w[2], h[2], sx[2], sy[2];
    memset(a, -1, sizeof(a));
    for (i = 0; i < 2; i++) {
      cin >> w[i] >> h[i] >> sx[i] >> sy[i];
      for (j = 1; j < h[i] + 1; j++)
        for (k = 1; k < w[i] + 1; k++)
          cin >> a[i][k][j];
    }
    vector<pair<int, int>> b[2];
    for (i = 0; i < 2; i++) {
      b[i].reserve(w[i] * h[i]);
      b[i].push_back(make_pair(0, 0));
      priority_queue<pair<int, pair<int, int>>,
                     deque<pair<int, pair<int, int>>>,
                     greater<pair<int, pair<int, int>>>>
          c;
      c.push(make_pair(1, make_pair(sx[i], sy[i])));
      while (c.empty() == 0) {
        int p, x, y;
        p = c.top().first;
        x = c.top().second.first;
        y = c.top().second.second;
        c.pop();
        if (a[i][x][y] != -1) {
          a[i][x][y] = -1;
          if (b[i].back().first == p) {
            b[i].back().second++;
          } else {
            b[i].push_back(make_pair(p, b[i].back().second + 1));
          }
          if (a[i][x + 1][y] != -1)
            c.push(make_pair(max(p, a[i][x + 1][y]), make_pair(x + 1, y)));
          if (a[i][x - 1][y] != -1)
            c.push(make_pair(max(p, a[i][x - 1][y]), make_pair(x - 1, y)));
          if (a[i][x][y + 1] != -1)
            c.push(make_pair(max(p, a[i][x][y + 1]), make_pair(x, y + 1)));
          if (a[i][x][y - 1] != -1)
            c.push(make_pair(max(p, a[i][x][y - 1]), make_pair(x, y - 1)));
        }
      }
    }
    int sm = 200000000;
    for (i = 0; i < (int)b[0].size() &&
                b[0][i].second + b[1][(int)b[1].size() - 1].second < n;
         i++)
      ;
    for (; i < (int)b[0].size() && b[0][i].second < n; i++) {
      int mn = 1, mx = b[1].size() - 1;
      for (;;) {
        if (0) {
        } else if (b[1][(mn + mx) / 2 - 1].second >= n - b[0][i].second) {
          mx = (mn + mx) / 2 - 1;
        } else if (b[1][(mn + mx) / 2].second < n - b[0][i].second) {
          mn = (mn + mx) / 2 + 1;
        } else {
          break;
        }
      }
      sm = min(sm, b[0][i].first + b[1][(mn + mx) / 2].first);
    }
    if (i < (int)b[0].size())
      sm = min(sm, b[0][i].first);
    cout << sm << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
int main() {
  int i, j, k;
  int r;
  while (cin >> r && r) {
    int a[2][502][502], w[2], h[2], x[2], y[2];
    memset(a, -1, sizeof(a));
    for (i = 0; i < 2; i++) {
      cin >> w[i] >> h[i] >> x[i] >> y[i];
      for (j = 1; j < h[i] + 1; j++)
        for (k = 1; k < w[i] + 1; k++)
          cin >> a[i][k][j];
    }
    unsigned b[2][502][502];
    memset(b, -1, sizeof(b));
    for (i = 0; i < 2; i++) {
      queue<pair<pair<int, int>, int>> c;
      c.push(make_pair(make_pair(x[i], y[i]), 0));
      while (c.empty() == 0) {
        int u, v, z;
        u = c.front().first.first;
        v = c.front().first.second;
        z = c.front().second;
        c.pop();
        if (a[i][u][v] != -1 && b[i][u][v] > (unsigned)max(a[i][u][v], z)) {
          b[i][u][v] = max(a[i][u][v], z);
          c.push(make_pair(make_pair(u - 1, v), max(a[i][u][v], z)));
          c.push(make_pair(make_pair(u + 1, v), max(a[i][u][v], z)));
          c.push(make_pair(make_pair(u, v - 1), max(a[i][u][v], z)));
          c.push(make_pair(make_pair(u, v + 1), max(a[i][u][v], z)));
        }
      }
    }
    map<int, int> c[2];
    map<int, int>::iterator it, jt;
    for (i = 0; i < 2; i++) {
      c[i][0] = 0;
      for (j = 1; j < h[i] + 1; j++)
        for (k = 1; k < w[i] + 1; k++)
          c[i][b[i][k][j]]++;
    }
    for (i = 0; i < 2; i++) {
      it = jt = c[i].begin();
      for (jt++; jt != c[i].end();) {
        (*jt).second += (*it).second;
        it++;
        jt++;
      }
    }
    unsigned mn = -1;
    for (it = c[0].begin(); it != c[0].end(); it++) {
      for (jt = c[1].begin();
           jt != c[1].end() && (*it).second + (*jt).second < r; jt++)
        ;
      if (jt != c[1].end())
        mn = min(mn, (unsigned)((*it).first + (*jt).first));
      if ((*it).second >= r)
        break;
    }
    cout << mn << endl;
  }
  return 0;
}