Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
#define ll long long
#define endl "\n"
const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
const ll integer = pow(2, 32) - 1; // 4294967295
#define N 8010000
#define TC                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  for (int T = 1; T <= t; T++)
#define mod 1000000007
#define PI acos(-1)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(v, d) memset(v, d, sizeof(v))
void AIA() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
bool isprime(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return (n != 1);
}
ll gcd(ll x, ll y) { return (y == 0 ? x : gcd(y, x % y)); }
ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }
/*
 _____________              __                ___        ___
___________________________     ____        ____ |             | /\ \ |   | | |
|                           |    \   \      /   / |   __________|           /
/\ \             |   |      |   |     |      _____      _____     | \   \    / /
|  |                     / /  \ \            |   |      |   |     |     |     |
|     |    |      \   \  /   / |  |                    / /    \ \           | |
|   |     |     |     |    |     |    |       \   \/   / |  |______ / / \ \ |
|______|   |     |     |     |    |     |    |        \      / |         | / /
\ \         |              |     |     |     |    |     |    |         \    / |
______|          / /          \ \        |    ______    |     |     |     |    |
|    |          |  | |  |                / /============\ \       |   |      |
|     |     |     |    |     |    |          |  | |  |               /
/==============\ \      |   |      |   |     |     |     |    |     |    | |  |
|  |              / /                \ \     |   |      |   |     |     |     |
|     |    |          |  | |  |             / /                  \ \    |   | |
|     |     |     |    |     |    |          |  |
|__|            /_/                    \_\   |___|      |___|     |_____| |____|
|____|          |__|

                                                                                  <<<<<<<<<<< NEVER GIVE UP !! >>>>>>>>>>>
                                                                                <<<<<<<< YOU CAN DO IT IF YOU WANT !! >>>>>>>>
                                                                                 <<<<<<<<< BELIEVE IN YOURSELF !! >>>>>>>>>
*/
ll n, x, k, m, cnt = 0;
vector<ll> v3, v, w;
ll dp[107][100007];
ll solve(ll i, ll W) {
  if (i == 0 || W == 0) {
    return 0;
  }
  ll &ans = dp[i][W];
  if (ans != -1) {
    return ans;
  } else {
    if (W >= w[i - 1]) {
      return dp[i][W] =
                 max(v[i - 1] + solve(i - 1, W - w[i - 1]), solve(i - 1, W));
    } else {
      dp[i][W] = solve(i - 1, W);
    }
  }
}
int main() {
  // cout<<fixed<<setprecision(20);
  // freopen("Input.txt.txt", "r", stdin);
  // freopen("Output.txt.txt", "w", stdout);
  AIA();
  cin >> n >> k;
  v.resize(n);
  w.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  memset(dp, -1, sizeof dp);
  cout << solve(n, k);

  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;
#define ll long long
#define endl "\n"
const int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
const ll integer = pow(2, 32) - 1; // 4294967295
#define N 8010000
#define TC                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  for (int T = 1; T <= t; T++)
#define mod 1000000007
#define PI acos(-1)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(v, d) memset(v, d, sizeof(v))
void AIA() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
bool isprime(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return 0;
  }
  return (n != 1);
}
ll gcd(ll x, ll y) { return (y == 0 ? x : gcd(y, x % y)); }
ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); }
/*
 _____________              __                ___        ___
___________________________     ____        ____ |             | /\ \ |   | | |
|                           |    \   \      /   / |   __________|           /
/\ \             |   |      |   |     |      _____      _____     | \   \    / /
|  |                     / /  \ \            |   |      |   |     |     |     |
|     |    |      \   \  /   / |  |                    / /    \ \           | |
|   |     |     |     |    |     |    |       \   \/   / |  |______ / / \ \ |
|______|   |     |     |     |    |     |    |        \      / |         | / /
\ \         |              |     |     |     |    |     |    |         \    / |
______|          / /          \ \        |    ______    |     |     |     |    |
|    |          |  | |  |                / /============\ \       |   |      |
|     |     |     |    |     |    |          |  | |  |               /
/==============\ \      |   |      |   |     |     |     |    |     |    | |  |
|  |              / /                \ \     |   |      |   |     |     |     |
|     |    |          |  | |  |             / /                  \ \    |   | |
|     |     |     |    |     |    |          |  |
|__|            /_/                    \_\   |___|      |___|     |_____| |____|
|____|          |__|

                                                                                  <<<<<<<<<<< NEVER GIVE UP !! >>>>>>>>>>>
                                                                                <<<<<<<< YOU CAN DO IT IF YOU WANT !! >>>>>>>>
                                                                                 <<<<<<<<< BELIEVE IN YOURSELF !! >>>>>>>>>
*/
ll n, x, k, m, cnt = 0;
vector<ll> v3, v, w;
ll dp[107][100007];
ll solve(ll i, ll W) {
  if (i == 0 || W == 0) {
    return 0;
  }
  ll &ans = dp[i][W];
  if (ans != -1) {
    return ans;
  } else {
    if (W >= w[i - 1]) {
      return dp[i][W] =
                 max(v[i - 1] + solve(i - 1, W - w[i - 1]), solve(i - 1, W));
    } else {
      return dp[i][W] = solve(i - 1, W);
    }
  }
}
int main() {
  // cout<<fixed<<setprecision(20);
  // freopen("Input.txt.txt", "r", stdin);
  // freopen("Output.txt.txt", "w", stdout);
  AIA();
  cin >> n >> k;
  v.resize(n);
  w.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> w[i] >> v[i];
  }
  memset(dp, -1, sizeof dp);
  cout << solve(n, k);

  return 0;
}