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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16

ll n, m;
ll h[111];
ll dpa[1000010], dpb[1000010];
ll na, nb;
ll ma[111], mb[111], da[111], db[111];

int main() {
  // cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ll maxh = 0;
    rep(i, n) {
      cin >> h[i];
      maxch(maxh, h[i]);
    }
    cin >> m;
    na = 0;
    nb = 0;
    rep(i, m) {
      string name, target;
      ll mp, dmg;
      cin >> name >> mp >> target >> dmg;
      if (target == "All") {
        ma[na] = mp;
        da[na] = dmg;
        na++;
      } else {
        mb[nb] = mp;
        db[nb] = dmg;
        nb++;
      }
    }
    rep(i, 1000010) dpa[i] = dpb[i] = INF;
    dpa[0] = dpb[0] = 0;
    rep(j, 1000001) rep(i, na) {
      if (dpa[j] == INF)
        continue;
      if (j + da[i] <= 1000000)
        minch(dpa[j + da[i]], dpa[j] + ma[i]);
    }
    for (ll j = 1000000; j >= 0; j--)
      minch(dpa[j], dpa[j + 1]);
    rep(j, 1000001) rep(i, nb) {
      if (dpb[j] == INF)
        continue;
      if (j + db[i] <= 1000000)
        minch(dpb[j + db[i]], dpb[j] + mb[i]);
    }
    for (ll j = 1000000; j >= 0; j--)
      minch(dpb[j], dpb[j + 1]);
    ll res = INF;
    rep(ah, maxh + 1) {
      ll sum = 0;
      rep(i, n) { sum += dpb[max(h[i] - ah, 0LL)]; }
      minch(res, sum + dpa[ah]);
    }
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16

ll n, m;
ll h[1111];
map<ll, ll> dpa, dpb;
ll na, nb;
ll ma[1111], mb[1111], da[1111], db[1111];

ll dfsa(ll i) {
  if (dpa.find(i) != dpa.end())
    return dpa[i];
  if (i == 0)
    return 0;
  ll res = INF;
  rep(j, na) { minch(res, ma[j] + dfsa(mmax(i - da[j], 0))); }
  return dpa[i] = res;
}

ll dfsb(ll i) {
  if (dpb.find(i) != dpb.end())
    return dpb[i];
  if (i == 0)
    return 0;
  ll res = INF;
  rep(j, nb) { minch(res, mb[j] + dfsb(mmax(i - db[j], 0))); }
  return dpb[i] = res;
}

int main() {
  // cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ll maxh = 0;
    rep(i, n) {
      cin >> h[i];
      maxch(maxh, h[i]);
    }
    cin >> m;
    na = 0;
    nb = 0;
    rep(i, m) {
      string name, target;
      ll mp, dmg;
      cin >> name >> mp >> target >> dmg;
      if (target == "All") {
        ma[na] = mp;
        da[na] = dmg;
        na++;
      } else {
        mb[nb] = mp;
        db[nb] = dmg;
        nb++;
      }
    }
    dpa.clear();
    dpb.clear();
    ll res = INF;
    rep(ah, maxh + 1) {
      ll sum = 0;
      rep(i, n) { sum += dfsb(mmax(h[i] - ah, 0LL)); }
      minch(res, sum + dfsa(ah));
    }
    cout << res << endl;
  }
  return 0;
}