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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[1111][1 << 12];
signed main() {
  int hi, hm;
  while (cin >> hi >> hm, hi || hm) {
    int r, c;
    cin >> r >> c;
    string a[r];
    for (int i = 0; i < r; i++)
      cin >> a[i];
    int t;
    cin >> t;
    char b[t];
    int d[t];
    for (int i = 0; i < t; i++)
      cin >> b[i] >> d[i];
    int s;
    cin >> s;
    char e[s];
    int n[s];
    for (int i = 0; i < s; i++)
      cin >> e[i] >> n[i];
    int q;
    cin >> q;
    int p[q];
    for (int i = 0; i < q; i++)
      cin >> p[i];
    // puts("OK");
    map<char, int> dm;
    for (int i = 0; i < t; i++)
      dm[b[i]] = d[i];
    string udlr = "UDLR";
    map<char, int> rd;
    for (int i = 0; i < (int)udlr.size(); i++)
      rd[udlr[i]] = i;
    memset(dp, 0, sizeof(dp));
    dp[0][0] = hi;
    int z = 0;
    int ax[] = {0, 0, -1, 1};
    int ay[] = {-1, 1, 0, 0};
    int px = 0, py = 0;
    for (int x = 0; x < s; x++) {
      for (int y = 0; y < n[x]; y++) {
        int k = rd[e[x]];
        px += ax[k];
        py += ay[k];
        for (int i = 0; i < (1 << q); i++) {
          if (!dp[z][i])
            continue;
          for (int l = 0; l < q; l++) {
            if ((i >> l) & 1)
              continue;
            int j = i + (1 << l);
            int tmp = min(hm, dp[z][i] + p[l]);
            dp[z][j] = max(dp[z][j], tmp);
            tmp -= dm[a[py][px]];
            dp[z + 1][j] = max(dp[z + 1][j], tmp);
          }
          dp[z + 1][i] = max(dp[z + 1][i], dp[z][i] - dm[a[py][px]]);
        }
        z++;
      }
    }
    bool f = 0;
    for (int i = 0; i < (1 << q); i++)
      f |= dp[z][i];
    cout << (f ? "YES" : "NO") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int dp[1111][1 << 12];
signed main() {
  int hi, hm;
  while (cin >> hi >> hm, hi || hm) {
    int r, c;
    cin >> r >> c;
    string a[r];
    for (int i = 0; i < r; i++)
      cin >> a[i];
    int t;
    cin >> t;
    char b[t];
    int d[t];
    for (int i = 0; i < t; i++)
      cin >> b[i] >> d[i];
    int s;
    cin >> s;
    char e[s];
    int n[s];
    for (int i = 0; i < s; i++)
      cin >> e[i] >> n[i];
    int q;
    cin >> q;
    int p[q];
    for (int i = 0; i < q; i++)
      cin >> p[i];
    // puts("OK");
    map<char, int> dm;
    for (int i = 0; i < t; i++)
      dm[b[i]] = d[i];
    string udlr = "UDLR";
    map<char, int> rd;
    for (int i = 0; i < (int)udlr.size(); i++)
      rd[udlr[i]] = i;
    memset(dp, 0, sizeof(dp));
    dp[0][0] = hi;
    int z = 0;
    int ax[] = {0, 0, -1, 1};
    int ay[] = {-1, 1, 0, 0};
    int px = 0, py = 0;
    for (int x = 0; x < s; x++) {
      for (int y = 0; y < n[x]; y++) {
        int k = rd[e[x]];
        px += ax[k];
        py += ay[k];
        // cout<<px<<" "<<py<<endl;
        for (int i = 0; i < (1 << q); i++) {
          if (!dp[z][i])
            continue;
          for (int l = 0; l < q; l++) {
            if ((i >> l) & 1)
              continue;
            int j = i + (1 << l);
            int tmp = min(hm, dp[z][i] + p[i]);
            dp[z][j] = max(dp[z][j], tmp);
            tmp -= dm[a[py][px]];
            if (tmp <= 0)
              continue;
            dp[z + 1][j] = max(dp[z + 1][j], tmp);
          }
        }
        z++;
      }
    }
    bool f = 0;
    for (int i = 0; i < (1 << q); i++)
      f |= dp[z][i];
    cout << (f ? "YES" : "NO") << endl;
  }
  return 0;
}