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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int main() {
  int init_hp, max_hp;
  while (cin >> init_hp >> max_hp, init_hp | max_hp) {
    int r, c;
    cin >> r >> c;
    vs a(r, string(c, ' '));
    REP(i, r) { cin >> a[i]; }
    int t;
    cin >> t;
    vi trap_damages('Z' + 1);
    REP(i, t) {
      char c;
      cin >> c;
      cin >> trap_damages[c];
    }
    int s;
    cin >> s;
    int total_steps = 0;
    int cnt = 0;
    vector<pii> steps(1000);
    int y = 0, x = 0;
    REP(i, s) {
      char direction;
      int step;
      cin >> direction >> step;
      int dy = 0, dx = 0;
      switch (direction) {
      case 'U':
        dy = -1;
        break;
      case 'D':
        dy = 1;
        break;
      case 'L':
        dx = -1;
        break;
      case 'R':
        dx = 1;
        break;
      }
      REP(j, step) {
        y += dy;
        x += dx;
        steps[cnt].first = y;
        steps[cnt].second = x;
        cnt++;
      }
      total_steps += step;
    }
    int p;
    cin >> p;
    vi potions(p);
    REP(i, p) { cin >> potions[i]; }

    vi cured_hp_for_potion_mask(1 << p);
    REP(i, 1 << p) {
      REP(j, p) {
        if (i & (1 << j)) {
          cured_hp_for_potion_mask[i] += potions[j];
        }
      }
    }

    vvi hp(total_steps + 1, vi(1 << p));
    hp[0][0] = init_hp;
    REP(step, total_steps) {
      char trap = a[steps[step].first][steps[step].second];
      int damage = trap_damages[trap];
      REP(potion_mask, 1 << p) {
        if (hp[step][potion_mask] <= 0) {
          continue;
        }
        if (hp[step][potion_mask] <= damage) {
          REP(used_potion_mask, 1 << p) {
            if ((used_potion_mask & potion_mask) == 0) {
              int new_hp = min(hp[step][potion_mask] +
                                   cured_hp_for_potion_mask[used_potion_mask],
                               max_hp) -
                           damage;
              hp[step + 1][potion_mask | used_potion_mask] =
                  max(hp[step + 1][potion_mask | used_potion_mask], new_hp);
            }
          }
        } else {
          hp[step + 1][potion_mask] =
              max(hp[step + 1][potion_mask], hp[step][potion_mask] - damage);
        }
      }
    }
    int ans = 0;
    REP(potion_mask, 1 << p) { ans = max(ans, hp[total_steps][potion_mask]); }
    cout << (ans > 0 ? "YES" : "NO") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
const double EPS = 1e-9;
const double PI = acos(-1.0);

int main() {
  int init_hp, max_hp;
  while (cin >> init_hp >> max_hp, init_hp | max_hp) {
    int r, c;
    cin >> r >> c;
    vs a(r, string(c, ' '));
    REP(i, r) { cin >> a[i]; }
    int t;
    cin >> t;
    vi trap_damages('Z' + 1);
    REP(i, t) {
      char c;
      cin >> c;
      cin >> trap_damages[c];
    }
    int s;
    cin >> s;
    int total_steps = 0;
    int cnt = 0;
    vector<pii> steps(1000);
    int y = 0, x = 0;
    REP(i, s) {
      char direction;
      int step;
      cin >> direction >> step;
      int dy = 0, dx = 0;
      switch (direction) {
      case 'U':
        dy = -1;
        break;
      case 'D':
        dy = 1;
        break;
      case 'L':
        dx = -1;
        break;
      case 'R':
        dx = 1;
        break;
      }
      REP(j, step) {
        y += dy;
        x += dx;
        steps[cnt].first = y;
        steps[cnt].second = x;
        cnt++;
      }
      total_steps += step;
    }
    int p;
    cin >> p;
    vi potions(p);
    REP(i, p) { cin >> potions[i]; }

    vvi hp(total_steps + 1, vi(1 << p));
    hp[0][0] = init_hp;
    REP(step, total_steps) {
      char trap = a[steps[step].first][steps[step].second];
      int damage = trap_damages[trap];
      REP(potion_mask, 1 << p) {
        if (hp[step][potion_mask] <= 0) {
          continue;
        }
        if (hp[step][potion_mask] <= damage) {
          REP(used_potion_mask, 1 << p) {
            bool ok = true;
            int cured_hp = 0;
            REP(i, p) {
              if ((used_potion_mask & (1 << i)) && (potion_mask & (1 << i))) {
                ok = false;
              }
              if ((used_potion_mask & (1 << i))) {
                cured_hp += potions[i];
              }
            }
            if (ok) {
              int new_hp =
                  min(hp[step][potion_mask] + cured_hp, max_hp) - damage;
              hp[step + 1][potion_mask | used_potion_mask] =
                  max(hp[step + 1][potion_mask | used_potion_mask], new_hp);
            }
          }
        } else {
          hp[step + 1][potion_mask] =
              max(hp[step + 1][potion_mask], hp[step][potion_mask] - damage);
        }
      }
    }
    int ans = 0;
    REP(potion_mask, 1 << p) { ans = max(ans, hp[total_steps][potion_mask]); }
    cout << (ans > 0 ? "YES" : "NO") << endl;
  }
}