Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define F first
#define S second
#define FOR(i, r) for (int i = 0; i < r; i++)
#define REP(i, l, r) for (int i = l; i < r; i++)
#define RER(i, l, r) for (int i = l; i >= r; i--)
#define print(v)                                                               \
  for (auto i : v)                                                             \
    cout << i << ' ';
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mem(arr, k) memset(arr, k, sizeof(arr));
#define pii acos(-1.0)
#define PI 3.1415926535897932385
#define Sin(a) sin((pi * a) / 180)
#define test()                                                                 \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
using namespace std;
const int inf = (int)1e17 + 9;
int dp[102][10002];
int32_t main() {

  FASTIO;
  int n, w, s = 0;
  cin >> n >> w;
  vector<int> wt(n), val(n);
  REP(i, 0, n) {
    cin >> wt[i] >> val[i];
    s += val[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= s; j++)
      dp[i][j] = inf;
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j >= val[i - 1]) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - val[i - 1]] + wt[i - 1]);
      }
    }
  }
  int ans = -1;
  for (int j = 1; j <= s; j++) {
    if (dp[n][j] <= w) {
      ans = max(ans, j);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define pb push_back
#define pi pair<int, int>
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define F first
#define S second
#define FOR(i, r) for (int i = 0; i < r; i++)
#define REP(i, l, r) for (int i = l; i < r; i++)
#define RER(i, l, r) for (int i = l; i >= r; i--)
#define print(v)                                                               \
  for (auto i : v)                                                             \
    cout << i << ' ';
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define mem(arr, k) memset(arr, k, sizeof(arr));
#define pii acos(-1.0)
#define PI 3.1415926535897932385
#define Sin(a) sin((pi * a) / 180)
#define test()                                                                 \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
using namespace std;
const int inf = (int)1e17 + 9;
int dp[102][100002];
int32_t main() {

  FASTIO;
  int n, w, s = 0;
  cin >> n >> w;
  vector<int> wt(n), val(n);
  REP(i, 0, n) {
    cin >> wt[i] >> val[i];
    s += val[i];
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= s; j++)
      dp[i][j] = inf;
  }
  for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= s; j++) {
      dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j >= val[i - 1]) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - val[i - 1]] + wt[i - 1]);
      }
    }
  }
  int ans = -1;
  for (int j = 1; j <= s; j++) {
    if (dp[n][j] <= w) {
      ans = max(ans, j);
    }
  }
  cout << ans << endl;
  return 0;
}