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

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

using ll = long long;
const ll HSIZE = 100001;
const ll INF = 1e15;

ll solve(int n) {
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  int m;
  cin >> m;
  vector<ll> smp, sdmg;
  vector<ll> amp, admg;
  bool isok = false;
  for (int i = 0; i < m; i++) {
    string buf, tar;
    int mp, dmg;
    cin >> buf >> mp >> tar >> dmg;
    if (mp == 0) {
      if (dmg == 0)
        continue;
      isok = true;
    } else if (tar == "Single") {
      smp.push_back(mp);
      sdmg.push_back(dmg);
    } else {
      amp.push_back(mp);
      admg.push_back(dmg);
    }
  }
  if (isok)
    return 0;
  int N = sdmg.size();
  vector<ll> sdp;
  {
    vector<vector<ll>> tmp(2, vector<ll>(HSIZE, INF));
    tmp[0][0] = 0;
    for (int i = 0; i < N; i++) {
      int fr = i & 1;
      int to = (i + 1) & 1;
      fill(tmp[to].begin(), tmp[to].end(), INF);
      for (int j = 0; j < HSIZE; j++) {
        tmp[to][j] = min(tmp[to][j], tmp[fr][j]);
        tmp[to][min(j + sdmg[i], HSIZE - 1)] =
            min(tmp[to][min(j + sdmg[i], HSIZE - 1)], tmp[to][j] + smp[i]);
      }
    }
    sdp = tmp[N & 1];
  }
  N = admg.size();
  vector<ll> adp;
  {
    vector<vector<ll>> tmp(2, vector<ll>(HSIZE, INF));
    tmp[0][0] = 0;
    for (int i = 0; i < N; i++) {
      int fr = i & 1;
      int to = (i + 1) & 1;
      fill(tmp[to].begin(), tmp[to].end(), INF);
      for (int j = 0; j < HSIZE; j++) {
        tmp[to][j] = min(tmp[to][j], tmp[fr][j]);
        tmp[to][min(j + admg[i], HSIZE - 1)] =
            min(tmp[to][min(j + admg[i], HSIZE - 1)], tmp[to][j] + amp[i]);
      }
    }
    adp = tmp[N & 1];
  }
  for (int i = HSIZE - 2; i >= 0; i--) {
    sdp[i] = min(sdp[i], sdp[i + 1]);
  }
  ll res = ll(1e15);
  for (int i = 0; i < HSIZE; i++) {
    ll sc = adp[i];
    for (int j = 0; j < n; j++) {
      sc += sdp[max(h[j] - i, 0)];
    }
    res = min(res, sc);
  }
  return res;
}
int main() {
  int n;
  while (cin >> n, n) {
    cout << solve(n) << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

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

const int HSIZE = 100001;
const int INF = 1e9;
using ll = long long;
ll solve(int n) {
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  int m;
  cin >> m;
  vector<int> smp, sdmg;
  vector<int> amp, admg;
  bool isok = false;
  for (int i = 0; i < m; i++) {
    string buf, tar;
    int mp, dmg;
    cin >> buf >> mp >> tar >> dmg;
    if (mp == 0) {
      if (dmg == 0)
        continue;
      isok = true;
    } else if (tar == "Single") {
      smp.push_back(mp);
      sdmg.push_back(dmg);
    } else {
      amp.push_back(mp);
      admg.push_back(dmg);
    }
  }
  if (isok)
    return 0;
  int N = sdmg.size();
  vector<int> sdp;
  {
    vector<vector<int>> tmp(2, vector<int>(HSIZE, INF));
    tmp[0][0] = 0;
    for (int i = 0; i < N; i++) {
      int fr = i & 1;
      int to = (i + 1) & 1;
      fill(tmp[to].begin(), tmp[to].end(), INF);
      for (int j = 0; j < HSIZE; j++) {
        tmp[to][j] = min(tmp[to][j], tmp[fr][j]);
        if (j + sdmg[i] < HSIZE)
          tmp[to][j + sdmg[i]] = min(tmp[to][j + sdmg[i]], tmp[to][j] + smp[i]);
      }
    }
    sdp = tmp[N & 1];
  }
  N = admg.size();
  vector<int> adp;
  {
    vector<vector<int>> tmp(2, vector<int>(HSIZE, INF));
    tmp[0][0] = 0;
    for (int i = 0; i < N; i++) {
      int fr = i & 1;
      int to = (i + 1) & 1;
      fill(tmp[to].begin(), tmp[to].end(), INF);
      for (int j = 0; j < HSIZE; j++) {
        tmp[to][j] = min(tmp[to][j], tmp[fr][j]);
        if (j + admg[i] < HSIZE)
          tmp[to][j + admg[i]] = min(tmp[to][j + admg[i]], tmp[to][j] + amp[i]);
      }
    }
    adp = tmp[N & 1];
  }
  for (int i = HSIZE - 2; i >= 0; i--) {
    sdp[i] = min(sdp[i], sdp[i + 1]);
  }
  ll res = ll(1e15);
  for (int i = 0; i < HSIZE; i++) {
    ll sc = adp[i];
    for (int j = 0; j < n; j++) {
      sc += sdp[h[j] - i];
    }
    res = min(res, sc);
  }
  return res;
}
int main() {
  int n;
  while (cin >> n, n) {
    cout << solve(n) << endl;
  }
  return 0;
}