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

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

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &a) {
  return is >> a.first >> a.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &a) {
  return os << a.first << " " << a.second;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    is >> vc[i];
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vc) {
  for (int i = 0; i < vc.size(); i++)
    os << vc[i] << endl;
  return os;
}

#define ForEach(it, c)                                                         \\
  for (__typeof(c).begin() it = (c).begin(); it != (c).end(); it++)
#define ALL(v) (v).begin(), (v).end()
#define UNQ(s)                                                                 \\
  {                                                                            \\
    sort(ALL(s));                                                              \\
    (s).erase(unique(ALL(s)), (s).end());                                      \\
  }
#define fr first
#define sc second

typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;

typedef long long int64;
const int INF = 1 << 30;

int main() {
  int C, D, W, X;
  vector<vector<int>> E, F;
  vector<vector<vector<int>>> dp;

  while (cin >> C >> D >> W >> X, C) {
    E = vector<vector<int>>(C, vector<int>(D));
    F = vector<vector<int>>(C, vector<int>(D));
    dp = vector<vector<vector<int>>>(
        D + 1, vector<vector<int>>(W + 1, vector<int>(X + 1, 0)));

    cin >> E;
    cin >> F;

    for (int i = 0; i < D; i++) {
      for (int j = 0; j <= X; j++) {
        for (int k = 0; k < C; k++) {
          if (E[k][i] == 0)
            continue;
          for (int l = W; l >= F[k][i]; l--) {
            // 複数回ライブをやる
            if (j > 0) {
              int value = 0, futan = 0;
              for (int m = k; m >= 0; m--) {
                if (E[m][i] == 0)
                  break;
                value += E[m][i];
                futan += F[m][i];
                if (l - futan < 0)
                  break;
                dp[i + 1][l - futan][j - 1] =
                    max(dp[i + 1][l - futan][j - 1], dp[i][l][j] + value);
              }
            }
            // 一回だけ
            dp[i + 1][l - F[k][i]][j] =
                max(dp[i + 1][l - F[k][i]][j], dp[i][l][j] + E[k][i]);
          }
        }
      }
      for (int j = 0; j <= W; j++) {
        for (int k = 0; k <= X; k++) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
        }
      }
    }

    int ret = 0;
    for (int j = 0; j <= W; j++) {
      for (int k = 0; k <= X; k++) {
        ret = max(ret, dp[D][j][k]);
      }
    }
    cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int C, D, W, X, E[15][30], F[15][30], d[] = {-1, 1};
  int dp[51][6];
  while (cin >> C >> D >> W >> X, C) {
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < D; j++) {
        cin >> E[i][j];
      }
    }
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < D; j++) {
        cin >> F[i][j];
      }
    }
    fill_n(*dp, 51 * 6, -1);
    dp[0][0] = 0;
    for (int i = 0; i < D; i++) {      // 今日
      for (int j = W; j >= 0; j--) {   // 今の負担
        for (int k = X; k >= 0; k--) { // 2回以上のライブ
          if (dp[j][k] == -1)
            continue;

          // 1回のみライブ
          for (int l = 0; l < C; l++) {
            if (j + F[l][i] > W)
              continue;
            dp[j + F[l][i]][k] = max(dp[j + F[l][i]][k], dp[j][k] + E[l][i]);
          }

          // 2回以上ライブ
          if (k + 1 > X)
            continue;
          for (int l = 0; l < C; l++) { // 始めるところ
            if (E[l][i] == 0)
              continue;
            for (int m = 0; m < 2; m++) { // 左へ行くのと右へ行く
              int pos = l + d[m], cost = E[l][i],
                  attack = F[l][i]; // 場所, 利益, 疲労
              while (pos >= 0 && pos < C && E[pos][i] != 0) {
                cost += E[pos][i];
                attack += F[pos][i];
                if (attack > W)
                  break;
                dp[j + attack][k + 1] =
                    max(dp[j + attack][k + 1], dp[j][k] + cost);
                pos += d[m];
              }
            }
          }
        }
      }
    }
    int ret = 0;
    for (int i = 0; i <= W; i++) {
      for (int j = 0; j <= X; j++) {
        ret = max(ret, dp[i][j]);
      }
    }
    cout << ret << endl;
  }
}