alexjercan/bugnet huggingface dataset:
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

int getnum(const vector<int> &as, int K) {
  int ans = 0;
  for (int l = 0; l <= as.size() - K; ++l) {
    int num = 1;
    for (int i = l; i < l + K; ++i) {
      num *= as[i];
    }
    ans = max(ans, num);
  }
  return ans;
}

int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    if (!N)
      break;
    vector<int> ns(N);
    for (int i = 0; i < N; ++i)
      cin >> ns[i];
    sort(ns.begin(), ns.end());
    int ans = 0;
    for (int i = 0; i < N; ++i) {
      int rest = K - ns[i] + 1;
      auto it = lower_bound(ns.begin() + i + 1, ns.end(), rest);
      ans += ns.end() - it;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

int getnum(const vector<int> &as, int K) {
  int ans = 0;
  for (int l = 0; l <= as.size() - K; ++l) {
    int num = 1;
    for (int i = l; i < l + K; ++i) {
      num *= as[i];
    }
    ans = max(ans, num);
  }
  return ans;
}

int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    vector<int> ns(N);
    for (int i = 0; i < N; ++i)
      cin >> ns[i];
    int ans = 0;
    for (int i = 0; i < N; ++i) {
      for (int j = i + 1; j < N; ++j) {
        if (ns[i] + ns[j] > K)
          ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

using namespace std;

int main() {
  int n, S;
  while (cin >> n >> S, n + S) {
    vi r;
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      r.push_back(m);
    }
    int ans = 0;
    for (int i = 0; i < r.size(); i++) {
      for (int j = 0; j < i; j++) {
        if (S < r[i] + r[j])
          ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

using namespace std;

int main() {
  int n, S;
  while (cin >> n >> S, n + S) {
    vi r;
    for (int i = 0; i < n; i++) {
      int m;
      cin >> m;
      r.push_back(m);
    }
    int ans = 0;
    for (int i = 0; i < r.size(); i++) {
      for (int j = i + 1; j < r.size(); j++) {
        if (S < r[i] + r[j])
          ans++;
      }
    }
    cout << ans << endl;
  }
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, s;
  int r[20000];

  while (cin >> n >> s, n || s) {
    for (int i = 0; i < n; i++) {
      cin >> r[i];
    }

    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (r[i] > s) {
        cnt += n - i - 1;
      } else {
        for (int j = i + 1; j < n; j++) {
          if (r[i] + r[j] > s) {
            cnt++;
          }
        }
      }
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, s;
  int r[20000];

  while (cin >> n >> s, n || s) {
    for (int i = 0; i < n; i++) {
      cin >> r[i];
    }

    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (r[i] + r[j] > s) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p00670 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, S;

  while (cin >> n >> S) {
    if (n + S == 0)
      break;
    vector<int> r(n);
    int ret = 0;
    for (int i = 0; i < n; i++) {
      cin >> r[i];
    }

    if (n == 1) {
      cout << 0 << endl;
      continue;
    }

    sort(r.begin(), r.end());
    for (int i = 0; i < n; i++) {
      if (r[i] > (double)S / 2) {
        ret += n - i - 1;
      } else {
        ret += r.end() - upper_bound(r.begin(), r.end(), S - r[i]);
      }
    }
    cout << ret << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, S;

  while (cin >> n >> S) {
    if (n + S == 0)
      break;
    vector<int> r(n), sum;
    for (int i = 0; i < n; i++) {
      cin >> r[i];
    }

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; ++j) {
        sum.push_back(r[i] + r[j]);
      }
    }
    sort(sum.begin(), sum.end());
    cout << sum.end() - upper_bound(sum.begin(), sum.end(), S) << endl;
  }
}
Problem p00670 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int a, b;
  int c[25000];
  while (scanf("%d%d", &a, &b), a | b) {
    int s = 0;
    for (int d = 0; d < a; d++)
      scanf("%d", &c[d]);
    sort(c, c + a);
    for (int e = 0; e < a - 1; e++) {
      s += c + a - lower_bound(c + e + 1, c + a, b - c[e] + 1);
    }
    printf("%d\\n", s);
  }
}

Error: 0

Fixed submission:

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

int main() {
  int a, b;
  int c[100];
  while (scanf("%d%d", &a, &b), a | b) {
    int s = 0;
    for (int d = 0; d < a; d++)
      scanf("%d", &c[d]);
    sort(c, c + a);
    for (int e = 0; e < a - 1; e++) {
      s += c + a - lower_bound(c + e, c + a, b - c[e]);
    }
    printf("%d\\n", s);
  }
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int r[20001];
int main() {
  int n, s;
  while (scanf("%d%d", &n, &s)) {
    if (!n && !s)
      break;
    for (int i = 0; i < n; i++)
      scanf("%d", &r[i]);
    sort(r, r + n);
    int cnt = 0;
    for (int i = n - 1; i > 0; i--) {
      if (r[i] >= s)
        cnt += i;
      else {
        for (int j = i - 1; j >= 0; j--) {
          if (r[i] + r[j] <= s)
            break;
          cnt++;
        }
      }
    }
    printf("%d\\n", cnt);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
int r[20001];
int main() {
  int n, s;
  while (scanf("%d%d", &n, &s)) {
    if (!n && !s)
      break;
    for (int i = 0; i < n; i++)
      scanf("%d", &r[i]);
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (r[i] + r[j] > s)
          cnt++;
      }
    }
    printf("%d\\n", cnt);
  }
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, s;
  while (cin >> n >> s, n || s) {
    int magic[101] = {0};
    long long sum = 0;
    for (int i = 0, a; i < n; i++) {
      cin >> a;
      magic[a]++;
    }
    for (int i = 1; i < 101; i++) {
      for (int j = i; j < 101; j++) {
        if (i + j > s) {
          if (i != j) {
            sum += magic[i] * magic[j];
          } else {
            sum += magic[i] * (magic[i] - 1) / 2;
          }
        }
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> magic;
  int n, s;
  while (cin >> n >> s, n || s) {
    int sum = 0;
    for (int i = 0, a; i < n; i++) {
      cin >> a;
      magic.push_back(a);
    }
    sort(magic.begin(), magic.end());
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (magic[i] + magic[j] > s) {
          sum += n - j;
          break;
        }
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>

using namespace std;

bool solve() {
  int n, s;
  cin >> n >> s;
  if (n == 0 && s == 0)
    return false;
  vector<int> r(n);
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  sort(r.rbegin(), r.rend());
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int p = r[i] + r[j];
      if (p > s) {
        //				cout<< r[i]<< " "<< r[j]<< endl;
        ans++;
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);
  while (solve())
    ;

  return 0;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>

using namespace std;

int f(int n, int k, vector<int> a) {
  int c1 = 0;
  for (int i = 0; i <= n - k; i++) {
    int num = 1;
    for (int j = 0; j < k; j++) {
      num *= a[i + j];
    }
    c1 = max(c1, num);
  }
  return c1;
}

bool solve() {
  int n, s;
  cin >> n >> s;
  if (n == 0 && s == 0)
    return false;
  vector<int> r(n);
  for (int i = 0; i < n; i++) {
    cin >> r[i];
  }
  sort(r.rbegin(), r.rend());
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int p = r[i] + r[j];
      if (p > s) {
        cout << r[i] << " " << r[j] << endl;
        ans++;
      } else {
        break;
      }
    }
  }
  cout << ans << endl;
  return true;
}

int main() {
  cout.setf(ios::fixed);
  cout.precision(10);
  while (solve())
    ;

  return 0;
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
  while (true) {
    int n, s;
    cin >> n >> s;
    if (n == 0 && s == 0)
      break;
    vector<int> r;
    rep(i, n) {
      int rr;
      cin >> rr;
      r.push_back(rr);
    }
    int res = 0;
    sort(r.begin(), r.end());
    rep(i, n) {
      if (r[i] + r[i] > s)
        --res;
      res += r.end() - upper_bound(r.begin(), r.end(), s - r[i]);
    }
    cout << res / 2 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
  while (true) {
    int n, s;
    cin >> n >> s;
    if (n == 0 && s == 0)
      break;
    vector<int> r;
    rep(i, n) {
      int rr;
      cin >> rr;
      r.push_back(rr);
    }
    int res = 0;
    rep(i, n) {
      vector<int> sum;
      rep(j, i) sum.push_back(r[i] + r[j]);
      sort(sum.begin(), sum.end());
      res += sum.end() - upper_bound(sum.begin(), sum.end(), s - r[i]);
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int n, a[50000], m, sum;
int main() {
  while (true) {
    sum = 0;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (a[i] + a[j] > m) {
          sum += n - j;
          break;
        }
      }
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int n, a[50000], m, sum;
int main() {
  while (true) {
    sum = 0;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (a[i] + a[j] > m) {
          sum++;
        }
      }
    }
    cout << sum << endl;
  }
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, S;
  while (cin >> n >> S) {
    if (n == 0 && S == 0)
      break;
    vector<int> m(n);
    for (int i = 0; i < n; i++) {
      cin >> m[i];
    }

    sort(m.begin(), m.end());

    int cnt = 0;
    for (vector<int>::iterator it = m.begin(); it != m.end(); it++) {
      vector<int>::iterator partner = upper_bound(it + 1, m.end(), S - *it);
      if (partner == m.end())
        continue;
      cnt += m.end() - partner;
    }

    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, S;
  while (cin >> n >> S) {
    if (n == 0 && S == 0)
      break;
    int m[n];
    for (int i = 0; i < n; i++) {
      cin >> m[i];
    }

    int cnt = 0;
    for (int i = 0; i < n - 1; i++) {
      for (int j = i + 1; j < n; j++) {
        if (m[i] + m[j] > S) {
          cnt++;
        }
      }
    }

    cout << cnt << endl;
  }
  return 0;
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int n, s;
  while (cin >> n >> s, n && s) {
    int cnt = 0, r[20000];
    for (int i = 0; i < n; i++) {
      cin >> r[i];
    }
    sort(r, r + n, greater<int>());
    for (int i = 0; i < n - 1; i++) {
      if (r[i] + r[i + 1] <= s) {
        break;
      }
      for (int j = n - 1; j > i; j--) {
        if (r[i] + r[j] > s) {
          cnt += j - i;
          break;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int n, s, cnt = 0;
  while (cin >> n >> s, n && s) {
    int r[20000];
    for (int i = 0; i < n; i++) {
      cin >> r[i];
    }
    sort(r, r + n, greater<int>());
    for (int i = 0; i < n - 1; i++) {
      if (r[i] + r[i + 1] <= s) {
        break;
      }
      for (int j = n - 1; j > i; j--) {
        if (r[i] + r[j] > s) {
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int n, s;
  while (cin >> n >> s, n || s) {
    int r[n];
    rep(i, n) cin >> r[i];
    int sum = 0;
    rep(i, n) {
      loop(j, i + 1, n) {
        if (r[i] + r[j] > s)
          sum++;
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int n, S;
  while (1) {
    cin >> n >> S;
    if (!n && !S)
      break;
    int dt[n];
    int ans = 0;
    bool check[n][n];
    rep(i, n) rep(j, n) check[i][j] = false;
    rep(i, n) cin >> dt[i];
    sort(dt, dt + n);
    for (int i = 0; i < n; i++) {
      int num = S - dt[i];
      int middle = n / 2;
      int start = 0, finish = n - 1;
      if (num < dt[0]) {
        finish = 0;
      } else if (num >= dt[n - 1]) {
        finish = n;
      } else {
        while (abs(start - finish) > 1) {
          if (dt[middle] > num) {
            finish = middle;
          } else {
            start = middle;
          }
          middle = (finish - start) / 2;
        }
      }
      ans += (n - finish);
    }
    cout << ans / 2 << endl;
  }
  return 0;
}
Problem p00671 (C++) — buggy submission, verdict: Runtime Error

// 22
#include <algorithm>
#include <iostream>

using namespace std;

void mrp(int &a, int b) {
  if (a < b) {
    a = b;
  }
}

int main() {
  for (int c, d, w, x; cin >> c >> d >> w >> x, c | d | w | x;) {
    int e[15][30];
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < d; j++) {
        cin >> e[i][j];
      }
    }
    int f[15][30];
    for (int i = 0; i < c; i++) {
      for (int j = 0; j < d; j++) {
        cin >> f[i][j];
      }
    }
    int dp[31][6][51] = {};
    for (int i = 0; i < d; i++) {
      copy(dp[i][0], dp[i + 1][0], dp[i + 1][0]);
      for (int j = 0; j <= x; j++) {
        for (int k = 0; k <= w; k++) {
          for (int l = 0; l < c; l++) {
            int cf = k + f[l][i];
            if (e[l][i] == 0 || cf > w)
              continue;
            int ce = dp[i][j][k] + e[l][i];
            mrp(dp[i + 1][j][cf], ce);
            if (j == x)
              continue;
            for (int d = -1; d <= 1; d += 2) {
              int df = cf;
              int de = ce;
              for (int ll = l + d; 0 <= ll && ll < c; ll += d) {
                int nf = df + f[ll][i];
                if (e[ll][i] == 0 || nf > w)
                  break;
                int ne = de + e[ll][i];
                mrp(dp[i + 1][j + 1][nf], ne);
                de = ne;
                df = nf;
              }
            }
          }
        }
      }
    }
    cout << *max_element(dp[0][0], dp[31][0]) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

// 22
#include <algorithm>
#include <iostream>

using namespace std;

void mrp(int &a, int b) {
  if (a < b) {
    a = b;
  }
}

int main() {
  for (int c, d, w, x; cin >> c >> d >> w >> x, c | d | w | x;) {
    int e[15][30];
    for (int i = 0; i < d; i++) {
      for (int j = 0; j < c; j++) {
        cin >> e[i][j];
      }
    }
    int f[15][30];
    for (int i = 0; i < d; i++) {
      for (int j = 0; j < c; j++) {
        cin >> f[i][j];
      }
    }
    int dp[31][6][51] = {};
    for (int i = 0; i < d; i++) {
      copy(dp[i][0], dp[i + 1][0], dp[i + 1][0]);
      for (int j = 0; j <= x; j++) {
        for (int k = 0; k <= w; k++) {
          for (int l = 0; l < c; l++) {
            int cf = k + f[i][l];
            if (cf > w)
              continue;
            int ce = dp[i][j][k] + e[i][l];
            mrp(dp[i + 1][j][cf], ce);
            if (j == x)
              continue;
            for (int d = -1; d <= 1; d += 2) {
              int df = cf;
              int de = ce;
              for (int ll = l + d; 0 <= ll && ll < c; ll += d) {
                int nf = df + f[i][ll];
                if (nf > w)
                  break;
                int ne = de + e[i][ll];
                mrp(dp[i + 1][j + 1][nf], ne);
                de = ne;
                df = nf;
              }
            }
          }
        }
      }
    }
    cout << *max_element(dp[0][0], dp[31][0]) << endl;
  }
  return 0;
}
Problem p00671 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll C, D, W, X;
ll E[16][31], F[16][31];
ll memo[31][51][6];
const int dx[] = {-1, 1};

ll solve(ll d, ll w, ll x) {
  if (d == D)
    return 0;

  ll &res = memo[d][w][x];
  if (res != -1)
    return res;

  res = 0;
  res = max(res, solve(d + 1, w, x));

  for (int i = 0; i < C; i++) {
    if (w + F[i][d] <= W && E[i][d] != 0) {
      res = max(res, solve(d + 1, w + F[i][d], x) + E[i][d]);
    }
  }

  if (x + 1 <= X) {
    for (int i = 0; i < C; i++) {
      for (int j = 0; j < 2; j++) {
        ll nx = i, cost = 0, f = 0;
        while (1) {
          if (nx < 0 || nx >= C || E[nx][d] == 0)
            break;
          f += F[nx][d];
          cost += E[nx][d];
          if (w + f <= W) {
            res = max(res, solve(d + 1, w + f, x + 1) + cost);
          }
          nx += dx[j];
        }
      }
    }
  }
  return res;
}

int main() {
  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];
      }
    }

    memset(memo, -1, sizeof(memo));

    ll res = solve(0, 0, 0);
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll C, D, W, X;
ll E[16][31], F[16][31];
ll memo[16][31][51][6];
const int dx[] = {-1, 1};

ll solve(ll c, ll d, ll w, ll x) {
  if (d == D)
    return 0;

  ll &res = memo[c][d][w][x];
  if (res != -1)
    return res;

  for (int i = 0; i < C; i++) {
    res = max(res, solve(i, d + 1, w, x));
  }
  if (w + F[c][d] <= W && E[c][d] != 0) {
    for (int i = 0; i < C; i++) {
      res = max(res, solve(i, d + 1, w + F[c][d], x) + E[c][d]);
    }
  }
  if (x + 1 <= X) {
    for (int i = 0; i < 2; i++) {
      ll nx = c, cost = 0, f = 0;
      while (1) {
        if (nx < 0 || nx >= C)
          break;
        f += F[nx][d];
        cost += E[nx][d];
        if (E[nx][d] == 0)
          break;
        if (w + f <= W) {
          for (int j = 0; j < C; j++) {
            res = max(res, solve(j, d + 1, w + f, x + 1) + cost);
          }
        }
        nx += dx[i];
      }
    }
  }
  return res;
}

int main() {
  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];
      }
    }

    memset(memo, -1, sizeof(memo));

    ll res = 0;
    for (int i = 0; i < C; i++) {
      res = max(res, solve(i, 0, 0, 0));
    }
    cout << res << endl;
  }
  return 0;
}