Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/*
 *  GUST KEEP GOING
 */
#define endl '\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<ll, ll>
#define vii vector<ii>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...)                                                             \
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
void run() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
}
int n, w;
ll dp[101][100001];
vector<pair<int, int>> v;
ll sol(int i, int w) {
  if (w < 0) {
    return -1e12;
  }
  if (i == n || w == 0)
    return 0;
  ll &ret = dp[i][w];
  if (~ret)
    return ret;
  return ret = max(sol(i + 1, w), v[i].second + sol(i + 1, w - v[i].first));
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  /// freopen("output.txt", "w", stdout);
#endif
  run();
  cin >> n >> w;
  v.resize(n);
  for (auto &i : v) {
    cin >> i.first >> i.second;
  }
  memset(dp, -1, sizeof(dp));
  cout << sol(0, w) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/*
 *  GUST KEEP GOING
 */
#define endl '\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<ll, ll>
#define vii vector<ii>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...)                                                             \
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
void run() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
}
int n, w;
ll dp[101][100001];
vector<pair<int, int>> v;
ll sol(int i, int w) {
  if (w < 0) {
    return -1e12;
  }
  if (i == n || w == 0)
    return 0;
  ll &ret = dp[i][w];
  if (~ret)
    return ret;
  return ret = max(sol(i + 1, w), v[i].second + sol(i + 1, w - v[i].first));
}
int main() {
  /*#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      ///freopen("output.txt", "w", stdout);
  #endif*/
  run();
  cin >> n >> w;
  v.resize(n);
  for (auto &i : v) {
    cin >> i.first >> i.second;
  }
  memset(dp, -1, sizeof(dp));
  cout << sol(0, w) << endl;
  return 0;
}