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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

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

const int dx[] = {-1, 0, 1}, dy[] = {0, 1, 0};

unsigned char G[10][10][10001]; // y, x, cost = life

struct P {
  int cost, life, x, y;
  uint16_t visited;

  bool operator>(const P &p) const { return cost > p.cost; }
};

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    int money, max_life, life;
    cin >> money >> max_life >> life;

    vvi field(h, vi(w, 0));
    REP(y, h) REP(x, w) { cin >> field[y][x]; }

    fill_n((unsigned char *)G, 10 * 10 * 10001, 0);
    priority_queue<P, vector<P>, greater<P>> que;

    REP(x, w) {
      int l = life - 1;
      if (l <= 0)
        continue;
      int cost = 0;
      int c = field[0][x];
      if (c > 0) {
        l = min(l + c, max_life);
      } else {
        cost += -c;
      }
      if (cost <= money && l >= 1) {
        G[0][x][cost] = l;
        que.push({cost, l, x, 0, (uint16_t)(1 << x)});
      }
    }

    while (!que.empty()) {
      P p = que.top();
      que.pop();

      REP(i, 3) {
        int x = p.x + dx[i], y = p.y + dy[i];
        if (y == h) {
          cout << p.cost << endl;
          goto END;
        }
        if (0 <= x && x < w && 0 <= y && y < h) {
          int life = p.life - 1;
          if (life <= 0)
            continue;
          int cost = p.cost;
          if (dy[i] == 0 && p.visited & (1 << x)) {
          } else {
            int c = field[y][x];
            if (c > 0) {
              life = min(life + c, max_life);
            } else {
              cost += -c;
            }
          }
          if (cost <= money && G[y][x][cost] <= life) {
            G[y][x][cost] = life;
            que.push({cost, life, x, y,
                      (uint16_t)(dy[i] == 1 ? 1 << x : p.visited | (1 << x))});
          }
        }
      }
    }
    cout << "NA" << endl;
  END : {}
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

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

const int dx[] = {-1, 0, 1}, dy[] = {0, 1, 0};

bool G[10][10][10001]; // y, x, cost

struct P {
  int cost, life, x, y;
  uint16_t visited;

  bool operator>(const P &p) const { return cost > p.cost; }
};

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    int money, max_life, life;
    cin >> money >> max_life >> life;

    vvi field(h, vi(w, 0));
    REP(y, h) REP(x, w) { cin >> field[y][x]; }

    fill_n((int *)G, 10 * 10 * 10001, false);
    priority_queue<P, vector<P>, greater<P>> que;

    REP(x, w) {
      int l = life - 1;
      int cost = 0;
      int c = field[0][x];
      if (c > 0) {
        l = min(l + c, max_life);
      } else {
        cost += -c;
      }
      if (cost <= money && l >= 1) {
        G[0][x][cost] = true;
        que.push({cost, l, x, 0, (uint16_t)(1 << x)});
      }
    }

    while (!que.empty()) {
      P p = que.top();
      que.pop();

      REP(i, 3) {
        int x = p.x + dx[i], y = p.y + dy[i];
        if (y == h) {
          cout << p.cost << endl;
          goto END;
        }
        if (0 <= x && x < w && 0 <= y && y < h) {
          int life = p.life - 1;
          int cost = p.cost;
          if (dy[i] == 0 && p.visited & (1 << x)) {
          } else {
            int c = field[y][x];
            if (c > 0) {
              life = min(life + c, max_life);
            } else {
              cost += -c;
            }
          }
          if (cost <= money && life >= 1 && !G[y][x][cost]) {
            G[y][x][cost] = true;
            que.push({cost, life, x, y,
                      (uint16_t)(dy[i] == 1 ? 1 << x : p.visited | (1 << x))});
          }
        }
      }
    }
    cout << "NA" << endl;
  END : {}
  }
}