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

#include <cstdio>
#include <cstring>
#define M 108
int dp[M * 10][5000];
char map1[M][M];
int a[M];
int b[M * 100];
int c[M];
int n, m;
int my_hash[M];
int o[4][2] = {0, -1, 1, 0, 0, 1, -1, 0};
int min(int x, int y) {
  if (x > y)
    return y;
  return x;
}
int max(int x, int y) {
  if (x < y)
    return y;
  return x;
}
int main() {
  int sp, tmax;
  while (~scanf("%d%d", &sp, &tmax) && (sp + tmax)) {
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
      scanf("%s", &map1[i]);
    int n1;
    scanf("%d", &n1);
    for (int i = 0; i < n1; i++) {
      char str[100];
      int t1;
      scanf("%s%d", str, &t1);
      my_hash[str[0] - 'A'] = t1;
    }
    int m1;
    scanf("%d", &m1);
    int sx = 0, sy = 0;
    int num = 1;
    memset(b, 0, sizeof(b));
    for (int i = 0; i < m1; i++) {
      char str[20];
      int m2;
      scanf("%s%d", str, &m2);
      int oo;
      if (str[0] == 'U')
        oo = 3;
      else if (str[0] == 'R')
        oo = 2;
      else if (str[0] == 'D')
        oo = 1;
      else if (str[0] == 'L')
        oo = 0;
      for (int i = 0; i < m2; i++) {
        sx = sx + o[oo][0];
        sy = sy + o[oo][1];
        int t1 = map1[sx][sy] - 'A';
        b[num++] = my_hash[t1];
      }
    }
    //        puts("@@@@@@@@@");
    int n2;
    scanf("%d", &n2);
    for (int i = 0; i < n2; i++) {
      scanf("%d", &c[i]);
    }
    //        for(int i=0;i<num;i++){
    //            printf("%d ",b[i]);
    //        }
    //        printf("%d!!!\\n",num);
    //        puts("");
    memset(dp, 0, sizeof(dp));
    int t2 = 1 << n2;
    t2--;
    //        printf("%d\\n",t2);
    dp[0][t2] = sp;
    for (int i = 0; i < num - 1; i++) {
      for (int j = t2; j >= 0; j--) {
        if (dp[i][j] <= 0)
          continue;
        if (dp[i][j] <= b[i + 1]) {
          //                    printf("%d!!!!!\\n",b[i+1]);
          for (int k = 0; k < n2; k++) {
            if (j & (1 << k)) {
              int tt = j ^ (1 << k);
              int t1 = min(tmax, dp[i][j] + c[k]);
              dp[i][tt] = max(t1, dp[i][tt]);
            }
          }
        } else {
          int t1 = dp[i][j] - b[i + 1];
          dp[i + 1][j] = max(dp[i + 1][j], t1);
        }
      }
    }
    int flag = 0;
    for (int i = 0; i <= t2; i++) {
      if (dp[num - 1][i] > 0)
        flag = 1;
      //            printf("%d  !!\\n",dp[num-1][i]);
    }
    if (flag)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}
/*
1 10
3 3
AAA
CAA
CBC
3
A 0
B 10000
C 9
3
D 2
R 1
U 2
5
4
4
4
4
4
*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
const int M = 110;
const int inf = 0x3fffffff;
char a[M][M];
int d[64];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int yao[64];
int kou[1100];
struct G {
  char s[4];
  int step;
} g[1100];
bool flag;
int lk, T, snum, hpmax;
int dp[1105][1 << 12];
int main() {
  int hp, n, m, dis, S, i, j;
  char ts[4];
  int x, y;
  while (~scanf("%d%d", &hp, &hpmax), hp + hpmax) {
    x = y = 0;
    scanf("%d%d", &n, &m);
    for (i = 0; i < n; i++) {
      scanf("%s", a[i]);
    }
    scanf("%d", &T);
    while (T--) {
      scanf("%s%d", ts, &dis);
      d[ts[0] - 'A'] = dis;
    }
    scanf("%d", &S);
    for (i = 0; i < S; i++) {
      scanf("%s%d", g[i].s, &g[i].step);
    }
    int sum1 = 0, sum2 = 0, mark = 0;
    scanf("%d", &T);

    snum = T;
    for (i = 0; i < T; i++) {
      scanf("%d", &yao[i]);
      sum1 += yao[i];
    }
    lk = 0;

    for (i = 0; i < S; i++) {
      int id;
      if (g[i].s[0] == 'U') {
        id = 0;
      } else if (g[i].s[0] == 'D') {
        id = 1;
      } else if (g[i].s[0] == 'L') {
        id = 2;
      } else if (g[i].s[0] == 'R') {
        id = 3;
      }
      for (int j = 0; j < g[i].step; j++) {
        x += dx[id];
        y += dy[id];
        dis = d[a[x][y] - 'A'];
        kou[lk++] = dis;
        if (dis > hpmax)
          mark = 1;
        sum2 += dis;
      }
    }
    if (mark || (sum1 + hp <= sum2)) {
      puts("NO");
      continue;
    }
    int Max = 1 << snum;
    for (i = 0; i < Max; i++)
      dp[0][i] = hp;
    for (i = 0; i < lk; i++) {
      for (j = Max - 1; j >= 0; j--) {
        for (int k = 0; k < snum; k++) {
          if ((1 << k) & (j))
            continue;
          // if(i==0)cout<<"fdsf"<<j<<endl;
          // if(dp[i-1][j]==-inf)continue;
          dp[i][(j | (1 << k))] =
              max(dp[i - 1][j] + yao[k], dp[i][j | (1 << k)]);
          dp[i][(j | (1 << k))] = min(dp[i][j | (1 << k)], hpmax);
        }
      }
      for (j = 0; j < Max; j++) {
        // if(i==0)cout<<dp[i][j]<<endl;
        dp[i][j] -= kou[i];
        if (dp[i][j] <= 0)
          dp[i][j] = -inf;
      }
    }
    int ans = -inf;
    for (i = 0; i < Max; i++)
      ans = max(ans, dp[lk - 1][i]);
    // cout<<ans<<"d"<<endl;
    if (ans > 0)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}