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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pint;

int dp[2][101][1001];
int sopt[1001], opt[1001];
const int INF = 1000000;
void dpru(int n, int low, int upper, int limit, int *e, int *s, int *opt) {
  for (int i = 0; i < 2; ++i)
    for (int j = 0; j <= upper; ++j)
      for (int k = 0; k <= limit; ++k)
        dp[i][j][k] = -INF;
  dp[0][0][0] = 0;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= upper; ++j) {
      for (int k = 0; k <= limit; ++k) {
        dp[(i + 1) & 1][j][k] = max(dp[(i + 1) & 1][j][k], dp[i & 1][j][k]);
        if (k + e[i] <= limit)
          dp[(i + 1) & 1][j + 1][k + e[i]] =
              max(dp[(i + 1) & 1][j + 1][k + e[i]], dp[i & 1][j][k] + s[i]);
      }
    }
  }

  for (int i = 0; i <= limit; ++i) {
    opt[i] = -INF;
    for (int j = low; j <= upper; ++j)
      opt[i] = max(opt[i], dp[n & 1][j][i]);
  }
}
int main() {
  int limit, n, m, x;
  while (cin >> limit >> n >> m >> x, limit | n | m | x) {
    string hoge;
    int se[100], ss[100], e[100], s[100];
    for (int i = 0; i < n; ++i)
      cin >> hoge >> se[i] >> ss[i];
    for (int i = 0; i < m; ++i)
      cin >> hoge >> e[i] >> s[i];

    dpru(n, 1, 2, limit, se, ss, sopt);
    dpru(m, x, m, limit, e, s, opt);

    int res = 0;
    for (int i = 0; i <= limit; ++i)
      for (int j = 0; i + j <= limit; ++j)
        res = max(res, sopt[i] + opt[j]);

    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-8;
typedef long long ll;
typedef pair<int, int> pint;

int dp[100 + 1][100 + 1][1001];
int main() {
  int limit, n, m, x;
  while (cin >> limit >> n >> m >> x, limit | n | m | x) {
    string hoge;
    int se[100], ss[100], e[100], s[100];
    for (int i = 0; i < n; ++i)
      cin >> hoge >> se[i] >> ss[i];
    for (int i = 0; i < m; ++i)
      cin >> hoge >> e[i] >> s[i];

    // memset(dp, 0, sizeof(dp));
    // for (int i = 0; i <= m; ++i)
    //	for (int j = 0; j <= limit; ++j)
    //		dp[0][i][j] = dp[m][i][j] = -10000000;
    for (int i = 0; i <= m; ++i)
      for (int j = 0; j <= x; ++j)
        for (int k = 0; k <= limit; ++k)
          dp[i][j][k] = -10000;

    for (int i = 0; i < n; ++i) {
      dp[0][0][se[i]] = max(dp[0][0][se[i]], ss[i]);
      for (int j = i + 1; j < n; ++j)
        dp[0][0][se[i] + se[j]] =
            max(dp[0][0][se[i] + se[j]], (int)(ss[i] + ss[j]));
    }

    for (int i = 0; i < m; ++i) {
      for (int j = 0; j <= x; ++j) {
        for (int k = 0; k <= limit; ++k) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
          if (k + e[i] <= limit)
            dp[i + 1][j + 1][k + e[i]] =
                max(dp[i + 1][j + 1][k + e[i]], (int)(dp[i][j][k] + s[i]));
        }
      }
    }

    int res = 0;
    for (int i = x; i <= m; ++i)
      for (int j = 0; j <= limit; ++j)
        res = max(res, dp[m][i][j]);

    cout << res << endl;
  }

  return 0;
}