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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int N;

bool solve(vector<int> &H, vector<int> &W) {
  REP(i, N) REP(j, N) if (W[j] > 0) {
    W[j]--;
    H[i]--;
  }
  REP(i, N) if (H[i] != 0) return 0;
  return 1;
}

int main() {
  while (cin >> N && N) {
    vector<int> H(N), W(N);
    REP(i, N) cin >> H[i];
    REP(i, N) cin >> W[i];
    SORT(H);
    REVERSE(H);
    cout << (solve(H, W) ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int N;

bool check(vector<int> &H, vector<int> &W, vector<vector<int>> &v) {
  REP(i, N) {
    int a = 0, b = 0, c = 0;
    REP(j, N) {
      if (v[i][j] == 0)
        ++a;
      if (v[i][j] == 1)
        ++b;
      if (v[i][j] == 2)
        ++c;
    }
    if (c == 0)
      continue;
    if (a + c == H[i]) {
      REP(j, N) if (v[i][j] == 2) v[i][j] = 0;
      return 1;
    }
    if (b + c == N - H[i]) {
      REP(j, N) if (v[i][j] == 2) v[i][j] = 1;
      return 1;
    }
  }
  REP(i, N) {
    int a = 0, b = 0, c = 0;
    REP(j, N) {
      if (v[j][i] == 0)
        ++a;
      if (v[j][i] == 1)
        ++b;
      if (v[j][i] == 2)
        ++c;
    }
    if (c == 0)
      continue;
    if (a + c == W[i]) {
      REP(j, N) if (v[j][i] == 2) v[j][i] = 0;
      return 1;
    }
    if (a + c == N - W[i]) {
      REP(j, N) if (v[j][i] == 2) v[j][i] = 1;
      return 1;
    }
  }
  return 0;
}

int main() {
  while (cin >> N && N) {
    vector<int> H(N), W(N);
    REP(i, N) cin >> H[i];
    REP(i, N) cin >> W[i];
    vector<vector<int>> v(N, vector<int>(N, 2));
    while (check(H, W, v))
      ;
    bool ans = 1;
    REP(i, N) REP(j, N) if (v[i][j] == 2) ans = 0;
    cout << (ans ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01329 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

struct S {
  int y, x, t;
  vector<int> v;
};

int main() {
  for (int N, M; cin >> N >> M, N;) {
    char g[50][51];
    int y, x;
    for (int i = 0; i < N; i++) {
      cin >> g[i];
      for (int j = 0; j < M; j++) {
        if (g[i][j] == 'S') {
          y = i;
          x = j;
        }
      }
    }
    int P;
    cin >> P;
    string p[10];
    for (int i = 0; i < P; i++) {
      cin >> p[i];
    }
    set<vector<int>> s[50][50];
    queue<S> que;
    que.push({y, x, 0, vector<int>(P)});
    while (!que.empty()) {
      auto cs = que.front();
      if (g[cs.y][cs.x] == 'G')
        break;
      que.pop();
      if (!s[cs.y][cs.x].insert(cs.v).second)
        continue;
      for (int i = 0; i < 4; i++) {
        static const int dy[] = {-1, 0, 1, 0};
        static const int dx[] = {0, 1, 0, -1};
        int ny = cs.y + dy[i];
        int nx = cs.x + dx[i];
        if (0 <= ny && ny < N && 0 <= nx && nx < M && g[ny][nx] != '#') {
          S ns{ny, nx, cs.t + 1, cs.v};
          for (int j = 0; j < P; j++) {
            string c = p[j].substr(0, cs.v[j]) + "URDL"[i];
            while (p[j].compare(0, c.size(), c)) {
              c.erase(c.begin());
            }
            if (c == p[j])
              goto fail;
            ns.v[j] = c.size();
          }
          que.push(ns);
        fail:;
        }
      }
    }
    cout << (que.empty() ? -1 : que.front().t) << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct S {
  int t;
  int y, x;
  int h;
};

int main() {
  for (int N, M; cin >> N >> M, N;) {
    char g[50][51];
    int sy, sx;
    for (int i = 0; i < N; i++) {
      cin >> g[i];
      for (int j = 0; j < M; j++) {
        if (g[i][j] == 'S') {
          sy = i;
          sx = j;
        }
      }
    }
    int P;
    cin >> P;
    string pt[10];
    int pb[10] = {};
    for (int i = 0; i < P; i++) {
      cin >> pt[i];
      for (auto e : pt[i]) {
        pb[i] = pb[i] << 2 | e % 23 % 4;
      }
    }
    queue<S> que;
    que.push({0, sy, sx, 0});
    set<int> p[50][50];
    while (!que.empty()) {
      S cs = que.front();
      if (g[cs.y][cs.x] == 'G')
        break;
      que.pop();
      if (cs.t >= 9 && !p[cs.y][cs.x].insert(cs.h).second)
        continue;
      for (int i = 0; i < 4; i++) {
        int ny = cs.y + (~i & i - 1);
        int nx = cs.x + (i ? 2 - i : 0);
        if (0 <= ny && ny < N && 0 <= nx && nx < M && g[ny][nx] != '#') {
          int nh = cs.h << 2 | i;
          for (int j = 0; j < P; j++) {
            if (cs.t + 1 >= pt[j].size() &&
                (nh & (1 << pt[j].size() * 2) - 1) == pb[j]) {
              goto fail;
            }
          }
          que.push({cs.t + 1, ny, nx, nh & (1 << 18) - 1});
        }
      fail:;
      }
    }
    cout << (que.empty() ? -1 : que.front().t) << endl;
  }
}
Problem p01334 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#ifdef Debug
#define dump(x) cerr << #x << " = " << (x) << endl
#else
#define dump(x)
#endif

int par[10000];
int Rank[10000];

void init(int n) {
  rep(i, n) {
    par[i] = i;
    Rank[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    pii data[110][110] = {};
    rep(i, n) rep(j, n) cin >> data[i][j].first >> data[i][j].second;

    init(n * n);

    rep(i, n) {
      rep(j, n) { unite(n * i + j, n * data[i][j].second + data[i][j].first); }
    }
    bool used[10000] = {};
    int sum = 0;
    rep(i, n * n) {
      int aa = find(i);
      if (used[aa] == false) {
        sum++;
        used[aa] = true;
      }
    }
    cout << sum << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#ifdef Debug
#define dump(x) cerr << #x << " = " << (x) << endl
#else
#define dump(x)
#endif

int par[100];
int Rank[100];

void init(int n) {
  rep(i, n) {
    par[i] = i;
    Rank[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    pii data[110][110] = {};
    rep(i, n) rep(j, n) cin >> data[i][j].first >> data[i][j].second;

    init(n * n);

    rep(i, n) {
      rep(j, n) { unite(n * i + j, n * data[i][j].second + data[i][j].first); }
    }
    bool used[10000] = {};
    int sum = 0;
    rep(i, n * n) {
      int aa = find(i);
      if (used[aa] == false) {
        sum++;
        used[aa] = true;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01334 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
int x[10000], a, b, n, c[10000], d[10000];
int main() {
  while (true) {
    for (int i = 0; i < 10000; i++) {
      d[i] = 0;
    }
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n * n; i++) {
      cin >> a >> b;
      x[i] = b * n + a;
    }
    int cnt = 0;
    for (int i = 0; i < n * n; i++) {
      if (d[i] == 0)
        for (int j = 0; j < 10000; j++) {
          c[j] = 0;
        }
      int cx = i;
      while (true) {
        c[cx] = 1;
        cx = x[cx];
        if (c[cx] == 1) {
          break;
        }
      }
      int C = 0;
      for (int j = 0; j < 10000; j++) {
        if (c[j] == 1 && d[j] >= 1) {
          C = d[j];
        }
      }
      if (C == 0) {
        cnt++;
        C = cnt;
      }
      for (int j = 0; j < 10000; j++) {
        if (c[j] == 1) {
          d[j] = C;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int x[100][100], y[100][100], z[100][100];
int main() {
  int n;
  while (true) {
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < 100; j++) {
        x[i][j] = 0;
        y[i][j] = 0;
        z[i][j] = 0;
      }
    }
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> x[i][j] >> y[i][j];
      }
    }
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (z[i][j] == 0) {
          cnt++;
          int C = cnt;
          int cx = j, cy = i;
          z[cy][cx] = C;
          while (true) {
            int cx1 = x[cy][cx], cy1 = y[cy][cx];
            cx = cx1;
            cy = cy1;
            if (z[cy][cx] >= 1) {
              C = z[cy][cx];
              break;
            }
            z[cy][cx] = C;
          }
          cx = i, cy = j;
          z[cy][cx] = C;
          if (C != cnt) {
            cnt--;
          }
          while (true) {
            int cx1 = x[cy][cx], cy1 = y[cy][cx];
            cx = cx1;
            cy = cy1;
            if (z[cy][cx] >= 1) {
              C = z[cy][cx];
              break;
            }
            x[cy][cx] = C;
          }
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p01334 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int par[20000];
int Rank[20000];

void init(int n) {
  rep(i, n) {
    par[i] = i;
    Rank[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    pii data[200][200];
    rep(i, n) {
      rep(j, n) {
        int a, b;
        cin >> a >> b;
        data[i][j] = make_pair(a, b);
      }
    }

    init(20000);

    rep(i, n) {
      rep(j, n) { unite(n * i + j, n * data[i][j].second + data[i][j].first); }
    }
    bool used[20000] = {};
    int c = 0;
    rep(i, n) rep(j, n) used[find(n * i + j)] = true;
    rep(i, 20000) if (used[i]) c++;
    cout << c << endl;
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int par[200];
int Rank[200];

void init(int n) {
  rep(i, n) {
    par[i] = i;
    Rank[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    pii data[200][200];
    rep(i, n) {
      rep(j, n) {
        int a, b;
        cin >> a >> b;
        data[i][j] = make_pair(a, b);
      }
    }

    init(200);

    rep(i, n) {
      rep(j, n) { unite(n * i + j, n * data[i][j].second + data[i][j].first); }
    }
    bool used[200] = {};
    int c = 0;
    rep(i, n) rep(j, n) used[find(n * i + j)] = true;
    rep(i, 200) if (used[i]) c++;
    cout << c << endl;
  }
}
Problem p01336 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
/* 変数名を被らせない
   デバッグコードを取り除く */
#define ANSWER(x) cout << x << endl
#define debug(x) cout << #x << ": " << x
const int MAX_INT = 1000000;
string name;
int C[301], V[301], D[301], L[301];
int dp[301][301][3];
int N, M;
int solve(int a, int b, int s);
int main() {
  while (cin >> N >> M) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < N; i++) {
      cin.ignore();
      getline(cin, name);
      cin >> C[i] >> V[i] >> D[i] >> L[i];
    }
    cout << max(solve(0, M, 0), max(solve(0, M, 1), solve(0, M, 2))) << endl;
  }
}
int solve(int a, int b, int s) {
  if (dp[a][b][s] != -1)
    return dp[a][b][s];
  else {
    if (a == N)
      return 0;
    else if (b < C[a])
      return dp[a][b][s] = solve(a + 1, b, s);
    else {
      if (s == 0)
        return dp[a][b][s] =
                   max(solve(a + 1, b - C[a], s) + V[a],
                       max(solve(a, b - C[a], s) + V[a], solve(a + 1, b, s)));
      else if (s == 1)
        return dp[a][b][s] =
                   max(solve(a + 1, b - C[a], s) + D[a],
                       max(solve(a, b - C[a], s) + D[a], solve(a + 1, b, s)));
      else
        return dp[a][b][s] =
                   max(solve(a + 1, b - C[a], s) + L[a],
                       max(solve(a, b - C[a], s) + L[a], solve(a + 1, b, s)));
    }
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/* 変数名を被らせない
   デバッグコードを取り除く */
#define ANSWER(x) cout << x << endl
#define debug(x) cout << #x << ": " << x
const int MAX_INT = 1000000;
string name[1000000];
int C[100000], V[100000], D[100000], L[100000];
int dp[100000][301][3];
int N, M;
int solve(int a, int b, int s);
int main() {
  while (cin >> N >> M) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < N; i++) {
      cin.ignore();
      getline(cin, name[i]);
      cin >> C[i] >> V[i] >> D[i] >> L[i];
    }
    cout << max(solve(0, M, 0), max(solve(0, M, 1), solve(0, M, 2))) << endl;
  }
}
int solve(int a, int b, int s) {
  if (dp[a][b][s] != -1)
    return dp[a][b][s];
  else {
    if (a == N)
      return 0;
    else if (b < C[a])
      return dp[a][b][s] = solve(a + 1, b, s);
    else {
      if (s == 0)
        return dp[a][b][s] =
                   max(solve(a + 1, b - C[a], s) + V[a],
                       max(solve(a, b - C[a], s) + V[a], solve(a + 1, b, s)));
      else if (s == 1)
        return dp[a][b][s] =
                   max(solve(a + 1, b - C[a], s) + D[a],
                       max(solve(a, b - C[a], s) + D[a], solve(a + 1, b, s)));
      else
        return dp[a][b][s] =
                   max(solve(a + 1, b - C[a], s) + L[a],
                       max(solve(a, b - C[a], s) + L[a], solve(a + 1, b, s)));
    }
  }
}
Problem p01336 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
int main() {
  int N, M;
  int c[300], v[300], d[300], l[300];
  int V[301], D[301], L[301];
  while (cin >> N >> M) {
    if (cin.eof())
      break;
    for (int i = 0; i < N; i++) {
      string temp;
      getline(cin, temp);
      // cout<<temp;
      getline(cin, temp);
      // cout<<temp;
      cin >> c[i] >> v[i] >> d[i] >> l[i];
    }
    for (int i = 0; i < M + 1; i++)
      V[i] = D[i] = L[i] = 0;
    for (int i = 0; i < N; i++) {
      for (int j = c[i]; j < M + 1; j++) {
        V[j] = max(V[j], V[j - c[i]] + v[i]);
        D[j] = max(D[j], D[j - c[i]] + d[i]);
        L[j] = max(L[j], L[j - c[i]] + l[i]);
      }
    }
    cout << max(V[M], max(D[M], L[M])) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <set>
using namespace std;
typedef pair<int, int> P;
typedef pair<double, int> PD;
#define EPS (1e-10)

P map[20];
bool done[20];
int donenum;
int N;
int dx, dy;
set<PD> distlist;

void makedist() {
  distlist.clear();
  for (int i = 0; i < N; i++) {
    double x = dx - map[i].first;
    double y = dy - map[i].second;
    distlist.insert(PD(sqrt(x * x + y * y), i));
    // cout<<x*x+y*y<<endl;
  }
}

bool dead(double time) {
  set<PD>::iterator itr = distlist.begin();
  while (itr != distlist.end()) {
    if (done[itr->second]) {
      itr++;
      continue;
    }
    if (time < itr->first)
      return false;
    return true;
  }
}

bool rec(int n, int x, int y, double t) {
  // if(donenum==N)return true;
  int nx = map[n].first;
  int ny = map[n].second;
  double dist = sqrt((nx - x) * (nx - x) + (ny - y) * (ny - y));
  // cout<<dist<<endl;
  if (dead(t + dist))
    return false;
  done[n] = true;
  ++donenum;
  if (donenum == N)
    return true;
  // cout<<n<<endl;
  for (int i = 0; i < N; i++) {
    if (done[i])
      continue;
    if (rec(i, nx, ny, t + dist))
      return true;
  }
  done[n] = false;
  --donenum;
  return false;
}

int main() {
  int hx, hy;
  while (cin >> N >> hx >> hy >> dx >> dy, N || hx || hy || dx || dy) {
    if (N == 0) {
      cout << "YES" << endl;
    }
    for (int i = 0; i < N; i++) {
      done[i] = false;
    }
    donenum = 0;
    for (int i = 0; i < N; i++) {
      int a, b;
      cin >> a >> b;
      map[i] = P(a, b);
    }
    makedist();
    bool ok = false;
    for (int i = 0; i < N; i++) {
      if (rec(i, hx, hy, 0)) {
        ok = true;
        break;
      }
    }
    if (ok)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}
Problem p01336 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define ll long long
using namespace std;
int n, m; // 　バイドルの数、費用
string str;
int c[333], v[333], d[333], l[333];
ll dp[333][333];
int solve(int i, ll sum, int *data);
int main() {

  while (cin >> n >> m) {
    for (int i = 0; i < n; i++) {
      getchar(); // 最初のごみ取り
      getline(cin, str);
      cin >> c[i] >> v[i] >> d[i] >> l[i];
    }

    memset(dp, -1, sizeof(dp));
    int A = solve(0, 0, &v[0]);
    memset(dp, -1, sizeof(dp));
    int B = solve(0, 0, &d[0]);
    memset(dp, -1, sizeof(dp));
    int C = solve(0, 0, &l[0]);
    memset(dp, -1, sizeof(dp));
    cout << max(max(A, B), C) << endl;
  }
}
int solve(int i, ll sum, int *data) {
  int ret = 0;
  if (i == n)
    return 0;

  if (dp[i][sum] != -1)
    return dp[i][sum];

  for (int j = 0; j * c[i] <= (m - sum); j++) {
    ret = max(ret, solve(i + 1, sum + (j * c[i]), data) + (data[i] * j));
  }

  return dp[i][sum] = ret;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, m; // 　バイドルの数、費用
string str;
int c[333], v[333], d[333], l[333];
int dp[333][333];
int solve(int i, int sum, int *data);
int main() {

  while (cin >> n >> m) {
    for (int i = 0; i < n; i++) {
      getchar(); // 最初のごみ取り
      getline(cin, str);
      cin >> c[i] >> v[i] >> d[i] >> l[i];
    }
    memset(dp, 0, sizeof(dp));
    int A = solve(0, 0, &v[0]);
    int B = solve(0, 0, &d[0]);
    int C = solve(0, 0, &l[0]);
    cout << max(max(A, B), C) << endl;
  }
}
int solve(int i, int sum, int *data) {
  int ret = 0;
  if (i == n) {
    ret = 0;
  } else if (sum + c[i] > m) {
    solve(i + 1, sum, data);
  } else {
    for (int j = 0; j * c[i] <= (m - sum); j++) {
      ret = max(ret, solve(i + 1, sum + (j * c[i]), data) + (data[i] * j));
    }
  }
  return ret;
}
Problem p01336 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int a[300], b[300], c[300], d[300], e[301][301], n, m;
int fi(int p, int q) {
  int i;
  if (e[p][q] != -1)
    return e[p][q];
  int mx = 0;
  for (i = 0; a[p] * i <= q; ++i)
    mx = max(mx, fi(p + 1, q - a[p] * i) + b[p] * i);
  return e[p][q] = mx;
}
int fj(int p, int q) {
  int i;
  if (e[p][q] != -1)
    return e[p][q];
  int mx = 0;
  for (i = 0; a[p] * i <= q; ++i)
    mx = max(mx, fj(p + 1, q - a[p] * i) + c[p] * i);
  return e[p][q] = mx;
}
int fk(int p, int q) {
  int i;
  if (e[p][q] != -1)
    return e[p][q];
  int mx = 0;
  for (i = 0; a[p] * i <= q; ++i)
    mx = max(mx, fk(p + 1, q - a[p] * i) + d[p] * i);
  return e[p][q] = mx;
}
int main() {
  int i;
  // int n,m;
  while (cin >> n >> m) {
    // int a[300],b[300],c[300],d[300];
    string s;
    for (i = 0; i < n; ++i) {
      getline(cin, s);
      getline(cin, s);
      cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    int mx = 0;
    memset(e, -1, sizeof(e));
    memset(e[n], 0, 301 * sizeof(int));
    mx = max(mx, fi(0, m));
    memset(e, -1, sizeof(e));
    memset(e[n], 0, 301 * sizeof(int));
    mx = max(mx, fj(0, m));
    memset(e, -1, sizeof(e));
    memset(e[n], 0, 301 * sizeof(int));
    mx = max(mx, fk(0, m));
    cout << mx << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, m;
pair<pair<int, int>, pair<int, int>> a[300];
int fn(int p, int q, int r, int s, int t) {
  int i;
  int mx = 0;
  if (p == n || a[p].first.first > t) {
    mx = max(max(q, r), s);
  } else {
    for (i = 0; i <= t / a[p].first.first; i++)
      mx = max(mx,
               fn(p + 1, q + a[p].first.second * i, r + a[p].second.first * i,
                  s + a[p].second.second * i, t - a[p].first.first * i));
  }
  return mx;
}
int main() {
  int i;
  while (cin >> n >> m) {
    for (i = 0; i < n; i++) {
      string t;
      int p, q, r, s;
      getline(cin, t);
      getline(cin, t);
      cin >> p >> q >> r >> s;
      a[i] = make_pair(make_pair(p, q), make_pair(r, s));
    }
    sort(a, a + n);
    cout << fn(0, 0, 0, 0, m) << endl;
  }
  return 0;
}