Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Int int32_t
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define bitcnt(n) __builtin_popcountll(n)
#define setpre(n) cout << fixed << setprecision(n)
#define tr(c)                                                                  \
  for (const auto &x : c)                                                      \
    cout << x << " ";                                                          \
  cout << "\n";
#define ol(c, s, e)                                                            \
  for (int pos = s; pos < e; pos++)                                            \
    cout << c[pos] << " ";                                                     \
  cout << "\n";
#define PI acos(-1LL)

const int M = 1000000007;
const int N = 2e5 + 5;
const long long INF = 1e9 + 12;

vector<vector<int>> v;
int n, weight;
int dp[105][N];
int fun(int i = n - 1, int w = weight) {
  if (w < 0)
    return -INF;
  if (i < 0)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];
  int ans = max(fun(i - 1, w - v[i][0]) + v[i][1], fun(i - 1, w));
  return ans;
}
void solve() {
  cin >> n >> weight;
  v.resize(n, vector<int>(2));
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1];
  }
  cout << fun();
}

signed main() {
  FAST int tc = 1;
  //    TODO: check for tc
  //    cin >> tc;
  for (int t = 1; t <= tc; t++) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Int int32_t
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define bitcnt(n) __builtin_popcountll(n)
#define setpre(n) cout << fixed << setprecision(n)
#define tr(c)                                                                  \
  for (const auto &x : c)                                                      \
    cout << x << " ";                                                          \
  cout << "\n";
#define ol(c, s, e)                                                            \
  for (int pos = s; pos < e; pos++)                                            \
    cout << c[pos] << " ";                                                     \
  cout << "\n";
#define PI acos(-1LL)

const int M = 1000000007;
const int N = 2e5 + 5;
const long long INF = 1e9 + 12;

vector<vector<int>> v;
int n, weight;
int dp[105][N];
int fun(int i = n - 1, int w = weight) {
  if (w < 0)
    return -INF;
  if (i < 0)
    return 0;
  if (dp[i][w] != -1)
    return dp[i][w];
  int ans = max(fun(i - 1, w - v[i][0]) + v[i][1], fun(i - 1, w));
  return dp[i][w] = ans;
}
void solve() {
  cin >> n >> weight;
  v.resize(n, vector<int>(2));
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1];
  }
  cout << fun();
}

signed main() {
  FAST int tc = 1;
  //    TODO: check for tc
  //    cin >> tc;
  for (int t = 1; t <= tc; t++) {
    solve();
  }
}