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

#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

#define inf (1 << 27)

struct State {
  int x, y, l, r, o, c;
  State(int xx, int yy, int ll, int rr, int oo, int cc) {
    x = xx, y = yy, l = ll, r = rr, o = oo, c = cc;
  }
};

bool operator>(const State &left, const State &right) {
  return left.c > right.c;
}

int map[10][10];
int cost[10][10][10][10][51];

int main() {
  int W, H, f, m, o;
  while (scanf("%d %d", &W, &H), W) {
    memset(cost, 0x10, sizeof(cost));
    scanf("%d %d %d", &f, &m, &o);
    for (int j = 0; j < H; j++)
      for (int i = 0; i < W; i++) {
        scanf("%d", &map[i][j]);
      }
    if (o <= 1) {
      printf("NA\\n");
      continue;
    }
    priority_queue<State, vector<State>, greater<State>> que;
    for (int i = 0; i < W; i++) {
      int oo = o - 1, cc = 0;
      if (map[i][0] < 0) {
        cc -= map[i][0];
      } else {
        oo += map[i][0];
        if (oo > m)
          oo = m;
      }
      que.push(State(i, 0, i, i, oo, cc));
    }
    int res = inf;
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      s.o--;
      if (s.y == (H - 1)) {
        res = s.c;
        break;
      }
      if (s.o == 0) {
        continue;
      }
      if (cost[s.x][s.y][s.l][s.r][s.o + 1] < s.c) {
        continue;
      }
      State ns = s;
      ns.y++;
      ns.l = ns.r = ns.x;
      if (ns.y < H) {
        if (map[ns.x][ns.y] > 0) {
          ns.o += map[ns.x][ns.y];
          if (ns.o > m)
            ns.o = m;
        } else {
          ns.c -= map[ns.x][ns.y];
        }
        if (cost[ns.x][ns.y][ns.l][ns.r][ns.o] > ns.c) {
          cost[ns.x][ns.y][ns.l][ns.r][ns.o] = ns.c;
          que.push(ns);
        }
      }
      ns = s;
      ns.x++;
      if (ns.x < W) {
        if (ns.x > ns.r) {
          ns.r = ns.x;
          if (map[ns.x][ns.y] > 0) {
            ns.o += map[ns.x][ns.y];
            if (ns.o > m)
              ns.o = m;
          } else {
            ns.c -= map[ns.x][ns.y];
          }
        }
        if (cost[ns.x][ns.y][ns.l][ns.r][ns.o] > ns.c) {
          cost[ns.x][ns.y][ns.l][ns.r][ns.o] = ns.c;
          que.push(ns);
        }
      }
      ns = s;
      ns.x--;
      if (ns.x >= 0) {
        if (ns.l > ns.x) {
          ns.l = ns.x;
          if (map[ns.x][ns.y] > 0) {
            ns.o += map[ns.x][ns.y];
            if (ns.o > m)
              ns.o = m;
          } else {
            ns.c -= map[ns.x][ns.y];
          }
        }
        if (cost[ns.x][ns.y][ns.l][ns.r][ns.o] > ns.c) {
          cost[ns.x][ns.y][ns.l][ns.r][ns.o] = ns.c;
          que.push(ns);
        }
      }
    }
    if (res <= f)
      printf("%d\\n", res);
    else
      printf("NA\\n");
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

#define INF (1 << 27)
#define rep(a, b) for (int a = 0; a < b; a++)

struct state {
  int x, y, l, r, o, c;
  state(int xx, int yy, int ll, int rr, int oo, int cc) {
    x = xx, y = yy, l = ll, r = rr, o = oo, c = cc;
  }
};

bool operator>(const state &a, const state &b) { return a.c > b.c; }

int map[10][10], cost[10][10][10][10][51];
int dx[] = {0, 1, -1}, dy[] = {1, 0, 0};
int W, H, f, m, o;

int main() {
  while (scanf("%d %d", &W, &H), W) {
    scanf("%d %d %d", &f, &m, &o);
    rep(j, H) rep(i, W) scanf("%d", &map[i][j]);
    if (o == 1) {
      printf("NA\\n");
      continue;
    }
    fill(cost[0][0][0][0], cost[0][0][0][0] + 10 * 10 * 10 * 10 * 51, INF);
    priority_queue<state, vector<state>, greater<state>> que;
    rep(i, W) {
      if (map[i][0] > 0) {
        int oo = (o - 1) + map[i][0];
        oo = oo >= m ? m : oo;
        cost[i][0][i][i][oo] = 0;
        que.push(state(i, 0, i, i, oo, 0));
      } else {
        cost[i][0][i][i][o - 1] = -map[i][0];
        que.push(state(i, 0, i, i, o - 1, -map[i][0]));
      }
    }
    while (!que.empty()) {
      state st(que.top());
      que.pop();
      if (cost[st.x][st.y][st.l][st.r][st.o] < st.c)
        continue;
      if (st.o <= 1)
        continue;
      rep(i, 3) {
        int nx = st.x + dx[i], ny = st.y + dy[i];
        if (0 > nx || nx >= W || 0 > ny || ny >= H)
          continue;
        if (i > 0) {
          if (st.l <= nx && nx <= st.r) {
            if (cost[nx][ny][st.l][st.r][st.o - 1] > st.c) {
              cost[nx][ny][st.l][st.r][st.o - 1] = st.c;
              que.push(state(nx, ny, st.l, st.r, st.o - 1, st.c));
            }
          }
          int ll = st.l, rr = st.r;
          if (dx[i] == 1)
            rr++;
          else
            ll--;
          if (map[nx][ny] > 0) {
            int oo = (st.o - 1) + map[nx][ny];
            oo = (oo >= m) ? m : oo;
            if (cost[nx][ny][ll][rr][oo] > st.c) {
              cost[nx][ny][ll][rr][oo] = st.c;
              que.push(state(nx, ny, ll, rr, oo, st.c));
            }
          } else {
            if (cost[nx][ny][ll][rr][st.o - 1] > (st.c - map[nx][ny])) {
              cost[nx][ny][ll][rr][st.o - 1] = (st.c - map[nx][ny]);
              que.push(state(nx, ny, ll, rr, st.o - 1, st.c - map[nx][ny]));
            }
          }
        } else {
          if (map[nx][ny] > 0) {
            int oo = (st.o - 1) + map[nx][ny];
            oo = (oo >= m) ? m : oo;
            if (cost[nx][ny][nx][nx][oo] > st.c) {
              cost[nx][ny][nx][nx][oo] = st.c;
              que.push(state(nx, ny, nx, nx, oo, st.c));
            }
          } else {
            if (cost[nx][ny][nx][nx][o - 1] > (st.c - map[nx][ny])) {
              cost[nx][ny][nx][nx][o - 1] = st.c - map[nx][ny];
              que.push(state(nx, ny, nx, nx, st.o - 1, st.c - map[nx][ny]));
            }
          }
        }
      }
    }
    int res = INF;
    rep(i, W) rep(j, W) rep(k, W) rep(l, o) if (res > cost[i][H - 1][j][k][l])
        res = cost[i][H - 1][j][k][l];
    if (res > f)
      printf("NA\\n");
    else
      printf("%d\\n", res);
  }
}