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

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pint pair<int, int>
#define INF 1 << 29
#define MAX 501
using namespace std;

int R, W[2], H[2], X[2], Y[2], L[2][MAX][MAX], d[2][MAX][MAX],
    dd[4] = {0, -1, 0, 1};
map<int, int> m[2];
map<int, int>::iterator ite;
vector<int> lev[2], sum[2];

void solve() {
  for (;;) {
    cin >> R;
    if (!R)
      return;
    FOR(i, 2) {
      cin >> W[i] >> H[i] >> X[i] >> Y[i];
      X[i]--;
      Y[i]--;
      FOR(j, H[i]) FOR(k, W[i]) cin >> L[i][j][k];
      FOR(j, H[i]) FOR(k, W[i]) d[i][j][k] = INT_MAX / 3;
      d[i][Y[i]][X[i]] = 1;
      priority_queue<pint, vector<pint>, greater<pint>> pque;
      pque.push(mp(1, Y[i] * W[i] + X[i]));
      while (!pque.empty()) {
        pint p = pque.top();
        pque.pop();
        int x = p.second / W[i], y = p.second % W[i];
        if (d[i][x][y] < p.first)
          continue;
        FOR(j, 4) {
          int nx = x + dd[j], ny = y + dd[j ^ 1];
          if (nx < 0 || ny < 0 || nx >= H[i] || ny >= W[i])
            continue;
          if (d[i][nx][ny] > max(d[i][x][y], L[i][nx][ny])) {
            d[i][nx][ny] = max(d[i][x][y], L[i][nx][ny]);
            pque.push(mp(d[i][nx][ny], nx * W[i] + ny));
          }
        }
      }
      FOR(j, H[i]) FOR(k, W[i]) m[i][d[i][j][k]]++;
      int tot = 0;
      lev[i].pb(0);
      sum[i].pb(0);
      for (ite = m[i].begin(); ite != m[i].end(); ite++) {
        lev[i].pb(ite->first);
        tot += ite->second;
        sum[i].pb(tot);
      }
    }
    sum[1].pb(INT_MAX);
    int mi = 1 << 30;
    FOR(i, lev[0].size()) {
      int ind = lower_bound(ALL(sum[1]), R - sum[0][i]) - sum[1].begin();
      if (sum[1][ind] == INT_MAX)
        continue;
      mi = min(mi, lev[1][ind] + lev[0][i]);
    }
    cout << mi << endl;
    FOR(i, 2) {
      m[i].clear();
      lev[i].clear();
      sum[i].clear();
    }
  }
}

int main() {
  solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pint pair<int, int>
#define tint tuple<int, int, int>
#define INF 1 << 29
#define MAX 501
using namespace std;

int R, W[2], H[2], X[2], Y[2], L[2][MAX][MAX], d[2][MAX][MAX],
    dd[4] = {0, -1, 0, 1};
map<int, int> m[2];
map<int, int>::iterator ite;

void solve() {
  for (;;) {
    cin >> R;
    if (!R)
      return;
    FOR(i, 2) {
      cin >> W[i] >> H[i] >> X[i] >> Y[i];
      X[i]--;
      Y[i]--;
      FOR(j, H[i]) FOR(k, W[i]) cin >> L[i][j][k];
      FOR(j, H[i]) FOR(k, W[i]) d[i][j][k] = INT_MAX / 3;
      d[i][X[i]][Y[i]] = 1;
      priority_queue<pint, vector<pint>, greater<pint>> pque;
      pque.push(mp(1, X[i] * W[i] + Y[i]));
      while (!pque.empty()) {
        pint p = pque.top();
        pque.pop();
        int x = p.second / W[i], y = p.second - x * W[i];
        if (d[i][x][y] < p.first)
          continue;
        FOR(j, 4) {
          int nx = x + dd[j], ny = y + dd[j ^ 1];
          if (nx < 0 || ny < 0 || nx >= H[i] || ny >= W[i])
            continue;
          if (d[i][nx][ny] > max(d[i][x][y], L[i][nx][ny])) {
            d[i][nx][ny] = max(d[i][x][y], L[i][nx][ny]);
            pque.push(mp(d[i][nx][ny], nx * W[i] + ny));
          }
        }
      }
      FOR(j, H[i]) FOR(k, W[i]) m[i][d[i][j][k]]++;
      for (ite = m[i].begin(); ite != m[i].end(); ite++) {
        if (ite == m[i].begin())
          continue;
        ite->second += (--ite)->second;
        ite++;
      }
    }
    int mi = min(m[1].lower_bound(R)->first, m[2].lower_bound(R)->first);
    for (ite = m[0].begin(); ite != m[0].end(); ite++) {
      mi = min(mi, (ite->first) + (m[1].lower_bound(R - (ite->second))->first));
    }
    cout << mi << endl;
    FOR(i, 2) m[i].clear();
  }
}

int main() {
  solve();
  return 0;
}