Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define MuhammedAly                                                            \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
#define popc(x) __builtin_popcount(x)
#define LLMX 1e18
#define NotinRange(n, m) i < 0 || i >= (n) || j < 0 || j >= (m)
#define mem(x, y) memset((x), (y), sizeof(x))
#define sz(a) (int)(a).size()
#define inFile(s) freopen((s), "r", stdin)
#define outFile(s) freopen((s), "w", stdout)
#define biEdge(v, a, b) (v)[(a)].pb((b)), (v)[(b)].pb((a))
#define pi (2 * acos(0))
#define X real()
#define Y imag()
#define cross(a, b) ((conj(a) * (b)).Y)
#define dot(a, b) ((conj(a) * (b)).X)
#define vec(a, b) ((b) - (a))
#define ll long long
typedef long double ld;
typedef complex<double> point;
typedef tuple<int, int, int> line;
typedef pair<point, point> segment;
typedef vector<point> polygon;
const int N = 1e3 + 5, M = 20 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7, base = 131,
          mod2 = 1000136437, mod3 = 998244353;
const double EPS = 1e-9;
ll n, w, memo[105][N], W[105], v[105], sum;
ll solve(int i, int V) {
  if (V == 0)
    return 0;
  if (i == n)
    return LLMX;
  ll &ret = memo[i][V];
  if (~ret)
    return ret;
  ret = solve(i + 1, V);
  if (V - v[i] >= 0)
    ret = min(ret, solve(i + 1, V - v[i]) + W[i]);
  return ret;
}
int main() {
  MuhammedAly mem(memo, -1);
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> W[i] >> v[i], sum += v[i];
  for (int i = sum; ~i; i--)
    if (solve(0, i) <= w)
      return cout << i, 0;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define MuhammedAly                                                            \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
#define popc(x) __builtin_popcount(x)
#define LLMX 1e18
#define NotinRange(n, m) i < 0 || i >= (n) || j < 0 || j >= (m)
#define mem(x, y) memset((x), (y), sizeof(x))
#define sz(a) (int)(a).size()
#define inFile(s) freopen((s), "r", stdin)
#define outFile(s) freopen((s), "w", stdout)
#define biEdge(v, a, b) (v)[(a)].pb((b)), (v)[(b)].pb((a))
#define pi (2 * acos(0))
#define X real()
#define Y imag()
#define cross(a, b) ((conj(a) * (b)).Y)
#define dot(a, b) ((conj(a) * (b)).X)
#define vec(a, b) ((b) - (a))
#define ll long long
typedef long double ld;
typedef complex<double> point;
typedef tuple<int, int, int> line;
typedef pair<point, point> segment;
typedef vector<point> polygon;
const int N = 1e5 + 5, M = 20 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7, base = 131,
          mod2 = 1000136437, mod3 = 998244353;
const double EPS = 1e-9;
ll n, w, memo[105][N], W[105], v[105], sum;
ll solve(int i, int V) {
  if (V == 0)
    return 0;
  if (i == n)
    return LLMX;
  ll &ret = memo[i][V];
  if (~ret)
    return ret;
  ret = solve(i + 1, V);
  if (V - v[i] >= 0)
    ret = min(ret, solve(i + 1, V - v[i]) + W[i]);
  return ret;
}
int main() {
  MuhammedAly mem(memo, -1);
  cin >> n >> w;
  for (int i = 0; i < n; i++)
    cin >> W[i] >> v[i], sum += v[i];
  for (int i = sum; ~i; i--)
    if (solve(0, i) <= w)
      return cout << i, 0;
  return 0;
}