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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
  int c, d, w, x;
  while (cin >> c >> d >> w >> x, c || d || w || x) {
    int e[20][35], f[20][35];
    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];
    int dp[35][55][10];
    memset(dp, -1, sizeof(dp));
    dp[0][0][0] = 0;
    for (int i = 0; i < d; i++) {
      for (int j = 0; j <= w; j++)
        for (int k = 0; k <= x; k++) {
          if (dp[i][j][k] == -1)
            continue;
          for (int l = 0; l < c; l++) {
            int now = l, nx = k, nw = j, nowe = dp[i][j][k];
            while (now < c) {
              nowe += e[now][i], nw += f[now][i];
              if (!e[now][i] || x < nx || w < nw)
                break;
              dp[i + 1][nw][nx] = max(dp[i + 1][nw][nx], nowe);

              now++;
              nx = k + 1;
            }
          }
          dp[i + 1][j][k] = max(dp[i][j][k], dp[i + 1][j][k]);
        }
    }
    int ans = 0;
    for (int i = 0; i <= w; i++)
      for (int j = 0; j <= x; j++)
        ans = max(ans, dp[d][i][j]);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define C 20
#define D 40
#define W 60
#define X 10
using namespace std;
int c, d, w, x;
int ans;
int e[C][D];
int f[C][D];
int dp /*[C]*/[D][W][X];
// dp[úÉ¿][æJ][cè]=v
void func(int nowday, int nowf, int nowe, int nowleft) {
  if (nowday == d) {
    ans = max(nowe, ans);
    return;
  }
  /*if(dp/*[i][nowday+1][nowf][nowleft]<nowe){
          //dp/*[i][nowday+1][nowf][nowleft]=nowe;
          */
  func(nowday + 1, nowf, nowe, nowleft);
  //}
  for (int i = 0; i < c; i++) {
    if (nowf - f[i][nowday] < 0 || !e[i][nowday])
      continue;
    // cout<<"------"<<endl;
    // cout<<dp/*[i]*/[nowday][nowf-f[i][nowday]][nowleft]<<"
    // "<<nowe+e[i][nowday]<<endl;
    if (dp /*[i]*/[nowday][nowf - f[i][nowday]][nowleft] <
        nowe + e[i][nowday]) {
      func(nowday + 1, nowf - f[i][nowday], nowe + e[i][nowday], nowleft);
      dp /*[i]*/[nowday][nowf - f[i][nowday]][nowleft] = nowe + e[i][nowday];
    }
    if (nowleft) {
      int pnf = nowf - f[i][nowday], pne = nowe + e[i][nowday];
      for (int j = i + 1; j < c; j++) {
        pnf -= f[j][nowday];
        pne += e[j][nowday];
        if (pnf < 0 || !e[j][nowday])
          break;
        if (dp /*[j]*/[nowday][pnf][nowleft - 1] < pne) {
          func(nowday + 1, pnf, pne, nowleft - 1);
          dp /*[j]*/[nowday][pnf][nowleft - 1] = pne;
        }
      }
      /*pnf=nowf-f[i][nowday],pne=nowe+e[i][nowday];
      for(int j=i-1;j>=0;j--){
              pnf-=f[j][nowday];
              pne+=e[j][nowday];
              if(pnf<0||!e[j][nowday])
                      break;
              if(dp/*[j][nowday][pnf][nowleft-1]<pne){

                      func(nowday+1,pnf,pne,nowleft-1);
                      dp/*[j][nowday][pnf][nowleft-1]=pne;
              }
      }*/
    }
    /*if(dp/*[i][nowday+1][nowf-f[i][nowday]][nowleft]>nowe+e[i][nowday]){
            dp/*[i][nowday+1][nowf-f[i][nowday]][nowleft]=nowe+e[i][nowday];
            func(nowday+1,nowf-f[i][nowday],nowe+e[i][nowday],nowleft);
    }*/
  }
}
int main() {
  /*
  c:næ
  d:úÔ
  w:æJÌÀE
  x:2ñÈãÅ«éú
  */
  /*
  e[næ][úÉ¿]:v
  f[næ][úÉ¿]:æJ
  */
  while (cin >> c >> d >> w >> x, c || d || w || x) {
    ans = 0;
    // for(int i=0;i<c;i++){
    for (int j = 0; j < d; j++)
      for (int k = 0; k < w; k++)
        for (int l = 0; l < x; l++)
          dp /*[i]*/[j][k][l] = 0;
    //}
    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];
      }
    }
    func(0, w, 0, x);
    cout << ans << endl;
  }
  return 0;
}