Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifndef bhartiya
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

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

typedef long long ll;
typedef pair<ll, ll> pll;
// not that imp
typedef pair<pll, ll> plll;
typedef vector<ll> vl;
typedef vector<pll> vll;

#ifdef bhartiya
#define endl '\n'
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define pb push_back
#define bitc __builtin_popcountl
#define mp make_pair
#define ff first
#define ss second

#define swap(a, b)                                                             \
  {                                                                            \
    a = a ^ b;                                                                 \
    b = a ^ b;                                                                 \
    a = a ^ b;                                                                 \
  }
#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define inf 200000000000000ll
// #define inf 10000000000ll
#define mod 1000000007ll
// #define mod 998244353ll
#define eps 1e-7

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const ll maxn = 100005;
ll dp[105][maxn];
bool vis[105][maxn];
ll v[maxn];
ll we[maxn];

void recur(ll i, ll w) {
  if (i < 0)
    return;
  else if (w == 0)
    return;
  if (vis[i][w])
    return;
  ll a = 0, b = 0;
  recur(i - 1, w);
  b = i > 0 ? dp[i - 1][w] : 0;
  if (we[i] <= w) {
    recur(i - 1, w - we[i]);
    a = i > 0 ? dp[i - 1][w - we[i]] + v[i] : v[i];
  }
  dp[i][w] = max(a, b);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
// cin.exceptions(cin.failbit);
#ifdef bhartiya
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, w;
    cin >> n >> w;
    rep(i, n) { cin >> we[i] >> v[i]; }
    recur(n - 1, w);
    cout << dp[n - 1][w];
  }
}

Fixed submission:
#ifndef bhartiya
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target
// ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif

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

typedef long long ll;
typedef pair<ll, ll> pll;
// not that imp
typedef pair<pll, ll> plll;
typedef vector<ll> vl;
typedef vector<pll> vll;

#ifdef bhartiya
#define endl '\n'
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define pb push_back
#define bitc __builtin_popcountl
#define mp make_pair
#define ff first
#define ss second

#define swap(a, b)                                                             \
  {                                                                            \
    a = a ^ b;                                                                 \
    b = a ^ b;                                                                 \
    a = a ^ b;                                                                 \
  }
#define fr(i, a, b) for (ll i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (ll i = (a), _b = (b); i >= _b; i--)
#define inf 200000000000000ll
// #define inf 10000000000ll
#define mod 1000000007ll
// #define mod 998244353ll
#define eps 1e-7

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const ll maxn = 100005;
ll dp[105][maxn];
bool vis[105][maxn];
ll v[maxn];
ll we[maxn];

void recur(ll i, ll w) {
  if (i < 0)
    return;
  else if (w == 0)
    return;
  if (vis[i][w])
    return;
  vis[i][w] = true;
  ll a = 0, b = 0;
  recur(i - 1, w);
  b = i > 0 ? dp[i - 1][w] : 0;
  if (we[i] <= w) {
    recur(i - 1, w - we[i]);
    a = i > 0 ? dp[i - 1][w - we[i]] + v[i] : v[i];
  }
  dp[i][w] = max(a, b);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.precision(10);
// cin.exceptions(cin.failbit);
#ifdef bhartiya
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, w;
    cin >> n >> w;
    rep(i, n) { cin >> we[i] >> v[i]; }
    recur(n - 1, w);
    cout << dp[n - 1][w];
  }
}