Problem p01361 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = (a); i < (b); ++i)

int HPinit, HPmax, R, C, T, S, P, p[22];
string grid[111];
unordered_map<char, int> trap;

string dirs = "LURD";
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

typedef pair<int, int> pii;
vector<pii> step;

int max_hp[1L << 13][1010];

void maxUpdate(int &a, int b) { a = max(a, b); }

bool solve() {
  int SS = step.size();

  vector<int> damage(SS);

  for_(i, 0, SS) {
    pii pos = step[i];
    int x = pos.first, y = pos.second;
    damage[i] = trap[grid[y][x]];
  }

  memset(max_hp, -1, sizeof(max_hp));

  max_hp[0][0] = HPinit;

  for_(U, 0, 1 << P) for_(i, 0, SS) {
    if (max_hp[U][i] <= 0)
      continue;

    maxUpdate(max_hp[U][i + 1], max_hp[U][i] - damage[i]);

    for_(j, 0, P) {
      if (U >> j & 1)
        continue;
      maxUpdate(max_hp[U | 1 << j][i], min(HPmax, max_hp[U][i] + p[j]));
    }
  }

  bool res = false;
  for_(U, 0, 1 << P) res |= (max_hp[U][SS] > 0);

  return res;
}

int main() {
  while (cin >> HPinit >> HPmax, HPinit) {
    cin >> R >> C;
    for_(r, 0, R) cin >> grid[r];

    trap.clear();
    cin >> T;

    for_(i, 0, T) {
      char alph;
      int d;
      cin >> alph >> d;
      trap[alph] = d;
    }

    step.clear();
    cin >> S;

    pii pos(0, 0);

    for_(i, 0, S) {
      char d;
      int n;
      cin >> d >> n;

      int dr = dirs.find(d);

      for_(j, 0, n) {
        pos = pii(pos.first + dx[dr], pos.second + dy[dr]);
        if (pos.first < 0)
          pos.first = 0;
        if (pos.second < 0)
          pos.second = 0;
        if (pos.first >= C)
          pos.first = C - 1;
        if (pos.second >= R)
          pos.second = R - 1;
        step.push_back(pos);
      }
    }

    cin >> P;
    for_(i, 0, P) cin >> p[i];

    cout << (solve() ? "YES" : "NO") << endl;
    ;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = (a); i < (b); ++i)

int HPinit, HPmax, R, C, T, S, P, p[22];
string grid[111];
unordered_map<char, int> trap;

string dirs = "LURD";
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

typedef pair<int, int> pii;
vector<pii> step;

int max_hp[1L << 15][1010];

void maxUpdate(int &a, int b) { a = max(a, b); }

bool solve() {
  int SS = step.size();

  vector<int> damage(SS);

  for_(i, 0, SS) {
    pii pos = step[i];
    int x = pos.first, y = pos.second;
    damage[i] = trap[grid[y][x]];
  }

  memset(max_hp, -1, sizeof(max_hp));

  max_hp[0][0] = HPinit;

  for_(U, 0, 1 << P) for_(i, 0, SS) {
    if (max_hp[U][i] == -1)
      continue;

    maxUpdate(max_hp[U][i + 1], max_hp[U][i] - damage[i]);

    for_(j, 0, P) {
      if (U >> j & 1)
        continue;
      maxUpdate(max_hp[U | 1 << j][i], max_hp[U][i] + p[j]);
    }
  }

  bool res = false;
  for_(U, 0, 1 << P) res |= (max_hp[U][SS] > 0);

  return res;
}

int main() {
  while (cin >> HPinit >> HPmax, HPinit) {
    cin >> R >> C;
    for_(r, 0, R) cin >> grid[r];

    trap.clear();
    cin >> T;

    for_(i, 0, T) {
      char alph;
      int d;
      cin >> alph >> d;
      trap[alph] = d;
    }

    step.clear();
    cin >> S;

    pii pos(0, 0);

    for_(i, 0, S) {
      char d;
      int n;
      cin >> d >> n;

      int dr = dirs.find(d);

      for_(j, 0, n) {
        pos = pii(pos.first + dx[dr], pos.second + dy[dr]);
        if (pos.first < 0)
          pos.first = 0;
        if (pos.second < 0)
          pos.second = 0;
        if (pos.first >= C)
          pos.first = C - 1;
        if (pos.second >= R)
          pos.second = R - 1;
        step.push_back(pos);
      }
    }

    cin >> P;
    for_(i, 0, P) cin >> p[i];

    cout << (solve() ? "YES" : "NO") << endl;
    ;
  }
}