Problem p03163 (C++)

Buggy submission (status: Runtime Error):

// Problem : C. Chocolate Bunny
// Contest : Codeforces - Codeforces Round #669 (Div. 2)
// URL : https://codeforces.com/contest/1407/problem/C
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vs vector<string>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define mii map<int, int>
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mkp make_pair
#define all(v) v.begin(), v.end()
#define input(a, b, n)                                                         \
  for (i = b; i < n; i++) {                                                    \
    cin >> a[i];                                                               \
  }
#define print(a)                                                               \
  for (i = 0; i < a.size(); i++) {                                             \
    cout << a[i] << " ";                                                       \
  }                                                                            \
  cout << endl;
#define trace1(a)                                                              \
  ;                                                                            \
  cout << "a=" << a << endl;
#define trace2(a, b)                                                           \
  ;                                                                            \
  cout << "a=" << a << " b=" << b << endl;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const int inf = 0x3f3f3f3f3f3f3f3f;
int power(int a, int b) {
  int res = 1;
  while (b != 0) {
    if (b & 1) {
      res *= a;
    }
    a = (a * a);
    b = (b / 2);
  }
  return res;
}
vector<vector<int>> dp(1001, vector<int>(1001, -1));
int knapSack(int W, int wt[], int val[], int n) {
  // Your code here
  if (dp[W][n] != -1) {
    return dp[W][n];
  }
  if (W == 0) {
    dp[W][n] = 0;
    return 0;
  }
  if (n == 0) {
    dp[W][n] = 0;
    return 0;
  }
  int x = -1, y = -1;
  if (wt[n - 1] <= W) {
    x = val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1);
  }
  y = knapSack(W, wt, val, n - 1);
  if (x != -1 && y != -1) {
    dp[W][n] = max(x, y);
    return max(x, y);
  } else if (x != -1) {
    dp[W][n] = x;
    return x;
  } else {
    dp[W][n] = y;
    return y;
  }
}
int32_t main() {
  // fast;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    int w, n;
    cin >> n >> w;
    int wt[n];
    int val[n];
    int i;
    for (i = 0; i < n; i++) {
      cin >> wt[i] >> val[i];
    }
    cout << knapSack(w, wt, val, n) << endl;
  }
  return 0;
}


Fixed submission:

// Problem : C. Chocolate Bunny
// Contest : Codeforces - Codeforces Round #669 (Div. 2)
// URL : https://codeforces.com/contest/1407/problem/C
// Memory Limit : 256 MB
// Time Limit : 1000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define vi vector<int>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vs vector<string>
#define vpii vector<pair<int, int>>
#define pii pair<int, int>
#define mii map<int, int>
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define mkp make_pair
#define all(v) v.begin(), v.end()
#define input(a, b, n)                                                         \
  for (i = b; i < n; i++) {                                                    \
    cin >> a[i];                                                               \
  }
#define print(a)                                                               \
  for (i = 0; i < a.size(); i++) {                                             \
    cout << a[i] << " ";                                                       \
  }                                                                            \
  cout << endl;
#define trace1(a)                                                              \
  ;                                                                            \
  cout << "a=" << a << endl;
#define trace2(a, b)                                                           \
  ;                                                                            \
  cout << "a=" << a << " b=" << b << endl;
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
const int inf = 0x3f3f3f3f3f3f3f3f;
int power(int a, int b) {
  int res = 1;
  while (b != 0) {
    if (b & 1) {
      res *= a;
    }
    a = (a * a);
    b = (b / 2);
  }
  return res;
}
vector<vector<int>> dp(100001, vector<int>(101, -1));
int knapSack(int W, int wt[], int val[], int n) {
  // Your code here
  if (dp[W][n] != -1) {
    return dp[W][n];
  }
  if (W == 0) {
    dp[W][n] = 0;
    return 0;
  }
  if (n == 0) {
    dp[W][n] = 0;
    return 0;
  }
  int x = -1, y = -1;
  if (wt[n - 1] <= W) {
    x = val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1);
  }
  y = knapSack(W, wt, val, n - 1);
  if (x != -1 && y != -1) {
    dp[W][n] = max(x, y);
    return max(x, y);
  } else if (x != -1) {
    dp[W][n] = x;
    return x;
  } else {
    dp[W][n] = y;
    return y;
  }
}
int32_t main() {
  // fast;
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    int w, n;
    cin >> n >> w;
    int wt[n];
    int val[n];
    int i;
    for (i = 0; i < n; i++) {
      cin >> wt[i] >> val[i];
    }
    cout << knapSack(w, wt, val, n) << endl;
  }
  return 0;
}