Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define rep(a, b, c) for (int a = (int)b; a < (int)c; a++)
#define repk(a, b, c, k) for (int a = (int)b; a < (int)c; a += (int)k)
#define comeback                                                               \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int, int>
#define ful(a) a.begin(), a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return em((x % mod * x % mod) % mod, n / 2);
  else
    return (x % mod * em((x % mod * x % mod) % mod, (n - 1) / 2)) % mod;
}
int dp[102][100009];
signed main() {
  comeback
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  int n, W, i, j, k, l;
  cin >> n >> W;
  int w[n + 1], va[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> w[i] >> va[i];
  }
  for (i = 0; i <= n; i++)
    for (j = 0; j <= 100000; j++)
      dp[i][j] = 1e15;
  dp[0][0] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= 100000; j++) {
      if (va[i] <= j)
        dp[i][j] = min(dp[i - 1][j], w[i] + dp[i - 1][j - va[i]]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  for (i = 100000; i >= 0; i--)
    if (dp[n][i] <= W) {
      cout << i;
      return 0;
    }
  cout << 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define rep(a, b, c) for (int a = (int)b; a < (int)c; a++)
#define repk(a, b, c, k) for (int a = (int)b; a < (int)c; a += (int)k)
#define comeback                                                               \
  std::ios_base::sync_with_stdio(false);                                       \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mod 1000000007
#define int long long int
#define pii pair<int, int>
#define ful(a) a.begin(), a.end()
#define ub upper_bound
#define lb lower_bound
#define ff first
#define ss second
using namespace std;
int em(int x, int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return em((x % mod * x % mod) % mod, n / 2);
  else
    return (x % mod * em((x % mod * x % mod) % mod, (n - 1) / 2)) % mod;
}
int dp[102][100009];
signed main() {
  comeback
      /*#ifndef ONLINE_JUDGE
              freopen("input.txt","r",stdin);
              freopen("output1.txt","w",stdout);
      #endif*/
      int n,
      W, i, j, k, l;
  cin >> n >> W;
  int w[n + 1], va[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> w[i] >> va[i];
  }
  for (i = 0; i <= n; i++)
    for (j = 0; j <= 100000; j++)
      dp[i][j] = 1e15;
  dp[0][0] = 0;
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= 100000; j++) {
      if (va[i] <= j)
        dp[i][j] = min(dp[i - 1][j], w[i] + dp[i - 1][j - va[i]]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }
  for (i = 100000; i >= 0; i--)
    if (dp[n][i] <= W) {
      cout << i;
      return 0;
    }
  cout << 0;
}