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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
const int MAXE = 102 * 102;
const int MAXA = 102;
const int INF = 100000000;

void update(int &x, int y) {
  if (x < y)
    x = y;
}

int main() {
  int S, T, U, N, O, D;
  cin >> S >> T >> U >> N >> O >> D;
  vector<int> e(T), c(T);
  REP(i, T) cin >> e[i] >> c[i];
  vector<int> h(N), a(N);
  REP(i, N) cin >> h[i] >> a[i];
  int dp1[MAXA][MAXA] = {};
  REP(i, MAXA) fill(dp1[i], dp1[i] + MAXA, -INF);
  dp1[0][0] = 0;
  int dp2[MAXE] = {};
  fill(dp2, dp2 + MAXE, -INF);
  dp2[0] = 0;
  for (int i = 0; i < T; i++) {
    for (int j = i; j >= 0; j--) {
      for (int k = MAXA - 1 - e[i]; k >= 0; k--) {
        if (dp1[j][k] < 0)
          continue;
        update(dp1[j + 1][k + e[i]], dp1[j][k] + c[i]);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j + a[i] < MAXE; j++) {
      if (dp2[j] < 0)
        continue;
      update(dp2[j + a[i]], dp2[j] + h[i]);
    }
  }
  for (int j = 1; j < MAXE; j++) {
    update(dp2[j], dp2[j - 1]);
  }
  int value[MAXA] = {};
  fill(value, value + MAXA, -INF);
  for (int i = 0; i < MAXA; i++) {
    if (dp1[U][i] < 0)
      continue;
    value[i] = dp2[dp1[U][i]];
  }

  vector<int> cur(S + 1, -INF);
  cur[S] = 0;
  for (int i = 0; i < D; i++) {
    vector<int> next(S + 1, -INF);
    for (int j = 0; j < S + 1; j++) {
      if (cur[j] < 0)
        continue;
      for (int k = 0; k <= j; k++) {
        if (value[k] < 0)
          continue;
        int nj = j - k + O;
        if (nj > S)
          nj = S;
        update(next[nj], cur[j] + value[k]);
      }
    }
    cur.swap(next);
  }
  int ans = -INF;
  for (int i = 0; i < S + 1; i++) {
    update(ans, cur[i]);
  }
  if (ans < 0)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
const int MAXE = 101 * 101;
const int MAXA = 101;
const int INF = 100000000;

void update(int &x, int y) {
  if (x < y)
    x = y;
}

int main() {
  int S, T, U, N, O, D;
  cin >> S >> T >> U >> N >> O >> D;
  vector<int> e(T), c(T);
  REP(i, T) cin >> e[i] >> c[i];
  vector<int> h(N), a(N);
  REP(i, N) cin >> h[i] >> a[i];
  int dp1[MAXA][MAXA] = {};
  REP(i, MAXA) fill(dp1[i], dp1[i] + MAXA, -INF);
  dp1[0][0] = 0;
  int dp2[MAXE] = {};
  fill(dp2, dp2 + MAXE, -INF);
  dp2[0] = 0;
  for (int i = 0; i < T; i++) {
    for (int j = i; j >= 0; j--) {
      for (int k = MAXA - 1 - e[i]; k >= 0; k--) {
        if (dp1[j][k] < 0)
          continue;
        update(dp1[j + 1][k + e[i]], dp1[j][k] + c[i]);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j + a[i] < MAXE; j++) {
      if (dp2[j] < 0)
        continue;
      update(dp2[j + a[i]], dp2[j] + h[i]);
    }
  }
  for (int j = 1; j < MAXE; j++) {
    update(dp2[j], dp2[j - 1]);
  }
  int value[MAXA] = {};
  fill(value, value + MAXA, -INF);
  for (int i = 0; i < MAXA; i++) {
    if (dp1[U][i] < 0)
      continue;
    value[i] = dp2[dp1[U][i]];
  }

  vector<int> cur(S + 1, -INF);
  cur[S] = 0;
  for (int i = 0; i < D; i++) {
    vector<int> next(S + 1, -INF);
    for (int j = 0; j < S + 1; j++) {
      if (cur[j] < 0)
        continue;
      for (int k = 0; k <= j; k++) {
        if (value[k] < 0)
          continue;
        int nj = j - k + O;
        update(next[nj], cur[j] + value[k]);
      }
    }
    cur.swap(next);
  }
  int ans = -INF;
  for (int i = 0; i < S + 1; i++) {
    update(ans, cur[i]);
  }
  if (ans < 0)
    ans = -1;
  cout << ans << endl;
  return 0;
}