Problem p00671 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

// i??\\???????????´???j??§2?????\\????????????????????°k??????????£????????????§???
ll dp[35][55][10];
ll E[20][35], F[20][35];

int main() {
  ll c, d, w, x;
  while (cin >> c >> d >> w >> x) {
    if (c == 0 && d == 0 && w == 0 && x == 0)
      break;

    memset(E, 0, sizeof(E));
    rep(i, c) { rep(j, d) cin >> E[i][j]; }

    memset(F, 0, sizeof(F));
    rep(i, c) { rep(j, d) cin >> F[i][j]; }

    // rep(i, c) {
    // 	rep(j, d) {
    // 		cout << "(" << E[i][j] << ", " << F[i][j] << ") ";
    // 	}
    // 	cout << endl;
    // }

    memset(dp, 0, sizeof(dp));

    rep(i, d) {
      rep(j, w + 1) {
        rep(k, x + 1) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);

          rep(s, c) {
            if (E[s][i] == 0)
              continue;

            ll sum = E[s][i];
            ll res = F[s][i];

            if (j + res <= w) {
              dp[i + 1][j + res][k] =
                  max(dp[i + 1][j + res][k], dp[i][j][k] + sum);
            }

            REP(t, s + 1, c) {
              if (E[t][i] == 0)
                break;

              sum += E[t][i];
              res += F[t][i];

              if (j + res <= w && k + 1 <= x) {
                dp[i + 1][j + res][k + 1] =
                    max(dp[i + 1][j + res][k + 1], dp[i][j][k] + sum);
              }
            }
          }
        }
      }
    }

    // rep(i, d + 1) {
    // 	cout << " ----- day : " << i << endl;
    // 	rep(j, w + 1) {
    // 		rep(k, x + 1) {
    // 			cout << setw(3) << dp[i][j][k] << " ";
    // 		}
    // 		cout << endl;
    // 	}
    // }

    ll ans = 0;
    // rep(i, d + 1) {
    rep(j, w + 1) {
      rep(k, x + 1) { ans = max(ans, dp[d][j][k]); }
    }
    // }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

// i??\\?????????j????????´???k??§2?????\\????????????????????°l??????????£????????????§???
ll dp[35][20][305][15];
int E[20][35], F[20][35];

int main() {
  int c, d, w, x;
  while (cin >> c >> d >> w >> x) {
    if (c == 0 && d == 0 && w == 0 && x == 0)
      break;

    memset(E, 0, sizeof(E));
    rep(i, c) { rep(j, d) cin >> E[i][j]; }

    memset(F, 0, sizeof(F));
    rep(i, c) { rep(j, d) cin >> F[i][j]; }

    // rep(i, c) {
    // 	rep(j, d) {
    // 		cout << "(" << E[i][j] << ", " << F[i][j] << ") ";
    // 	}
    // 	cout << endl;
    // }

    memset(dp, 0, sizeof(dp));

    rep(i, d) {
      rep(j, c) {
        rep(k, w + 1) {
          rep(l, x + 1) {
            // ??¶???
            // i??\\?????????j??????????????´??????k??§2?????\\???????????????????????\\??°???l
            rep(s, c) {
              ll sum = E[s][i];
              ll res = F[s][i];

              if (k + res <= w) {
                dp[i + 1][s][k + res][l] =
                    max(dp[i + 1][s][k + res][l], dp[i][j][k][l] + sum);
              }

              REP(t, s + 1, c) {
                sum += E[t][i];
                res += F[t][i];

                if (k + res <= w && l + 1 <= x) {
                  dp[i + 1][t][k + res][l + 1] =
                      max(dp[i + 1][t][k + res][l + 1], dp[i][j][k][l] + sum);
                }
              }
            }
          }
        }
      }
    }

    // rep(i, d + 1) {
    // 	cout << "--------- day : " << i << endl;
    // 	rep(j, c) {
    // 		rep(k, w + 5) {
    // 			cout << setw(3) << dp[i][j][k] << " ";
    // 		}
    // 		cout << endl;
    // 	}
    // }
    //
    ll ans = 0;
    rep(i, d + 1) {
      rep(j, c) {
        rep(k, w + 1) {
          rep(l, 15) { ans = max(ans, dp[i][j][k][l]); }
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}