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

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int(i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y)                                                           \\
  cerr << "(" << #x << ", " << #y << ") = "                                    \\
       << "(" << x << ", " << y << ")" << endl;
#define debug3(x, y, z)                                                        \\
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = "                      \\
       << "(" << x << ", " << y << ", " << z << ")" << endl;
#define debugB(x, y) cerr << #x << ": " << bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};

template <typename T> void printA(vector<T> &printArray, char between = ' ') {
  int paSize = printArray.size();
  for (int i = 0; i < paSize; i++) {
    cerr << printArray[i] << between;
  }
  if (between != '\\n') {
    cerr << endl;
  }
}

// ------------------------------------------------------------------------------------------

const int NN = 111, WW = 1111;
const int INF = 1e8;
int N, W;
int w[NN], v[NN];
int dp[NN][NN * WW];

int dfs(int idx, int wSum) {
  if (idx == N) {
    return 0;
  }
  if (wSum + w[idx] > W) {
    return dfs(idx + 1, wSum);
  }
  if (dp[idx][wSum] != -1) {
    return dp[idx][wSum];
  }
  return dp[idx][wSum] =
             max(dfs(idx + 1, wSum + w[idx]) + v[idx], dfs(idx + 1, wSum));
}

int main() {
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }
  rep(i, NN) {
    rep(j, NN * WW) { dp[i][j] = -1; }
  }
  cout << dfs(0, 0) << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int(i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int(i) = (m); (i) < (n); ++(i))
#define debug(x) cerr << #x << ": " << x << endl;
#define debug2(x, y)                                                           \\
  cerr << "(" << #x << ", " << #y << ") = "                                    \\
       << "(" << x << ", " << y << ")" << endl;
#define debug3(x, y, z)                                                        \\
  cerr << "(" << #x << ", " << #y << ", " << #z << ") = "                      \\
       << "(" << x << ", " << y << ", " << z << ")" << endl;
#define debugB(x, y) cerr << #x << ": " << bitset<y>(x) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};

template <typename T> void printA(vector<T> &printArray, char between = ' ') {
  int paSize = printArray.size();
  for (int i = 0; i < paSize; i++) {
    cerr << printArray[i] << between;
  }
  if (between != '\\n') {
    cerr << endl;
  }
}

// ------------------------------------------------------------------------------------------

const int NN = 111, WW = 1111;
const int INF = 1e8;
int N, W;
int w[NN], v[NN];
int dp[NN][NN * WW];

int dfs(int idx, int wSum) {
  if (idx == N) {
    return 0;
  }
  if (wSum + w[idx] > W) {
    return dfs(idx + 1, wSum);
  }
  if (dp[idx][wSum + w[idx]] != INF) {
    return dp[idx][wSum + w[idx]];
  }
  return max(dfs(idx + 1, wSum + w[idx]) + v[idx], dfs(idx + 1, wSum));
}

int main() {
  cin >> N >> W;
  rep(i, N) { cin >> v[i] >> w[i]; }
  rep(i, NN) {
    rep(j, NN * WW) { dp[i][j] = INF; }
  }
  cout << dfs(0, 0) << endl;

  return 0;
}