Problem p03163 (C++)

Buggy submission (status: Runtime Error):
// author:satwik_bhv1
#include <bits/stdc++.h>
// datatypes
#define ll long long
#define ld long double
// loops
#define fr(i, z, n) for (ll i = z; i < n; i++)
#define br(i, z, n) for (ll i = z; i > n; i--)
// operations
#define mp make_pair
#define ff first
#define ss second
#define pub push_back
#define all(v) v.begin(), v.end()
// map
#define mi map<ll, ll>
// vectors
#define vi vector<ll>
#define vpi vector<pair<ll, ll>>
// constants
#define pi 3.1415926535897932384626
#define mod 1000000007
#define MAXN 100001
/*notes
    Don't use inbuilt fun for power
    Range of longlong=(-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 )
    INT_MAX = 2147483647(aprox 2*10^10)
    INT_MIN = -2147483648
    LLONG_MAX = 9223372036854775807
    LLONG_MIN = -9223372036854775808
    __gcd is the function in built for hcf
    priority queue is max heap by default
    topological sort -> khan's algo use bfs and indegree of node
    sssp(positive weighted edges) -> Dijkstras
    apsp -> Floydwarshall
    MST -> kruskal(DSU)
*/
using namespace std;
// functions
ll power(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
    }
    y /= 2;
    x *= x;
  }
  return res;
}
ll power(ll x, ll y, ll z) {
  ll res = 1;
  while (y) {
    if (y % 2) {
      res = (res * x) % z;
    }
    y /= 2;
    x = (x * x) % z;
  }
  return res;
}
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.second > b.second);
}
ll ceil(ll x, ll y) { return (x / y) + (x % y != 0); }
ll n, w;
ll v[100][2];
ll dp[100][100001];
ll fun(ll pos, ll limit) {
  if (pos == n) {
    return 0;
  }
  ll ans = 0;
  if (dp[pos + 1][limit] == -1) {
    ans = max(ans, fun(pos + 1, limit));
  } else {
    ans = max(ans, dp[pos + 1][limit]);
  }
  if (limit >= v[pos][0]) {
    if (dp[pos + 1][limit - v[pos][0]] == -1) {
      ans = max(ans, fun(pos + 1, limit - v[pos][0]) + v[pos][1]);
    } else {
      ans = max(ans, dp[pos + 1][limit - v[pos][0]] + v[pos][1]);
    }
  }
  return dp[pos][limit] = ans;
}
void solve() {
  cin >> n >> w;
  fr(i, 0, n) {
    fr(j, 0, 2) { cin >> v[i][j]; }
  }
  fr(i, 0, n) {
    fr(j, 0, w + 1) { dp[i][j] = -1; }
  }
  cout << fun(0, w);
}
int main() {
  fast();
  ll t;
  t = 1;
  // cin>>t;
  fr(i, 0, t) { solve(); }
}
/*





























*/

Fixed submission:
// author:satwik_bhv1
#include <bits/stdc++.h>
// datatypes
#define ll long long
#define ld long double
// loops
#define fr(i, z, n) for (ll i = z; i < n; i++)
#define br(i, z, n) for (ll i = z; i > n; i--)
// operations
#define mp make_pair
#define ff first
#define ss second
#define pub push_back
#define all(v) v.begin(), v.end()
// map
#define mi map<ll, ll>
// vectors
#define vi vector<ll>
#define vpi vector<pair<ll, ll>>
// constants
#define pi 3.1415926535897932384626
#define mod 1000000007
#define MAXN 100001
/*notes
    Don't use inbuilt fun for power
    Range of longlong=(-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 )
    INT_MAX = 2147483647(aprox 2*10^10)
    INT_MIN = -2147483648
    LLONG_MAX = 9223372036854775807
    LLONG_MIN = -9223372036854775808
    __gcd is the function in built for hcf
    priority queue is max heap by default
    topological sort -> khan's algo use bfs and indegree of node
    sssp(positive weighted edges) -> Dijkstras
    apsp -> Floydwarshall
    MST -> kruskal(DSU)
*/
using namespace std;
// functions
ll power(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
    }
    y /= 2;
    x *= x;
  }
  return res;
}
ll power(ll x, ll y, ll z) {
  ll res = 1;
  while (y) {
    if (y % 2) {
      res = (res * x) % z;
    }
    y /= 2;
    x = (x * x) % z;
  }
  return res;
}
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b) {
  return (a.second > b.second);
}
ll ceil(ll x, ll y) { return (x / y) + (x % y != 0); }
ll n, w;
ll v[101][2];
ll dp[101][100001];
ll fun(ll pos, ll limit) {
  if (pos == n) {
    return 0;
  }
  ll ans = 0;
  if (dp[pos + 1][limit] == -1) {
    ans = max(ans, fun(pos + 1, limit));
  } else {
    ans = max(ans, dp[pos + 1][limit]);
  }
  if (limit >= v[pos][0]) {
    if (dp[pos + 1][limit - v[pos][0]] == -1) {
      ans = max(ans, fun(pos + 1, limit - v[pos][0]) + v[pos][1]);
    } else {
      ans = max(ans, dp[pos + 1][limit - v[pos][0]] + v[pos][1]);
    }
  }
  return dp[pos][limit] = ans;
}
void solve() {
  cin >> n >> w;
  fr(i, 0, n) {
    fr(j, 0, 2) { cin >> v[i][j]; }
  }
  fr(i, 0, n) {
    fr(j, 0, w + 1) { dp[i][j] = -1; }
  }
  cout << fun(0, w);
}
int main() {
  fast();
  ll t;
  t = 1;
  // cin>>t;
  fr(i, 0, t) { solve(); }
}
/*





























*/