Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define rep0(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define reprev1(i, n) for (int i = n; i > 0; i--)
#define reprev0(i, n) for (int i = n - 1; i >= 0; i--)
#define reprev(i, x, y) for (int i = x; i >= y; i--)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define si set<int>
#define vi vector<int>
#define mii map<int, int>
#define pii pair<int, int>
#define usi unordered_set<int>
#define umii unordered_map<int, int>
#define pqmx priority_queue<int>
#define pqmn priority_queue<int, vi, greater<int>>
#define sp(x, y) fixed << setprecision(y) << x
#define setbits(x) __builtin_popcount(x)
#define zerobits(x) __builtin_ctz(x)
#define in(a, n)                                                               \
  vi a(n);                                                                     \
  rep0(i, n) cin >> a[i];
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define endl '\n'
#define mod 1000000007
#define M (int)1e6 + 6
#define inf 1e18
using namespace std;

int n, m, k, x, y, z, a1, b, c, d, i, j;
int dp[M][105] = {};

int profit(int weight, int w[], int v[], int i) {

  if (i < 0)
    return 0;

  // if(dp[weight][i]!=0)return dp[weight][i];
  if (w[i] > weight)
    return dp[weight][i] = profit(weight, w, v, i - 1);
  int op1 = profit(weight, w, v, i - 1);
  int op2 = v[i] + profit(weight - w[i], w, v, i - 1);

  return dp[weight][i] = max(op1, op2);
}

void sol() {
  int weight;
  cin >> n >> weight;
  int w[n], v[n];
  rep0(i, n) cin >> w[i] >> v[i];

  cout << profit(weight, w, v, n - 1) << endl;
}

int32_t main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // w(t)
  sol();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define rep0(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define reprev1(i, n) for (int i = n; i > 0; i--)
#define reprev0(i, n) for (int i = n - 1; i >= 0; i--)
#define reprev(i, x, y) for (int i = x; i >= y; i--)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define si set<int>
#define vi vector<int>
#define mii map<int, int>
#define pii pair<int, int>
#define usi unordered_set<int>
#define umii unordered_map<int, int>
#define pqmx priority_queue<int>
#define pqmn priority_queue<int, vi, greater<int>>
#define sp(x, y) fixed << setprecision(y) << x
#define setbits(x) __builtin_popcount(x)
#define zerobits(x) __builtin_ctz(x)
#define in(a, n)                                                               \
  vi a(n);                                                                     \
  rep0(i, n) cin >> a[i];
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define endl '\n'
#define mod 1000000007
#define M (int)1e6 + 6
#define inf 1e18
using namespace std;

int n, m, k, x, y, z, a1, b, c, d, i, j;
int dp[M][105] = {};

int profit(int weight, int w[], int v[], int i) {

  if (i < 0)
    return 0;

  if (dp[weight][i] != 0)
    return dp[weight][i];
  if (w[i] > weight)
    return dp[weight][i] = profit(weight, w, v, i - 1);
  int op1 = profit(weight, w, v, i - 1);
  int op2 = v[i] + profit(weight - w[i], w, v, i - 1);

  return dp[weight][i] = max(op1, op2);
}

void sol() {
  int weight;
  cin >> n >> weight;
  int w[n], v[n];
  rep0(i, n) cin >> w[i] >> v[i];

  cout << profit(weight, w, v, n - 1) << endl;
}

int32_t main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // w(t)
  sol();
  return 0;
}