Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef long double lld;
#define mod 1000000007
#define mod2 998244353
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define sortvec(a) sort(a.begin(), a.end())
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl
#define cerr                                                                   \
  if (true)                                                                    \
  cerr
void print() { cout << endl; }
template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " ";
  print(args...);
}
lli llipowerp(lli x, lli y, lli p = LLONG_MAX) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
lli binarySearch(vector<int> arr, int l, int r, int x) {
  if (r >= l) {
    lli mid = l + (r - l) / 2;
    if (arr[mid] == x)
      return mid;
    if (arr[mid] > x)
      return binarySearch(arr, l, mid - 1, x);
    return binarySearch(arr, mid + 1, r, x);
  }
  return -1;
}
template <typename T> inline void printvec(vector<T> inp) {
  for (auto it : inp)
    cout << it << " ";
  cout << endl;
}
//---------------------------------------------------------------------------------
vector<lli> w, v;

lli n, W;
lli dp[(lli)1e5 + 5][101];
lli fill(lli Wr, lli Nr) {
  if (dp[Wr][Nr] != -1)
    return dp[Wr][Nr];
  if (Nr <= 0)
    return 0;
  lli maxv = 0;
  for (lli i = 1; i <= Nr; i++) {
    if (w[i] <= Wr)
      maxv = max(fill(Wr - w[i], i - 1) + v[i], maxv);
  }
  dp[Wr][Nr] = maxv;
  return dp[Wr][Nr];
}
int main() {

  cin >> n >> W;
  w.resize(n + 1);
  v.resize(n + 1);
  fori(n) { cin >> w[i + 1] >> v[i + 1]; }
  memset(dp, -1, sizeof(dp));
  cout << fill(W, n) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lli;
typedef long double lld;
#define mod 1000000007
#define mod2 998244353
#define fori(n) for (int i = 0; i < n; i++)
#define forj(n) for (int j = 0; j < n; j++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define sortvec(a) sort(a.begin(), a.end())
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl
#define cerr                                                                   \
  if (true)                                                                    \
  cerr
void print() { cout << endl; }
template <typename T, typename... Args> void print(T a, Args... args) {
  cout << a << " ";
  print(args...);
}
lli llipowerp(lli x, lli y, lli p = LLONG_MAX) {
  lli res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
lli binarySearch(vector<int> arr, int l, int r, int x) {
  if (r >= l) {
    lli mid = l + (r - l) / 2;
    if (arr[mid] == x)
      return mid;
    if (arr[mid] > x)
      return binarySearch(arr, l, mid - 1, x);
    return binarySearch(arr, mid + 1, r, x);
  }
  return -1;
}
template <typename T> inline void printvec(vector<T> inp) {
  for (auto it : inp)
    cout << it << " ";
  cout << endl;
}
//---------------------------------------------------------------------------------
vector<lli> w, v;

lli n, W;
lli dp[(lli)1e5 + 5][101];
lli fill(lli Wr, lli Nr) {
  if (dp[Wr][Nr] != -1)
    return dp[Wr][Nr];
  if (Nr <= 0)
    return 0;
  lli maxv = fill(Wr, Nr - 1);
  if (w[Nr] <= Wr) {
    maxv = max(fill(Wr - w[Nr], Nr - 1) + v[Nr], maxv);
  }
  dp[Wr][Nr] = maxv;
  return dp[Wr][Nr];
}
int main() {

  cin >> n >> W;
  w.resize(n + 1);
  v.resize(n + 1);
  fori(n) { cin >> w[i + 1] >> v[i + 1]; }
  memset(dp, -1, sizeof(dp));
  cout << fill(W, n) << endl;
}