Problem p03163 (C++)

Buggy submission (status: Runtime Error):
/******************************************
* AUTHOR : SHUBHAM KUMAR *
* NICK : { "CODECHEF" : "SCHELEON",
                   "CODEFORCES" : "SCH3130N" } *
* INSTITUTION : BIT MESRA *
******************************************/

#include <bits/stdc++.h>
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define sd(x) scanf("%lld", &x)
#define ll long long int
#define large 100005
#define MOD 1000000007
#define dd double
#define pb push_back
#define mk make_pair
#define rep0(i, b) for (int i = 0; i < b; i++)
#define rep1(i, b) for (int i = 1; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i <= b; i++)
#define pll pair<ll, ll>
#define ld long double
#define vll vector<ll>
#define vpll vector<pll>
#define sortv(v) sort(v.begin(), v.end())

ll ModE(ll x, unsigned ll y, ll p = MOD) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

using namespace std;

ll dp[100][large], a[large][3];
ll n;
ll v[100], w[100];
ll solve(ll i, ll weight) {
  if (i < 0)
    return 0;
  if (dp[i][weight] != -1)
    return dp[i][weight];

  if (weight - w[i] >= 0)
    dp[i][weight] =
        max(v[i] + solve(i - 1, weight - w[i]), solve(i - 1, weight));
  else
    dp[i][weight] = solve(i - 1, weight);
  return dp[i][weight];

  // if(weight-w[i]>=0)
  // 	return max(v[i]+solve(i-1, weight-w[i]), solve(i-1, weight));
  // else
  // 	return solve(i-1, weight);
  // return dp[i][weight];
}

int main() {
  // #ifndef _DEBUG
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
#endif

  fastio
      // ios_base& scientific (ios_base& str);

      // ll T; cin>>T; while(T--){}
      ll W;
  cin >> n >> W;
  rep0(i, n) { cin >> w[i] >> v[i]; }
  rep0(i, large) rep0(j, n) dp[j][i] = -1;

  // rep0(i,10){
  // 	rep0(j,10)
  // 		cout<<dp[i][j]<<" ";
  // 		cout<<endl;}
  // cout<<"hello"<<endl;
  cout << solve(n - 1, W);

  return 0;
}

Fixed submission:
/******************************************
* AUTHOR : SHUBHAM KUMAR *
* NICK : { "CODECHEF" : "SCHELEON",
                   "CODEFORCES" : "SCH3130N" } *
* INSTITUTION : BIT MESRA *
******************************************/

#include <bits/stdc++.h>
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define sd(x) scanf("%lld", &x)
#define ll long long int
#define large 100005
#define MOD 1000000007
#define dd double
#define pb push_back
#define mk make_pair
#define rep0(i, b) for (int i = 0; i < b; i++)
#define rep1(i, b) for (int i = 1; i <= b; i++)
#define rep2(i, a, b) for (int i = a; i <= b; i++)
#define pll pair<ll, ll>
#define ld long double
#define vll vector<ll>
#define vpll vector<pll>
#define sortv(v) sort(v.begin(), v.end())

ll ModE(ll x, unsigned ll y, ll p = MOD) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

using namespace std;

ll dp[100][large], a[large][3];
ll n;
ll v[100], w[100];
ll solve(ll i, ll weight) {
  if (i < 0)
    return 0;
  if (dp[i][weight] != -1)
    return dp[i][weight];

  if (weight - w[i] >= 0)
    dp[i][weight] =
        max(v[i] + solve(i - 1, weight - w[i]), solve(i - 1, weight));
  else
    dp[i][weight] = solve(i - 1, weight);
  return dp[i][weight];

  // if(weight-w[i]>=0)
  // 	return max(v[i]+solve(i-1, weight-w[i]), solve(i-1, weight));
  // else
  // 	return solve(i-1, weight);
  // return dp[i][weight];
}

int main() {
  // #ifndef _DEBUG
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  // freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  // freopen("output.txt", "w", stdout);
#endif

  fastio
      // ios_base& scientific (ios_base& str);

      // ll T; cin>>T; while(T--){}
      ll W;
  cin >> n >> W;
  rep0(i, n) { cin >> w[i] >> v[i]; }
  rep0(i, large) rep0(j, n) dp[j][i] = -1;

  // rep0(i,10){
  // 	rep0(j,10)
  // 		cout<<dp[i][j]<<" ";
  // 		cout<<endl;}
  // cout<<"hello"<<endl;
  cout << solve(n - 1, W);

  return 0;
}