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

#include <iostream>
#include <vector>

using namespace std;

const int infty = 1 << 28;
static int dp[1001][1 << 12];

int main() {
  while (true) {

    int initHP;
    int maxHP;
    cin >> initHP >> maxHP;
    if (initHP == 0 && maxHP == 0)
      break;

    int R, C;
    cin >> R >> C;
    char M[R][C];
    int damage[256];
    for (int i = 0; i < R; ++i) {
      for (int j = 0; j < C; ++j) {
        cin >> M[i][j];
      }
    }

    int T;
    cin >> T;
    for (int i = 0; i < T; ++i) {
      char c;
      int dam;
      cin >> c >> dam;
      damage[(int)c] = dam;
    }

    int S;
    cin >> S;
    string inst;
    for (int i = 0; i < S; ++i) {
      char c;
      int step;
      cin >> c >> step;
      for (int j = 0; j < step; ++j) {
        inst += c;
      }
    }

    int P;
    vector<int> potion;
    cin >> P;
    for (int i = 0; i < P; ++i) {
      int p;
      cin >> p;
      potion.push_back(p);
    }

    S = inst.length();

    int Pmax = 1 << potion.size();
    for (int i = 0; i < 1 + S; ++i) {
      for (int j = 0; j < Pmax; ++j) {
        dp[i][j] = -infty;
      }
    }
    dp[0][0] = initHP;

    int ni = 0;
    int nj = 0;
    for (int i = 0; i < S; ++i) {
      switch (inst[i]) {
      case 'D':
        ++ni;
        break;
      case 'U':
        --ni;
        break;
      case 'R':
        ++nj;
        break;
      case 'L':
        --nj;
        break;
      }
      for (int j = 0; j < Pmax; ++j) {
        if (dp[i][j] <= 0)
          continue;
        for (int k = 0; k < (int)potion.size(); ++k) {
          if ((j & (1 << k)) == 0) {
            dp[i][j | (1 << k)] =
                max(dp[i][j | (1 << k)], min(maxHP, dp[i][j] + potion[k]));
          }
        }
      }
      for (int j = 0; j < Pmax; ++j) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - damage[(int)M[ni][nj]]);
      }
    }

    int res = -infty;
    for (int i = 0; i < Pmax; ++i) {
      res = max(res, dp[S][i]);
    }

    if (res > 0)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

const int infty = 1 << 28;

int main() {
  while (true) {

    int initHP;
    int maxHP;
    cin >> initHP >> maxHP;
    if (initHP == 0 && maxHP == 0)
      break;

    int R, C;
    cin >> R >> C;
    char M[R][C];
    int damage[256];
    for (int i = 0; i < R; ++i) {
      for (int j = 0; j < C; ++j) {
        cin >> M[i][j];
      }
    }

    int T;
    cin >> T;
    for (int i = 0; i < T; ++i) {
      char c;
      int dam;
      cin >> c >> dam;
      damage[(int)c] = dam;
    }

    int S;
    cin >> S;
    string inst;
    for (int i = 0; i < S; ++i) {
      char c;
      int step;
      cin >> c >> step;
      for (int j = 0; j < step; ++j) {
        inst += c;
      }
    }

    int P;
    vector<int> potion;
    cin >> P;
    for (int i = 0; i < P; ++i) {
      int p;
      cin >> p;
      potion.push_back(p);
    }

    S = inst.length();

    int Pmax = 1 << potion.size();
    int dp[1 + S][Pmax];
    for (int i = 0; i < 1 + S; ++i) {
      for (int j = 0; j < Pmax; ++j) {
        dp[i][j] = -infty;
      }
    }
    dp[0][0] = initHP;

    int ni = 0;
    int nj = 0;
    for (int i = 0; i < S; ++i) {
      switch (inst[i]) {
      case 'D':
        ++ni;
        break;
      case 'U':
        --ni;
        break;
      case 'R':
        ++nj;
        break;
      case 'L':
        --nj;
        break;
      }
      for (int j = 0; j < Pmax; ++j) {
        for (int k = 0; k < (int)potion.size(); ++k) {
          if ((j & (1 << k)) == 0) {
            dp[i][j | (1 << k)] =
                max(dp[i][j | (1 << k)], min(maxHP, dp[i][j] + potion[k]));
          }
        }
      }
      for (int j = 0; j < Pmax; ++j) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] - damage[(int)M[ni][nj]]);
      }
    }

    int res = -infty;
    for (int i = 0; i < Pmax; ++i) {
      res = max(res, dp[S][i]);
    }
    if (res > 0)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}