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

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long LL;

#define INF 1000000000

struct spell {
  LL mp;
  LL damage;
};

struct spell sps[100], spa[100];
LL dps[100001], dpa[100001];

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      return 0;
    vector<LL> hp(n);
    for (int i = 0; i < n; i++) {
      cin >> hp[i];
    }
    int m;
    cin >> m;
    int counts = 0, counta = 0;
    string name, target;
    LL mp, damage;
    for (int i = 0; i < m; i++) {
      cin >> name >> mp >> target >> damage;
      if (target == "Single") {
        sps[counts] = {mp, damage};
        counts++;
      } else {
        spa[counta] = {mp, damage};
        counta++;
      }
    }
    for (int i = 0; i < 100001; i++) {
      dpa[i] = dps[i] = INF;
    }
    dpa[0] = dps[0] = 0;
    for (int i = 1; i < 100001; i++) {
      for (int j = 0; j < counts; j++) {
        if (i - sps[j].damage < 0) {
          dps[i] = min(dps[i], sps[j].mp);
        } else {
          dps[i] = min(dps[i], dps[i - sps[j].damage] + sps[j].mp);
        }
      }
    }
    for (int i = 1; i < 100001; i++) {
      for (int j = 0; j < counta; j++) {
        if (i - spa[j].damage < 0) {
          dpa[i] = min(dpa[i], spa[j].mp);
        } else {
          dpa[i] = min(dpa[i], dpa[i - spa[j].damage] + spa[j].mp);
        }
      }
    }
    LL ans = INF;
    LL count;
    for (int i = 0; i < 100001; i++) {
      count = dpa[i];
      for (int j = 0; j < n; j++) {
        count += (hp[j] - i >= 0 ? dps[hp[j] - i] : 0);
      }
      ans = min(ans, count);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

typedef long long LL;

#define INF 1000000000

struct spell {
  int mp;
  int damage;
};

int main() {
  int n;
  cin >> n;
  vector<int> hp(n);
  for (int i = 0; i < n; i++) {
    cin >> hp[i];
  }
  int m;
  cin >> m;
  vector<struct spell> sps(m), spa(m);
  int counts = 0, counta = 0;
  string name, target;
  int mp, damage;
  for (int i = 0; i < n; i++) {
    cin >> name >> mp >> target >> damage;
    if (target == "Single") {
      sps[counts] = {mp, damage};
      counts++;
    } else {
      spa[counta] = {mp, damage};
      counta++;
    }
  }
  vector<int> dps(100001, INF), dpa(100001, INF);
  dpa[0] = dps[0] = 0;
  for (int i = 1; i < 100001; i++) {
    for (int j = 0; j < counts; j++) {
      if (i - sps[j].damage < 0) {
        dps[i] = min(dps[i], sps[j].mp);
      } else {
        dps[i] = min(dps[i], dps[i - sps[j].damage] + sps[j].mp);
      }
    }
  }
  for (int i = 1; i < 100001; i++) {
    for (int j = 0; j < counta; j++) {
      if (i - spa[j].damage < 0) {
        dpa[i] = min(dpa[i], spa[j].mp);
      } else {
        dpa[i] = min(dpa[i], dpa[i - spa[j].damage] + spa[j].mp);
      }
    }
  }
  int ans = INF;
  int count;
  for (int i = 0; i < 100001; i++) {
    count = dpa[i];
    for (int j = 0; j < n; j++) {
      count += (hp[j] - i >= 0 ? dps[hp[j] - i] : 0);
    }
    ans = min(ans, count);
  }
  cout << ans << endl;
  return 0;
}