Problem p01361 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int potion[13];
int dmg[256];
int dp[1010][1 << 12];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int dx[256] = {};
  int dy[256] = {};

  rep(i, 4) {
    dx["UDRL"[i]] = vector<int>({-1, 1, 0, 0})[i];
    dy["UDRL"[i]] = vector<int>({0, 0, 1, -1})[i];
  }

  int mhp, hp;
  while (cin >> hp >> mhp, hp) {
    int r, c;
    cin >> r >> c;
    vector<string> dangeon(r);
    rep(i, r) cin >> dangeon[i];

    int t;
    cin >> t;
    rep(i, t) {
      char c;
      int d;
      cin >> c >> d;
      dmg[c] = d;
    }

    int x, y;
    x = y = 0;
    string mv;
    mv += dangeon[y][x];

    int s;
    cin >> s;
    while (s--) {
      char c;
      int k;
      cin >> c >> k;
      while (k--) {
        x += dx[c];
        y += dy[c];
        mv += dangeon[y][x];
      }
    }

    int p;
    cin >> p;
    rep(i, p) {
      int x;
      cin >> x;
      potion[i] = x;
    }

    MEMSET(dp, 0);
    dp[0][0] = hp;

    int sz = mv.size();
    rep(i, sz) {
      if (i)
        rep(j, 1 << p) { dp[i][j] = dp[i - 1][j] - dmg[mv[i]]; }
      rep(j, 1 << p) {
        rep(k, p) {
          int x = 1 << k;
          if (~j & x)
            continue;
          if (dp[i][j & ~x] <= 0)
            continue;
          dp[i][j] = max(dp[i][j], min(mhp, dp[i][j & ~x] + potion[k]));
        }
      }

      // rep(j, 1 << p){
      //	bitset<2> b(j);
      //	cout << b.to_string() << ' ' << dp[i][j] << endl;
      // }
    }
    bool b = *max_element(dp[sz - 1], dp[sz - 1] + (1 << p)) >= 1;
    cout << (b ? "YES" : "NO") << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int potion[13];
int dmg[256];
int dp[1010][1 << 12];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int dx[256] = {};
  int dy[256] = {};

  rep(i, 4) {
    dx["UDRL"[i]] = vector<int>({0, 0, 1, -1})[i];
    dy["UDRL"[i]] = vector<int>({-1, 1, 0, 0})[i];
  }

  int mhp, hp;
  while (cin >> hp >> mhp, hp) {
    int r, c;
    cin >> r >> c;
    vector<string> dangeon(r);
    rep(i, r) cin >> dangeon[i];

    int t;
    cin >> t;
    rep(i, t) {
      char c;
      int d;
      cin >> c >> d;
      dmg[c] = d;
    }

    int x, y;
    x = y = 0;
    string mv;
    mv += dangeon[y][x];

    int s;
    cin >> s;
    while (s--) {
      char c;
      int k;
      cin >> c >> k;
      while (k--) {
        x += dx[c];
        y += dy[c];
        mv += dangeon[y][x];
      }
    }

    int p;
    cin >> p;
    rep(i, p) {
      int x;
      cin >> x;
      potion[i] = x;
    }

    MEMSET(dp, 0);
    dp[0][0] = hp;

    int sz = mv.size();
    rep(i, sz) {
      if (i)
        rep(j, 1 << p) { dp[i][j] = dp[i - 1][j] - dmg[mv[i]]; }
      rep(j, 1 << p) {
        rep(k, p) {
          int x = 1 << k;
          if (~j & x)
            continue;
          if (dp[i][j & ~x] <= 0)
            continue;
          dp[i][j] = max(dp[i][j], min(mhp, dp[i][j & ~x] + potion[k]));
        }
      }

      // rep(j, 1 << p){
      //	bitset<2> b(j);
      //	cout << b.to_string() << ' ' << dp[i][j] << endl;
      // }
    }
    bool b = *max_element(dp[sz - 1], dp[sz - 1] + (1 << p)) >= 1;
    cout << (b ? "YES" : "NO") << endl;
  }

  return 0;
}