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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int l[2][500][500];
bool c[2][500][500];
int memo[2][250001];

struct level {
  int l;
  int x;
  int y;
  level(int a, int b, int c) {
    l = a;
    x = b;
    y = c;
  }
};

bool operator<(const level &a, const level &b) { return a.l > b.l; }

int main() {
  int r;
  while (r = getInt()) {
    int w[2], h[2], x[2], y[2];
    REP(k, 2) {
      w[k] = getInt();
      h[k] = getInt();
      x[k] = getInt() - 1;
      y[k] = getInt() - 1;
      REP(i, h[k]) REP(j, w[k]) {
        l[k][i][j] = getInt();
        c[k][i][j] = false;
      }

      priority_queue<level> pq;
      pq.push(level(1, x[k], y[k]));
      c[k][y[k]][x[k]] = true;

      int cnt = 0;
      int now = 1;

      memo[k][cnt++] = 0;
      while (!pq.empty()) {
        level lv = pq.top();
        int ll = lv.l;
        int xx = lv.x;
        int yy = lv.y;
        pq.pop();
        // printf("%d %d %d\\n",ll,xx,yy);

        memo[k][cnt++] = (now = max(ll, now));

        REP(i, 4) {
          int xxx = xx + _dx[i];
          int yyy = yy + _dy[i];

          if (ISIN(xxx, yyy, w[k], h[k]) && !c[k][yyy][xxx]) {
            c[k][yyy][xxx] = true;
            pq.push(level(l[k][yyy][xxx], xxx, yyy));
          }
        }
      }

      // REP(i, w[k]*h[k]+1) printf("%d ",memo[k][i]); puts("");
    }

    int end = min(r, w[0] * h[0]) + 1;
    int start = max(0, r - w[1] * h[1]);
    int ans = INT_MAX;
    rep(i, start, end) ans = min(ans, memo[0][i] + memo[1][r - i]);
    print(ans);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int l[2][500][500];
bool c[2][500][500];
int memo[2][100001];

struct level {
  int l;
  int x;
  int y;
  level(int a, int b, int c) {
    l = a;
    x = b;
    y = c;
  }
};

bool operator<(const level &a, const level &b) { return a.l > b.l; }

int main() {
  int r;
  while (r = getInt()) {
    int w[2], h[2], x[2], y[2];
    REP(k, 2) {
      w[k] = getInt();
      h[k] = getInt();
      x[k] = getInt() - 1;
      y[k] = getInt() - 1;
      REP(i, h[k]) REP(j, w[k]) {
        l[k][i][j] = getInt();
        c[k][i][j] = false;
      }

      priority_queue<level> pq;
      pq.push(level(1, x[k], y[k]));
      c[k][y[k]][x[k]] = true;

      int cnt = 0;
      int now = 1;

      memo[k][cnt++] = 0;
      while (!pq.empty()) {
        level lv = pq.top();
        int ll = lv.l;
        int xx = lv.x;
        int yy = lv.y;
        pq.pop();
        // printf("%d %d %d\\n",ll,xx,yy);

        memo[k][cnt++] = (now = max(ll, now));
        if (cnt == w[k] * h[k] + 1)
          break;

        REP(i, 4) {
          int xxx = xx + _dx[i];
          int yyy = yy + _dy[i];

          if (ISIN(xxx, yyy, w[k], h[k]) && !c[k][yyy][xxx]) {
            c[k][yyy][xxx] = true;
            pq.push(level(l[k][yyy][xxx], xxx, yyy));
          }
        }
      }

      // REP(i, w[k]*h[k]+1) printf("%d ",memo[k][i]); puts("");
    }

    int end = min(r, w[0] * h[0]) + 1;
    int start = max(0, r - w[1] * h[1]);
    int ans = INT_MAX;
    rep(i, start, end) ans = min(ans, memo[0][i] + memo[1][r - i]);
    print(ans);
  }

  return 0;
}