Problem p01361 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int vy[] = {-1, 1, 0, 0};
const int vx[] = {0, 0, -1, 1};
const string temp = "UDLR";

int main() {
  vector<int> buff[13][1 << 12];
  for (int k = 0; k < 13; k++) {
    for (int i = 0; i < 1 << k; i++) {
      for (int j = 0; j < 1 << k; j++) {
        if (i & j)
          continue;
        buff[k][i].push_back(j);
      }
    }
  }

  int latte, malta, H, W, T, S, P;
  string A[100];
  int damage[256], B[1000], C[12];

  while (cin >> latte >> malta, latte) {
    cin >> H >> W;
    for (int i = 0; i < H; i++)
      cin >> A[i];
    cin >> T;
    for (int i = 0; i < T; i++) {
      char c;
      int d;
      cin >> c >> d;
      damage[c] = d;
    }
    cin >> S;
    int curr = 0;
    for (int i = 0; i < S; i++) {
      char c;
      int d;
      cin >> c >> d;
      int dir = (int)temp.find(c);
      while (d--)
        B[curr++] = dir;
    }
    cin >> P;
    for (int i = 0; i < P; i++) {
      cin >> C[i];
    }

    int yy = 0, xx = 0;

    vector<int> add(1 << P, 0);
    for (int i = 0; i < 1 << P; i++) {
      for (int j = 0; j < P; j++) {
        if ((i >> j) & 1)
          add[i] += C[j];
      }
    }

    vector<int> dp(1 << P, -1);
    dp[0] = latte;
    for (int i = 0; i < curr; i++) {
      vector<int> nextdp(1 << P, -1);
      yy += vy[B[i]], xx += vx[B[i]];
      for (int j = 0; j < 1 << P; j++) {
        if (dp[j] == -1)
          continue;
        if (dp[j] - damage[A[yy][xx]] >= 1) {
          nextdp[j] = max(nextdp[j], dp[j] - damage[A[yy][xx]]);
        }
        for (int k : buff[P][j]) {
          int get = min(malta, dp[j] + add[k]) - damage[A[yy][xx]];
          if (get >= 1)
            nextdp[j | k] = max(nextdp[j | k], get);
        }
      }
      dp.swap(nextdp);
    }
    bool beet = false;
    for (int k : dp)
      if (k >= 1)
        beet = true;
    if (beet)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int vy[] = {-1, 1, 0, 0};
const int vx[] = {0, 0, -1, 1};
const string temp = "UDLR";

int main() {
  vector<int> buff[13][1 << 12];
  for (int k = 0; k < 13; k++) {
    for (int i = 0; i < 1 << k; i++) {
      for (int j = 0; j < 1 << k; j++) {
        if (i & j)
          continue;
        buff[k][i].push_back(j);
      }
    }
  }

  int latte, malta, H, W, T, S, P;
  string A[100];
  int damage[256], B[1000], C[12];

  while (cin >> latte >> malta, latte) {
    cin >> H >> W;
    for (int i = 0; i < H; i++)
      cin >> A[i];
    cin >> T;
    for (int i = 0; i < T; i++) {
      char c;
      int d;
      cin >> c >> d;
      damage[c] = d;
    }
    cin >> S;
    int curr = 0;
    for (int i = 0; i < S; i++) {
      char c;
      int d;
      cin >> c >> d;
      int dir = (int)temp.find(c);
      while (d--)
        B[curr++] = dir;
    }
    cin >> P;
    for (int i = 0; i < P; i++) {
      cin >> C[i];
    }

    int yy = 0, xx = 0;

    vector<int> add(1 << P, 0);
    for (int i = 0; i < 1 << P; i++) {
      for (int j = 0; j < P; j++) {
        if ((i >> j) & 1)
          add[i] += C[j];
      }
    }

    vector<int> dp(1 << P, -1);
    dp[0] = latte;
    for (int i = 0; i < curr; i++) {
      vector<int> nextdp(1 << P, -1);
      yy += vy[B[i]], xx += vx[B[i]];
      for (int j = 0; j < 1 << P; j++) {
        if (dp[j] == -1)
          continue;
        if (dp[j] - damage[A[yy][xx]] >= 1) {
          nextdp[j] = max(nextdp[j], dp[j] - damage[A[yy][xx]]);
          continue;
        }
        for (int k : buff[P][j]) {
          int get = min(malta, dp[j] + add[k]) - damage[A[yy][xx]];
          if (get >= 1)
            nextdp[j | k] = max(nextdp[j | k], get);
        }
      }
      dp.swap(nextdp);
    }
    bool beet = false;
    for (int k : dp)
      if (k >= 1)
        beet = true;
    if (beet)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}