Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 _  __    _   _   _ ____ _____
| |/ /   / \ | | | / ___|_   _|
| ' /   / _ \| | | \___ \ | |
| . \  / ___ \ |_| |___) || |
|_|\_\/_/   \_\___/|____/ |_|
*/
#include <bits/stdc++.h>
#include <fstream>
#define rep(i, a, b) for (long long i = (a); i < (b); i++)
#define per(i, a, b) for (long long i = (a); i > (b); i--)
#define a(x) (x.begin(), x.end())
#define ar(x) (x.rbegin(), x.rend())
#define pb push_back
#define Pb() pop_back()
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sc scanf
#define scin(x) sc("%d", &(x))
#define scln(x) sc("%lld", &(x))
#define pf prllf
#define ms(a, b) memset(a, b, sizeof(a))
#define mp make_pair
#define db double
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x) * (x)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define DBG pf("HI\n")
#define MOD 1000000007
#define CIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++)
#define CASE(t) prllf("Case %d: ", t)
#define CASEL(t) prllf("Case %d:\n", t)
#define intlimit 2147483647
#define longlimit 9223372036854775807
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define PI 2 * acos(0.0)
using namespace std;
vector<pair<ll, ll>> v;
ll n, w;
vll dp;
ll find_max(ll index, ll weight) {
  if (weight == 0 || (weight > 0 && index >= n)) {
    return 0;
  }
  if (weight < 0 || index >= n) {
    return -100000000000;
  }
  if (dp[index][weight] != -1) {
    return dp[index][weight];
  }
  ll maxi = 0;
  rep(i, index, n) {
    maxi = max(maxi, v[i].second + find_max(i + 1, weight - v[i].first));
  }
  dp[index][weight] = maxi;
  return maxi;
}
int main() {
  CIN;
  cin >> n >> w;
  v.resize(n);
  dp.resize(n, vl(w + 1, -1));
  rep(i, 0, n) { cin >> v[i].first >> v[i].second; }
  cout << find_max(0, w);
}

Fixed submission:
/*
 _  __    _   _   _ ____ _____
| |/ /   / \ | | | / ___|_   _|
| ' /   / _ \| | | \___ \ | |
| . \  / ___ \ |_| |___) || |
|_|\_\/_/   \_\___/|____/ |_|
*/
#include <bits/stdc++.h>
#include <fstream>
#define rep(i, a, b) for (long long i = (a); i < (b); i++)
#define per(i, a, b) for (long long i = (a); i > (b); i--)
#define a(x) (x.begin(), x.end())
#define ar(x) (x.rbegin(), x.rend())
#define pb push_back
#define Pb() pop_back()
#define ll long long int
#define ull unsigned long long int
#define pii pair<int, int>
#define pll pair<ll, ll>
#define sc scanf
#define scin(x) sc("%d", &(x))
#define scln(x) sc("%lld", &(x))
#define pf prllf
#define ms(a, b) memset(a, b, sizeof(a))
#define mp make_pair
#define db double
#define EPS 10E-10
#define ff first
#define ss second
#define sqr(x) (x) * (x)
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<ll>>
#define DBG pf("HI\n")
#define MOD 1000000007
#define CIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define RUN_CASE(t, T) for (__typeof(t) t = 1; t <= T; t++)
#define CASE(t) prllf("Case %d: ", t)
#define CASEL(t) prllf("Case %d:\n", t)
#define intlimit 2147483647
#define longlimit 9223372036854775807
#define infinity (1 << 28)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b) / gcd(a, b))
#define PI 2 * acos(0.0)
using namespace std;
vector<pair<ll, ll>> v;
ll n, w;
vll dp;
ll find_max(ll index, ll weight) {
  if (weight == 0 || (weight > 0 && index >= n)) {
    return 0;
  }
  if (weight < 0 || index >= n) {
    return -100000000000;
  }
  if (dp[index][weight] != -1) {
    return dp[index][weight];
  }
  ll maxi = 0;
  maxi = max(v[index].second + find_max(index + 1, weight - v[index].first),
             find_max(index + 1, weight));
  dp[index][weight] = maxi;
  return maxi;
}
int main() {
  CIN;
  cin >> n >> w;
  v.resize(n);
  dp.resize(n, vl(w + 1, -1));
  rep(i, 0, n) { cin >> v[i].first >> v[i].second; }
  cout << find_max(0, w);
}