Problem p03163 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define MOD (long long int)1e9 + 7
using namespace std;

typedef long long int ll;

// bool cmp(const pair<int, set<int>> a, const pair<int, set<int>> b) {
//     return a.second.size() > b.second.size();
// }

bool cmp(int a, int b) { return a > b; }

// void printV(vector<int> nums) {
//     for (int i: nums) {
//         printf("%d ", i);// << " ";
//     }
//     printf("\n");
// }

void printV(vector<ll> nums) {
  for (ll i : nums) {
    printf("%lld ", i); // << " ";
  }
  printf("\n");
}

int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool valid(int i, int j, int n, int m) {
  if (i >= 0 && i < n && j >= 0 && j < m)
    return true;
  else
    return false;
}

ll dp[110][100010];

ll solve(int index, ll currweight, ll weight, vector<vector<int>> nums, int n) {
  if (index == n)
    return 0;
  if (dp[index][currweight] != -1)
    return dp[index][currweight];
  ll res = solve(index + 1, currweight, weight, nums, n);
  if (currweight + nums[index][0] <= weight) {
    res =
        max(res, nums[index][1] + solve(index + 1, currweight + nums[index][0],
                                        weight, nums, n));
  }
  dp[index][currweight] = res;
  return res;
}

int main() {
#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
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);cout.tie(0);
  // time_t start, end;
  // time(&start);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> nums(n, vector<int>(2));
  for (int i = 0; i < n; i++)
    cin >> nums[i][0] >> nums[i][1];
  memset(dp, -1, sizeof(dp));
  ll res = solve(0, 0, k, nums, n);
  cout << res << endl;
  // l time(&end);
  //  double time_taken = double(end - start);
  //  cout << "Time Taken: " << fixed << time_taken << setprecision(5);
  //  cout << " sec" << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define MOD (long long int)1e9 + 7
using namespace std;

typedef long long int ll;

// bool cmp(const pair<int, set<int>> a, const pair<int, set<int>> b) {
//     return a.second.size() > b.second.size();
// }

bool cmp(int a, int b) { return a > b; }

// void printV(vector<int> nums) {
//     for (int i: nums) {
//         printf("%d ", i);// << " ";
//     }
//     printf("\n");
// }

void printV(vector<ll> nums) {
  for (ll i : nums) {
    printf("%lld ", i); // << " ";
  }
  printf("\n");
}

int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

bool valid(int i, int j, int n, int m) {
  if (i >= 0 && i < n && j >= 0 && j < m)
    return true;
  else
    return false;
}

ll dp[110][100010];

ll solve(int index, ll currweight, ll weight, vector<vector<int>> &nums,
         int n) {
  if (index == n)
    return 0;
  if (dp[index][currweight] != -1)
    return dp[index][currweight];
  ll res = solve(index + 1, currweight, weight, nums, n);
  if (currweight + nums[index][0] <= weight) {
    res =
        max(res, nums[index][1] + solve(index + 1, currweight + nums[index][0],
                                        weight, nums, n));
  }
  dp[index][currweight] = res;
  return res;
}

int main() {
#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
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);cout.tie(0);
  // time_t start, end;
  // time(&start);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> nums(n, vector<int>(2));
  for (int i = 0; i < n; i++)
    cin >> nums[i][0] >> nums[i][1];
  memset(dp, -1, sizeof(dp));
  ll res = solve(0, 0, k, nums, n);
  cout << res << endl;
  // l time(&end);
  //  double time_taken = double(end - start);
  //  cout << "Time Taken: " << fixed << time_taken << setprecision(5);
  //  cout << " sec" << endl;
  return 0;
}