Problem p03164 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long int
#define fi first
#define se second
#define pub push_back
#define mkp make_pair
#define pi pair<int, int>
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define repd(i, l, r) for (int i = (int)(l); i >= (int)(r); i--)
int power(int x, unsigned int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
    }
    y = y >> 1;
    x = x * x;
  }
  return res;
}
int powermod(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
#define print2d(mat, n, m)                                                     \
  {                                                                            \
    for (int i = 0; i < (int)(n); i++) {                                       \
      for (int j = 0; j < (m); j++) {                                          \
        cout << mat[i][j] << " ";                                              \
      }                                                                        \
      cout << endl;                                                            \
    }                                                                          \
  }
#define print1d(mat, n)                                                        \
  {                                                                            \
    for (int i = 0; i < (int)(n); i++)                                         \
      cout << mat[i] << " ";                                                   \
    cout << endl;                                                              \
  }
#define clr(a, x) memset(a, x, sizeof(a))
#define rr(v) for (auto &val : v)
#define printpair(v)                                                           \
  for (auto val : v) {                                                         \
    cout << val.fi << ":" << val.se << ", ";                                   \
  }                                                                            \
  cout << endl;
#define print(v)                                                               \
  for (const auto itr : v) {                                                   \
    cout << itr << ' ';                                                        \
  }                                                                            \
  cout << endl;
#define init(v, x)                                                             \
  for (auto &itr : v) {                                                        \
    itr = x;                                                                   \
  }
#define ln length()
const int mod = 998244353;
#define sz size()
const int inf = 1e18;
int n, w;
int warr[105], varr[105];
int tv;
int dp[105][100005];
int f(int i, int currv) {
  if (currv >= tv) {
    return 0;
  }
  if (i == n) {
    return inf;
  }
  if (dp[i][currv] != -1) {
    return dp[i][currv];
  }
  int lol;
  int ans = inf;
  lol = f(i + 1, currv + varr[i]);
  if (lol != inf) {
    ans = min(ans, lol + warr[i]);
  }
  lol = f(i + 1, currv);
  if (lol != inf) {
    ans = min(ans, lol);
  }
  if (ans != inf) {
    dp[i][currv] = ans;
  }
  return ans;
}
void solve() {
  cin >> n >> w;
  int totv = 0;
  rep(i, 0, n) {
    cin >> warr[i] >> varr[i];
    totv += varr[i];
  }

  int hi, lo, mid;
  hi = totv;
  lo = 0;
  while (lo <= hi) {
    mid = (hi + lo) / 2;
    tv = mid;
    clr(dp, -1);
    if (f(0, 0) <= w) {
      lo = mid + 1;

    } else {

      hi = mid - 1;
    }
  }
  cout << lo - 1 << endl;
}

int32_t main() {
  fastIO
#ifndef ONLINE_JUDGE
      freopen(R"(C:\Users\swast\CLionProjects\untitled\input.txt)", "r", stdin);
#endif
  int t = 1;
  //    cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long int
#define fi first
#define se second
#define pub push_back
#define mkp make_pair
#define pi pair<int, int>
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define repd(i, l, r) for (int i = (int)(l); i >= (int)(r); i--)
int power(int x, unsigned int y) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
    }
    y = y >> 1;
    x = x * x;
  }
  return res;
}
int powermod(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
#define print2d(mat, n, m)                                                     \
  {                                                                            \
    for (int i = 0; i < (int)(n); i++) {                                       \
      for (int j = 0; j < (m); j++) {                                          \
        cout << mat[i][j] << " ";                                              \
      }                                                                        \
      cout << endl;                                                            \
    }                                                                          \
  }
#define print1d(mat, n)                                                        \
  {                                                                            \
    for (int i = 0; i < (int)(n); i++)                                         \
      cout << mat[i] << " ";                                                   \
    cout << endl;                                                              \
  }
#define clr(a, x) memset(a, x, sizeof(a))
#define rr(v) for (auto &val : v)
#define printpair(v)                                                           \
  for (auto val : v) {                                                         \
    cout << val.fi << ":" << val.se << ", ";                                   \
  }                                                                            \
  cout << endl;
#define print(v)                                                               \
  for (const auto itr : v) {                                                   \
    cout << itr << ' ';                                                        \
  }                                                                            \
  cout << endl;
#define init(v, x)                                                             \
  for (auto &itr : v) {                                                        \
    itr = x;                                                                   \
  }
#define ln length()
const int mod = 998244353;
#define sz size()
const int inf = 1e18;
int n, w;
int warr[105], varr[105];
int tv;
int dp[105][100005];
int f(int i, int currv) {
  if (currv >= tv) {
    return 0;
  }
  if (i == n) {
    return inf;
  }
  if (dp[i][currv] != -1) {
    return dp[i][currv];
  }
  int lol;
  int ans = inf;
  lol = f(i + 1, currv + varr[i]);
  if (lol != inf) {
    ans = min(ans, lol + warr[i]);
  }
  lol = f(i + 1, currv);
  if (lol != inf) {
    ans = min(ans, lol);
  }

  dp[i][currv] = ans;

  return ans;
}
void solve() {
  cin >> n >> w;
  int totv = 0;
  rep(i, 0, n) {
    cin >> warr[i] >> varr[i];
    totv += varr[i];
  }

  int hi, lo, mid;
  hi = totv;
  lo = 0;
  while (lo <= hi) {
    mid = (hi + lo) / 2;
    tv = mid;
    clr(dp, -1);
    if (f(0, 0) <= w) {
      lo = mid + 1;

    } else {

      hi = mid - 1;
    }
  }
  cout << lo - 1 << endl;
}

int32_t main() {
  fastIO
#ifndef ONLINE_JUDGE
      freopen(R"(C:\Users\swast\CLionProjects\untitled\input.txt)", "r", stdin);
#endif
  int t = 1;
  //    cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}