Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_temp>
using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp>>;

const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const ll MOD = (ll)(1e9 + 7);
const double PI = acos(-1.0);
const int limit = 200010;

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbg2(x_)                                                               \
  for (auto a_ : x_)                                                           \
    cerr << a_ << " ";                                                         \
  cerr << endl;
#define dbg3(x_, sx_)                                                          \
  rep(i, sx_) cerr << x_[i] << " ";                                            \
  cerr << endl;
vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, W;
  cin >> n >> W;
  ll w[n], v[n];
  rep(i, n) cin >> w[i] >> v[i];
  map<ll, map<ll, ll>> mp;
  set<ll> for_search; // 重さ
  for_search.insert(W);

  rep(i, n) {
    set<ll> temp;
    for (auto wi : for_search) {
      if (wi - w[i] >= 0) {
        mp[i + 1][wi - w[i]] = max(mp[i][wi - w[i]], mp[i][wi] + v[i]);
        temp.insert(wi - w[i]);
      }
      mp[i + 1][wi] = max(mp[i + 1][wi], mp[i][wi]);
    }
    for (auto a : temp)
      for_search.insert(a);
  }
  ll ans = 0;
  for (ll i = 0; i <= W; ++i)
    ans = max(ans, mp[n][i]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #include <boost/multiprecision/cpp_int.hpp>
// using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <typename Q_temp>
using smaller_queue = priority_queue<Q_temp, vector<Q_temp>, greater<Q_temp>>;

const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const ll MOD = (ll)(1e9 + 7);
const double PI = acos(-1.0);
const int limit = 200010;

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbg2(x_)                                                               \
  for (auto a_ : x_)                                                           \
    cerr << a_ << " ";                                                         \
  cerr << endl;
#define dbg3(x_, sx_)                                                          \
  rep(i, sx_) cerr << x_[i] << " ";                                            \
  cerr << endl;
vector<int> Dx = {0, 0, -1, 1, -1, 1, -1, 1, 0};
vector<int> Dy = {1, -1, 0, 0, -1, -1, 1, 1, 0};

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, W;
  cin >> n >> W;
  ll w[n], v[n];
  rep(i, n) cin >> w[i] >> v[i];
  ll mp[n + 1][W + 1] = {};
  set<ll> for_search; // 重さ
  for_search.insert(W);

  rep(i, n) {
    set<ll> temp;
    for (auto wi : for_search) {
      if (wi - w[i] >= 0) {
        mp[i + 1][wi - w[i]] = max(mp[i][wi - w[i]], mp[i][wi] + v[i]);
        temp.insert(wi - w[i]);
      }
      mp[i + 1][wi] = max(mp[i + 1][wi], mp[i][wi]);
    }
    for (auto a : temp)
      for_search.insert(a);
  }
  ll ans = 0;
  for (ll i = 0; i <= W; ++i)
    ans = max(ans, mp[n][i]);
  cout << ans << endl;
  return 0;
}