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

#include <bits/stdc++.h>
using namespace std;
int hp, HP;
int h, w;
string mp[101];
int dmg[1 << 8];
string seq;
int P, p[12];

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

void Go(int &x, int &y, char ch) {
  if (ch == 'U')
    y--;
  if (ch == 'D')
    y++;
  if (ch == 'L')
    x--;
  if (ch == 'R')
    x++;
}

int dp[1010][1 << 12];
void update(int idx, int bit, int hp, int dmg, int i) {
  Max(dp[i][bit], hp - dmg);
  if (idx == P || hp - dmg > 0)
    return;
  update(idx + 1, bit, hp, dmg, i);
  if (bit >> idx & 1)
    return;
  update(idx + 1, bit | (1 << idx), min(HP, hp + p[idx]), dmg, i);
}

bool DP() {
  int x = 0, y = 0, size = seq.size();
  memset(dp, 0, sizeof(dp));
  dp[0][0] = hp;
  for (int i = 0; i < size; i++) {
    Go(x, y, seq[i]);
    for (int bit = 0; bit < (1 << P); bit++)
      if (dp[i][bit])
        update(0, bit, dp[i][bit], dmg[mp[y][x]], i + 1);
  }

  for (int i = 0; i < (1 << P); i++)
    if (dp[size][i])
      return 1;
  return 0;
}

int main() {
  int cnt = 0;
  while (1) {
    cnt++;
    cin >> hp >> HP;
    if (!hp && !HP)
      return 0;
    cin >> h >> w;
    for (int i = 0; i < h; i++)
      cin >> mp[i];

    int Alpha;
    cin >> Alpha;
    for (int i = 0, cost; i < Alpha; i++) {
      char ch;
      cin >> ch >> cost;
      dmg[ch] = cost;
    }

    int s;
    cin >> s;
    seq.clear();
    for (int i = 0, num; i < s; i++) {
      char ch;
      cin >> ch >> num;
      seq += string(num, ch);
    }

    cin >> P;
    for (int i = 0; i < P; i++)
      cin >> p[i];
    cout << (DP() ? "YES" : "NO") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int hp, HP;
int h, w;
string mp[101];
int dmg[1 << 8];
string seq;
int P, p[12];

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

void Go(int &x, int &y, char ch) {
  if (ch == 'U')
    y--;
  if (ch == 'D')
    y++;
  if (ch == 'L')
    x--;
  if (ch == 'R')
    x++;
}

int dp[1010][1 << 12];
void update(int idx, int bit, int hp, int i) {
  Max(dp[i + 1][bit], hp);
  if (idx == P)
    return;
  update(idx + 1, bit, hp, i);
  if (bit >> idx & 1)
    return;
  update(idx + 1, bit | (1 << idx), min(HP, hp + p[idx]), i);
}

bool DP() {
  int x = 0, y = 0, size = seq.size();
  memset(dp, 0, sizeof(dp));
  dp[0][0] = hp;
  for (int i = 0; i < size; i++) {
    Go(x, y, seq[i]);
    int cost = dmg[mp[y][x]];
    for (int bit = 0; bit < (1 << P); bit++)
      if (dp[i][bit])
        update(0, bit, dp[i][bit] - cost, i);
  }

  for (int i = 0; i < (1 << P); i++)
    if (dp[size][i])
      return 1;
  return 0;
}

int main() {
  while (1) {
    cin >> hp >> HP;
    if (!hp && !HP)
      return 0;
    cin >> h >> w;
    for (int i = 0; i < h; i++)
      cin >> mp[i];

    int Alpha;
    cin >> Alpha;
    for (int i = 0, cost; i < Alpha; i++) {
      char ch;
      cin >> ch >> cost;
      dmg[ch] = cost;
    }

    int s;
    cin >> s;
    seq.clear();
    for (int i = 0, num; i < s; i++) {
      char ch;
      cin >> ch >> num;
      seq += string(num, ch);
    }

    cin >> P;
    for (int i = 0; i < P; i++)
      cin >> p[i];
    cout << (DP() ? "YES" : "NO") << endl;
  }
  return 0;
}