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

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
constexpr int INF = 1000000000LL;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return std::vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return std::vector<decltype(inner)>(x, inner); }
template<typename T = int, typename... Args> T in(Args &&... args) { T x(args...); std::cin >> x; return x; }
using namespace std;
// clang-format on

signed main() {
  while (true) {
    int n = in();
    if (n == 0)
      break;
    auto h = in<vector<int>>(n);
    int m = in();
    int maxH = *max_element(all(h));
    auto dp = vectors(2, maxH + 1, INF);
    dp[0][0] = dp[1][0] = 0;
    rep(_, m) {
      auto name = in<string>();
      int p = in();
      auto t = in<string>();
      int d = in();
      if (d == 0)
        continue;
      rep(i, maxH + 1)
          cmin(dp[t[0] == 'A'][min(i + d, maxH)], dp[t[0] == 'A'][i] + p);
    }
    for (int i = maxH - 1; i >= 0; i--)
      cmin(dp[0][i], dp[0][i + 1]);
    int ans = INF;
    rep(i, maxH + 1) {
      if (dp[1][i] == INF)
        continue;
      int s = dp[1][i];
      rep(j, n) s += dp[0][max(0LL, h[j] - i)];
      cmin(ans, s);
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

// clang-format off
#include <bits/stdc++.h>
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
constexpr int INF = 1000000000;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return std::vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return std::vector<decltype(inner)>(x, inner); }
template<typename T = int, typename... Args> T in(Args &&... args) { T x(args...); std::cin >> x; return x; }
using namespace std;
// clang-format on

int main() {
  while (true) {
    int n = in();
    if (n == 0)
      break;
    auto h = in<vector<int>>(n);
    int m = in();
    int maxH = *max_element(all(h));
    auto dp = vectors(2, maxH + 1, INF);
    dp[0][0] = dp[1][0] = 0;
    rep(_, m) {
      auto name = in<string>();
      int p = in();
      auto t = in<string>();
      int d = in();
      if (d == 0)
        continue;
      loop(i, d, maxH + 1) cmin(dp[t[0] == 'A'][i], dp[t[0] == 'A'][i - d] + p);
    }
    rep(k, 2) for (int i = maxH - 1; i >= 0; i--) cmin(dp[k][i], dp[k][i + 1]);
    int ans = INF;
    rep(i, maxH + 1) {
      if (dp[1][i] == INF)
        continue;
      int c = dp[1][i];
      rep(j, m) c += dp[0][max(0, h[j] - i)];
      cmin(ans, c);
    }
    cout << ans << endl;
  }
}