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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

int n, m, am, sm, hp[110], maxhp;
ll amp[110], adm[110], smp[110], sdm[110];
ll dpa[100100], dps[100100];
string name;
ll tmp;
const ll INF = (ll)(1e15);

int main() {
  while (cin >> n, n) {
    maxhp = 0;
    for (int i = 0; i < n; i++) {
      cin >> hp[i];
      maxhp = max(maxhp, hp[i]);
    }

    cin >> m;
    am = sm = 0;

    for (int i = 0; i < m; i++) {
      cin >> name >> tmp;
      cin >> name;
      if (name == "All")
        amp[am] = tmp;
      else
        smp[sm] = tmp;
      cin >> tmp;
      if (name == "All")
        adm[am++] = tmp;
      else
        sdm[sm++] = tmp;
    }

    for (int i = 0; i <= maxhp; i++)
      dpa[i] = INF;
    dpa[0] = 0;

    for (int i = 0; i < am; i++) {
      for (int j = 0; j <= maxhp; j++) {
        if (dpa[j] < INF) {
          if (j + adm[i] <= maxhp) {
            dpa[j + adm[i]] = min(dpa[j + adm[i]], dpa[j] + amp[i]);
          } else {
            dpa[maxhp] = min(dpa[maxhp], dpa[j] + amp[i]);
          }
        }
      }
    }

    tmp = INF;
    for (int i = maxhp; i >= 0; i--) {
      if (tmp < dpa[i])
        dpa[i] = tmp;
      else
        tmp = dpa[i];
    }

    for (int i = 0; i <= maxhp; i++)
      dps[i] = INF;
    dps[0] = 0;

    for (int i = 0; i < sm; i++) {
      for (int j = 0; j <= maxhp; j++) {
        if (dps[j] < INF) {
          if (j + sdm[i] <= maxhp) {
            dps[j + sdm[i]] = min(dps[j + sdm[i]], dps[j] + smp[i]);
          } else {
            dps[maxhp] = min(dps[maxhp], dps[j] + smp[i]);
          }
        }
      }
    }

    tmp = INF;
    for (int i = maxhp; i >= 0; i--) {
      if (tmp < dps[i])
        dps[i] = tmp;
      else
        tmp = dps[i];
    }

    ll ans = INF;
    for (int i = 0; i <= maxhp; i++) {
      tmp = dpa[i];
      for (int j = 0; j < n; j++) {
        int rem = hp[j] - i;
        if (rem <= 0)
          continue;
        tmp += dps[rem];
      }
      ans = min(ans, tmp);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef long long ll;

int n, m, am, sm, hp[110], maxhp;
ll amp[110], adm[110], smp[110], sdm[110];
ll dpa[100100], dps[100100];
string name;
ll tmp;
const ll INF = (ll)(1e15);

int main() {
  while (cin >> n, n) {
    maxhp = 0;
    for (int i = 0; i < n; i++) {
      cin >> hp[i];
      maxhp = max(maxhp, hp[i]);
    }

    cin >> m;
    am = sm = 0;

    for (int i = 0; i < m; i++) {
      cin >> name >> tmp;
      cin >> name;
      if (name == "All")
        amp[am] = tmp;
      else
        smp[sm] = tmp;
      cin >> tmp;
      if (name == "All")
        adm[am++] = tmp;
      else
        sdm[sm++] = tmp;
    }

    for (int i = 0; i <= maxhp; i++)
      dpa[i] = INF;
    dpa[0] = 0;

    for (int i = 0; i < am; i++) {
      for (int j = 0; j <= maxhp; j++) {
        if (dpa[j] < INF && j + adm[i] <= maxhp) {
          dpa[j + adm[i]] = min(dpa[j + adm[i]], dpa[j] + amp[i]);
        }
      }
    }

    tmp = INF;
    for (int i = maxhp; i >= 0; i--) {
      if (tmp < dpa[i])
        dpa[i] = tmp;
      else
        tmp = dpa[i];
    }

    for (int i = 0; i <= maxhp; i++)
      dps[i] = INF;
    dps[0] = 0;

    for (int i = 0; i < sm; i++) {
      for (int j = 0; j <= maxhp; j++) {
        if (dps[j] < INF && j + sdm[i] <= maxhp) {
          dps[j + sdm[i]] = min(dps[j + adm[i]], dps[j] + smp[i]);
        }
      }
    }

    tmp = INF;
    for (int i = maxhp; i >= 0; i--) {
      if (tmp < dps[i])
        dps[i] = tmp;
      else
        tmp = dps[i];
    }

    ll ans = INF;
    for (int i = 0; i <= maxhp; i++) {
      tmp = dpa[i];
      for (int j = 0; j < n; j++) {
        int rem = hp[j] - i;
        if (rem <= 0)
          continue;
        tmp += dps[rem];
      }
      ans = min(ans, tmp);
    }
    cout << ans << endl;
  }
}