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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <stdint.h>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define MAX_N 101
#define MAX_M 101
#define MAX_Damage 1000005
using namespace std;

int N, M;
int HP[MAX_N];
vector<int> MP[2], Damage[2];
bool Target[MAX_M]; // false -> Single, true -> All
uint64_t dp[2][MAX_Damage];

int main() {
  while (cin >> N, N) {
    rep(i, N) cin >> HP[i];
    cin >> M;
    string Name, target;
    int mp, damage;
    rep(i, 2) MP[i].clear(), Damage[i].clear();
    rep(i, M) {
      cin >> Name >> mp >> target >> damage;
      if (damage == 0)
        continue;
      if (target[0] == 'S') {
        Target[i] = 0;
        MP[0].push_back(mp), Damage[0].push_back(damage);
      } else {
        Target[i] = 1;
        MP[1].push_back(mp), Damage[1].push_back(damage);
      }
    }

    rep(i, 2) rep(j, MAX_Damage) dp[i][j] = inf;
    dp[0][0] = dp[1][0] = 0;

    rep(i, 2) {
      rep(j, MP[i].size()) {
        rep(k, MAX_Damage) {
          if (dp[i][k] == inf || k + Damage[i][j] >= MAX_Damage)
            continue;
          dp[i][k + Damage[i][j]] =
              min(dp[i][k + Damage[i][j]], dp[i][k] + MP[i][j]);
        }
      }
    }

    rep(i, 2) {
      for (int j = MAX_Damage - 2; j >= 0; j--) {
        dp[i][j] = min(dp[i][j], dp[i][j + 1]);
      }
    }
    /*
    rep(i,11)
      {
        cout << "dp["<<0<<"]["<<i<<"] = " <<dp[0][i] << endl;
      }
    */
    uint64_t ans = inf;
    rep(i, MAX_Damage) // All
    {
      if (dp[1][i] == inf)
        continue;
      uint64_t cnt = dp[1][i];
      bool ok = true;
      rep(j, N) // Single
      {
        if (HP[j] - i <= 0)
          continue;
        if (dp[0][HP[j] - i] == inf) {
          ok = false;
          break;
        }
        cnt += dp[0][HP[j] - i];
      }
      if (!ok)
        continue;
      // cout << "cnt = " << cnt << " ans = "<< ans << endl;
      ans = min(ans, cnt);
    }
    // cout << "1 : " << ans << endl;
    int mex = -inf;
    uint64_t cnt = 0;
    rep(i, N) // Single
    {
      mex = max(mex, HP[i]);
      cnt += dp[0][HP[i]];
    }
    ans = min(ans, cnt);
    // cout << "2 : " << ans << endl;
    ans = min(ans, dp[1][mex]); // All
    // cout << "3 : " << ans << endl;
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define MAX_N 101
#define MAX_M 101
#define MAX_Damage 1000005
using namespace std;

int N, M;
int HP[MAX_N];
vector<int> MP[2], Damage[2];
bool Target[MAX_M]; // false -> Single, true -> All
int dp[2][MAX_Damage];

int main() {
  while (cin >> N) {
    rep(i, N) cin >> HP[i];
    cin >> M;
    string Name, target;
    int mp, damage;
    rep(i, 2) MP[i].clear(), Damage[i].clear();
    rep(i, M) {
      cin >> Name >> mp >> target >> damage;
      if (damage == 0)
        continue;
      if (target[0] == 'S') {
        Target[i] = 0;
        MP[0].push_back(mp), Damage[0].push_back(damage);
      } else {
        Target[i] = 1;
        MP[1].push_back(mp), Damage[1].push_back(damage);
      }
    }

    rep(i, 2) rep(j, MAX_Damage) dp[i][j] = inf;
    dp[0][0] = dp[1][0] = 0;

    rep(i, 2) {
      rep(j, MP[i].size()) {
        rep(k, MAX_Damage) {
          if (dp[i][k] == inf || k + Damage[i][j] >= MAX_Damage)
            continue;
          dp[i][k + Damage[i][j]] =
              min(dp[i][k + Damage[i][j]], dp[i][k] + MP[i][j]);
        }
      }
    }

    rep(i, 2) {
      for (int j = MAX_Damage - 2; j >= 0; j--) {
        dp[i][j] = min(dp[i][j], dp[i][j + 1]);
      }
    }

    int ans = inf;
    rep(i, MAX_Damage) // All
    {
      if (dp[1][i] == inf)
        continue;
      int cnt = dp[1][i];
      rep(j, N) // Single
      {
        if (HP[j] - i <= 0)
          continue;
        if (dp[0][HP[j] - i] == inf)
          break;
        cnt += dp[0][HP[j] - i];
      }
      ans = min(ans, cnt);
    }

    int mex = -inf;
    int cnt = 0;
    rep(i, N) // Single
    {
      mex = max(mex, HP[i]);
      cnt += dp[0][HP[i]];
    }
    ans = min(ans, cnt);
    ans = min(ans, dp[1][mex]); // All

    cout << ans << endl;
  }
  return 0;
}