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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int n, hp[111];
int m1, mp1[111], d1[111];
int m2, mp2[111], d2[111];
ll dp1[200010], dp2[200010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  while (cin >> n && n) {
    rep(i, n) cin >> hp[i];
    int m;
    cin >> m;
    m1 = m2 = 0;
    rep(i, m) {
      string s, t;
      int mp, d;
      cin >> s >> mp >> t >> d;
      d = min(d, 100000);
      if (t == "Single") {
        mp1[m1] = mp;
        d1[m1] = d;
        m1++;
      } else {
        mp2[m2] = mp;
        d2[m2] = d;
        m2++;
      }
    }
    fill(dp1, dp1 + 200001, INF);
    fill(dp2, dp2 + 200001, INF);
    dp1[0] = dp2[0] = 0;
    rep2(i, 1, 200001) {
      rep(j, m1) if (d1[j] <= i) {
        dp1[i] = min(dp1[i], mp1[j] + dp1[i - d1[j]]);
      }
      rep(j, m2) if (d2[j] <= i) {
        dp2[i] = min(dp2[i], mp2[j] + dp2[i - d2[j]]);
      }
    }
    for (int i = 199999; i >= 0; i--) {
      dp1[i] = min(dp1[i], dp1[i + 1]);
      dp2[i] = min(dp2[i], dp2[i + 1]);
    }
    ll ans = INF * INF;
    rep(i, 200001) {
      ll res = dp2[i];
      rep(j, n) {
        ll rest = max(0, hp[j] - i);
        res += dp1[rest];
      }
      ans = min(ans, res);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define foreach(it, X)                                                         \\
  for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define ite iterator
#define mp make_pair
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define pb push_back
#define sec second
#define sz(x) ((int)(x).size())

using namespace std;

struct timer {
  time_t start;
  timer() { start = clock(); }
  ~timer() {
    cerr << 1. * (clock() - start) / CLOCKS_PER_SEC << " secs" << endl;
  }
};

typedef istringstream iss;
typedef long long ll;
typedef pair<int, int> pi;
typedef stringstream sst;
typedef vector<int> vi;

int n, hp[111];
int m1, mp1[111], d1[111];
int m2, mp2[111], d2[111];
int dp1[100010], dp2[100010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  while (cin >> n && n) {
    rep(i, n) cin >> hp[i];
    int m;
    cin >> m;
    rep(i, m) {
      string s, t;
      int m, d;
      cin >> s >> m >> t >> d;
      if (t == "Single") {
        mp1[m1] = m;
        d1[m1] = d;
        m1++;
      } else {
        mp2[m2] = m;
        d2[m2] = d;
        m2++;
      }
    }
    fill(dp1, dp1 + 100001, INF);
    fill(dp2, dp2 + 100001, INF);
    dp1[0] = dp2[0] = 0;
    rep2(i, 1, 100001) {
      rep(j, m1) if (d1[j] <= i) {
        dp1[i] = min(dp1[i], mp1[j] + dp1[i - d1[j]]);
      }
      rep(j, m2) if (d2[j] <= i) {
        dp2[i] = min(dp2[i], mp2[j] + dp2[i - d2[j]]);
      }
    }
    for (int i = 99999; i >= 0; i--) {
      dp1[i] = min(dp1[i], dp1[i + 1]);
      dp2[i] = min(dp2[i], dp2[i + 1]);
    }
    int ans = INF;
    rep(i, 100001) {
      int res = dp2[i];
      rep(j, n) {
        int rest = max(0, hp[j] - i);
        res += dp1[rest];
      }
      ans = min(ans, res);
    }
    cout << ans << endl;
  }
}