Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10, OO = 0x3f3f3f3f;
// const int mod = 1000000007;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define loop(i, n) for (int i = 0; i < int(n); i++)
#define rloop(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define xloop(i, a, b) for (int i = int(a); i <= int(b); i++)
#define range(vec)                                                             \
  for (auto &x : vec)                                                          \
    cin >> x;
#define ALL(v) ((v).begin()), ((v).end())
#define SZ(v) ((int)((v).size()))
// int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
// int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
// int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight
// Direction int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal
// Direction

int n, weight;
int w[105], v[105];
ll dp[105][1005];

ll solve(int idx, int remV) {

  if (idx >= n) {
    if (remV == 0)
      return 0;
    return 1LL * OO * OO;
  }
  ll &ret = dp[idx][remV];
  if (~ret)
    return ret;
  ret = 1LL * OO * OO;
  ret = min(ret, solve(idx + 1, remV));
  if (remV >= v[idx])
    ret = min(ret, solve(idx + 1, remV - v[idx]) + w[idx]);
  return ret;
}

int main() {
  startTime = (double)clock();
#ifndef ONLINE_JUDGE
  // freopen("in.in", "r", stdin);
  // freopen("out.in", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> weight;
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  clr(dp, -1);
  for (int value = sum; value >= 0; --value) {
    ll leastW = solve(0, value);
    if (leastW <= weight) {
      cout << value << "\n";
      return 0;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10, OO = 0x3f3f3f3f;
// const int mod = 1000000007;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define loop(i, n) for (int i = 0; i < int(n); i++)
#define rloop(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define xloop(i, a, b) for (int i = int(a); i <= int(b); i++)
#define range(vec)                                                             \
  for (auto &x : vec)                                                          \
    cin >> x;
#define ALL(v) ((v).begin()), ((v).end())
#define SZ(v) ((int)((v).size()))
// int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
// int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
// int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight
// Direction int dx[6]={2,1,-1,-2,-1,1};int dy[6]={0,1,1,0,-1,-1}; //Hexagonal
// Direction

int n, weight;
int w[105], v[105];
ll dp[105][100005];

ll solve(int idx, int remV) {

  if (idx >= n) {
    if (remV == 0)
      return 0;
    return 1LL * OO * OO;
  }
  ll &ret = dp[idx][remV];
  if (~ret)
    return ret;
  ret = 1LL * OO * OO;
  ret = min(ret, solve(idx + 1, remV));
  if (remV >= v[idx])
    ret = min(ret, solve(idx + 1, remV - v[idx]) + w[idx]);
  return ret;
}

int main() {
  startTime = (double)clock();
#ifndef ONLINE_JUDGE
  // freopen("in.in", "r", stdin);
  // freopen("out.in", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> weight;
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    cin >> w[i] >> v[i];
    sum += v[i];
  }
  clr(dp, -1);
  for (int value = sum; value >= 0; --value) {
    ll leastW = solve(0, value);
    if (leastW <= weight) {
      cout << value << "\n";
      return 0;
    }
  }

  return 0;
}