Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define user spaesk
#define lli long long int
#define ld long double
#define ff first
#define ss second
#define lower(a) transform(a.begin(), a.end(), a.begin(), ::tolower);

using namespace std;

typedef vector<vector<lli>> matrix;

const int N = 3e5 + 500;
const long long mod = 1e9 + 7;
const long long cmod = 998244353;
const long long inf = 1LL << 61;
const int M = 1e6 + 500;
const lli ths = 1LL << 40;
const int NN = 5e3 + 6;

lli n, w;
lli W[102], V[102];
lli cache[101][N];

lli dp(lli idx, lli weight) {

  if (idx > n + 1) {
    return 0;
  }

  lli &ans = cache[idx][weight];

  if (ans != -1) {
    return ans;
  }

  if (weight >= W[idx])
    ans = max(V[idx] + dp(idx + 1, weight - W[idx]), dp(idx + 1, weight));
  else
    ans = dp(idx + 1, weight);

  return ans;
}

void solve() {

  cin >> n >> w;

  memset(cache, -1, sizeof cache);

  for (int i = 1; i <= n; i++) {
    cin >> W[i] >> V[i];
  }

  lli ans = dp(1, w);

  cout << ans;

  cout << "\n";
  return;
}

/*
 */

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  //    cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define user spaesk
#define lli long long int
#define ld long double
#define ff first
#define ss second
#define lower(a) transform(a.begin(), a.end(), a.begin(), ::tolower);

using namespace std;

typedef vector<vector<lli>> matrix;

const int N = 3e5 + 500;
const long long mod = 1e9 + 7;
const long long cmod = 998244353;
const long long inf = 1LL << 61;
const int M = 1e6 + 500;
const lli ths = 1LL << 40;
const int NN = 5e3 + 6;

lli n, w;
lli W[102], V[102];
lli cache[101][N];

lli dp(lli idx, lli weight) {

  if (idx > n) {
    return 0;
  }

  lli &ans = cache[idx][weight];

  if (ans != -1) {
    return ans;
  }

  if (weight >= W[idx])
    ans = max(V[idx] + dp(idx + 1, weight - W[idx]), dp(idx + 1, weight));
  else
    ans = dp(idx + 1, weight);

  return ans;
}

void solve() {

  cin >> n >> w;

  memset(cache, -1, sizeof cache);

  for (int i = 1; i <= n; i++) {
    cin >> W[i] >> V[i];
  }

  lli ans = dp(1, w);

  cout << ans;

  cout << "\n";
  return;
}

/*
 */

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int t = 1;
  //    cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}