Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define faltu                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define binary(x)                                                              \
  std::string binary = std::bitset<25>(x).to_string(); // to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int, int>
#define lpi pair<int, int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM = inf;
int pw(int x, int y) {
  int z = 1;
  for (; y; y >>= 1, x = 1ll * x * x % MMM)
    if (y & 1)
      z = 1ll * z * x % MMM;
  return z;
}
using namespace std;
vi pm;
void prm(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++)
    if (prime[p])
      pm.pb(p);
}
bool sortbysecdesc(const pair<ll int, ll int> &a,
                   const pair<ll int, ll int> &b) {
  return a.first > b.first;
}
bool cmp(int x, int y) { return x > y; }
ll int dp[100][100002];
int main() {
  faltu;
  ll int n, ww;
  cin >> n >> ww;
  ll int val[n + 1], we[n + 1];
  for (ll int i = 0; i < n; i++) {
    cin >> we[i] >> val[i];
  }
  for (ll int i = 0; i <= n; i++) {
    for (ll int w = 0; w <= ww; w++) {
      if (i == 0 || w == 0)
        dp[i][w] = 0;
      else if (we[i - 1] <= w) {
        dp[i][w] = max(val[i - 1] + dp[i - 1][w - we[i - 1]], dp[i - 1][w]);
      } else
        dp[i][w] = dp[i - 1][w];
    }
  }
  cout << dp[n][ww];
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define faltu                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define binary(x)                                                              \
  std::string binary = std::bitset<25>(x).to_string(); // to binary
#define inf 1000000007
#define mod 1000000007
#define pi pair<int, int>
#define lpi pair<int, int>
#define mp make_pair
#define pb push_back
#define vi vector<int>
#define lvi vector<long long int>
#define pie 3.14159265358979323846
ll int MMM = inf;
int pw(int x, int y) {
  int z = 1;
  for (; y; y >>= 1, x = 1ll * x * x % MMM)
    if (y & 1)
      z = 1ll * z * x % MMM;
  return z;
}
using namespace std;
vi pm;
void prm(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++)
    if (prime[p])
      pm.pb(p);
}
bool sortbysecdesc(const pair<ll int, ll int> &a,
                   const pair<ll int, ll int> &b) {
  return a.first > b.first;
}
bool cmp(int x, int y) { return x > y; }
ll int dp[205][200002];
int main() {
  faltu;
  ll int n, ww;
  cin >> n >> ww;
  ll int val[n + 1], we[n + 1];
  for (ll int i = 0; i < n; i++) {
    cin >> we[i] >> val[i];
  }
  for (ll int i = 0; i <= n; i++) {
    for (ll int w = 0; w <= ww; w++) {
      if (i == 0 || w == 0)
        dp[i][w] = 0;
      else if (we[i - 1] <= w) {
        dp[i][w] = max(val[i - 1] + dp[i - 1][w - we[i - 1]], dp[i - 1][w]);
      } else
        dp[i][w] = dp[i - 1][w];
    }
  }
  cout << dp[n][ww];
}