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

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int v[500 * 500 + 500] = {};
int v2[500 * 500 + 500] = {};
int a[505][505];
int b[505][505];
bool used[505][505] = {};
int main() {
  while (1) {
    int r;
    int w1, h1, x1, y1;
    scanf("%d", &r);
    if (!r)
      return 0;
    scanf("%d %d %d %d", &w1, &h1, &x1, &y1);
    for (int i = 1; i <= h1; i++) {
      for (int j = 1; j <= w1; j++) {
        scanf("%d", &a[j][i]);
      }
    }
    int w2, h2, x2, y2;
    scanf("%d %d %d %d", &w2, &h2, &x2, &y2);
    for (int i = 1; i <= h2; i++) {
      for (int j = 1; j <= w2; j++) {
        scanf("%d", &b[j][i]);
      }
    }
    memset(used, 0, sizeof(used));
    priority_queue<P1, vector<P1>, greater<P1>> que;
    que.push(mp(1, mp(x1, y1)));
    int cur = 0;
    int now = 0;
    while (!que.empty()) {
      P1 p = que.top();
      que.pop();
      int x = p.second.first;
      int y = p.second.second;
      if (used[x][y])
        continue;
      used[x][y] = true;
      now = max(now, p.first);
      v[++cur] = now;
      int dx[4] = {0, 1, 0, -1};
      int dy[4] = {1, 0, -1, 0};
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (!(1 <= nx && nx <= w1))
          continue;
        if (!(1 <= ny && ny <= h1))
          continue;
        if (used[nx][ny])
          continue;
        que.push(mp(a[nx][ny], mp(nx, ny)));
      }
    }
    memset(used, 0, sizeof(used));
    que.push(mp(1, mp(x2, y2)));
    cur = now = 0;
    while (!que.empty()) {
      P1 p = que.top();
      que.pop();
      int x = p.second.first;
      int y = p.second.second;
      if (used[x][y])
        continue;
      used[x][y] = true;
      now = max(now, p.first);
      v2[++cur] = now;
      int dx[4] = {0, 1, 0, -1};
      int dy[4] = {1, 0, -1, 0};
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (!(1 <= nx && nx <= w2))
          continue;
        if (!(1 <= ny && ny <= h2))
          continue;
        if (used[nx][ny])
          continue;
        que.push(mp(b[nx][ny], mp(nx, ny)));
      }
    }
    // for(int i=0;i<=w1*h1;i++) printf("%ds ",v[i]);

    // for(int i=0;i<=w2*h2;i++) printf("%ds ",v2[i]);
    int ret = INF;
    for (int i = max(0, r - w2 * h2); i <= min(r, w1 * h1); i++) {
      ret = min(ret, v[i] + v2[r - i]);
    }
    printf("%d\\n", ret);
  }
}

Error: 0

Fixed submission:

// Bokan ga bokka--nn!!
// Daily Lunch Special Tanoshii !!
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d", &x)
#define rep(i, x) for (int i = 0; i < x; i++)
int v[500 * 500 + 500] = {};
int v2[500 * 500 + 500] = {};
int a[505][505];
int b[505][505];
bool used[505][505] = {};
int main() {
  while (1) {
    int r;
    int w1, h1, x1, y1;
    scanf("%d", &r);
    if (!r)
      return 0;
    scanf("%d %d %d %d", &w1, &h1, &x1, &y1);
    for (int i = 1; i <= h1; i++) {
      for (int j = 1; j <= w1; j++) {
        scanf("%d", &a[j][i]);
      }
    }
    int w2, h2, x2, y2;
    scanf("%d %d %d %d", &w2, &h2, &x2, &y2);
    for (int i = 1; i <= h2; i++) {
      for (int j = 1; j <= w2; j++) {
        scanf("%d", &b[j][i]);
      }
    }
    memset(used, 0, sizeof(used));
    priority_queue<P1, vector<P1>, greater<P1>> que;
    que.push(mp(1, mp(x1, y1)));
    int cur = 0;
    int now = 0;
    while (!que.empty()) {
      P1 p = que.top();
      que.pop();
      int x = p.second.first;
      int y = p.second.second;
      used[x][y] = true;
      now = max(now, p.first);
      v[++cur] = now;
      int dx[4] = {0, 1, 0, -1};
      int dy[4] = {1, 0, -1, 0};
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (!(1 <= nx && nx <= w1))
          continue;
        if (!(1 <= ny && ny <= h1))
          continue;
        if (used[nx][ny])
          continue;
        que.push(mp(a[nx][ny], mp(nx, ny)));
      }
    }
    memset(used, 0, sizeof(used));
    que.push(mp(1, mp(x2, y2)));
    cur = now = 0;
    while (!que.empty()) {
      P1 p = que.top();
      que.pop();
      int x = p.second.first;
      int y = p.second.second;
      used[x][y] = true;
      now = max(now, p.first);
      v2[++cur] = now;
      int dx[4] = {0, 1, 0, -1};
      int dy[4] = {1, 0, -1, 0};
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (!(1 <= nx && nx <= w2))
          continue;
        if (!(1 <= ny && ny <= h2))
          continue;
        if (used[nx][ny])
          continue;
        que.push(mp(b[nx][ny], mp(nx, ny)));
      }
    }
    // for(int i=0;i<=w1*h1;i++) printf("%ds ",v[i]);

    // for(int i=0;i<=w2*h2;i++) printf("%ds ",v2[i]);
    int ret = INF;
    for (int i = max(0, r - w2 * h2); i <= min(r, w1 * h1); i++) {
      ret = min(ret, v[i] + v2[r - i]);
    }
    printf("%d\\n", ret);
  }
}