Problem p03163 (C++)

Buggy submission (status: Runtime Error):
/*author : Yashvardhan
Saturday, January 12, 2019
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

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

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i)
    os << v[i] << " ";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto it : v)
    os << it << " ";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.ff << " " << v.ss;
  return os;
}

const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;

int dp[101][100005];
int wi[101];
int vs[101];

int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

int solve(int n, int w) {
  if (dp[n][w] != -1)
    return dp[n][w];
  if (w == 0)
    return dp[n][w] = 0;
  if (wi[n] > w)
    return dp[n][w] = solve(n - 1, w);
  else
    return dp[n][w] = max(solve(n - 1, w), vs[n] + solve(n - 1, w - wi[n]));
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  time_t t1, t2;
  t1 = clock();

  int n, w1;
  cin >> n >> w1;

  for (int i = 1; i <= n; i++) {
    int w, v;
    cin >> w >> v;
    wi[i] = w;
    vs[i] = v;
  }

  for (int i = 0; i < 101; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = -1;
    }
  }

  cout << solve(n, w1) << endl;
  t2 = clock();
  cerr << endl << t2 - t1 << endl;

  return 0;
}


Fixed submission:
/*author : Yashvardhan
Saturday, January 12, 2019
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define int ll
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define ff first
#define ss second

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS

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

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); ++i)
    os << v[i] << " ";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto it : v)
    os << it << " ";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.ff << " " << v.ss;
  return os;
}

const int mod = 1e9 + 7;
const int inf = 2e18;
const int ninf = -2e18;

int dp[101][100005];
int wi[101];
int vs[101];

int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

int solve(int n, int w) {
  if (dp[n][w] != -1)
    return dp[n][w];
  if (n == 0)
    return dp[n][w] = 0;
  if (wi[n] > w)
    return dp[n][w] = solve(n - 1, w);
  else
    return dp[n][w] = max(solve(n - 1, w), vs[n] + solve(n - 1, w - wi[n]));
}

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  time_t t1, t2;
  t1 = clock();

  int n, w1;
  cin >> n >> w1;

  for (int i = 1; i <= n; i++) {
    int w, v;
    cin >> w >> v;
    wi[i] = w;
    vs[i] = v;
  }

  for (int i = 0; i < 101; i++) {
    for (int j = 0; j <= 100000; j++) {
      dp[i][j] = -1;
    }
  }

  cout << solve(n, w1) << endl;
  t2 = clock();
  cerr << endl << t2 - t1 << endl;

  return 0;
}