Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (ll i = start; i < end; i++)
#define N 100001
#define all(v) v.begin(), v.end()
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << " ";                                                       \
  nl
int main() {
  fastio();
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  ll n, w, smv;
  cin >> n >> w;

  vector<ll> a(n + 1), v(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i + 1] >> v[i + 1], smv += v[i + 1];

  vector<ll> dp(smv + 10, 1e18);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = smv - v[i]; j >= 0; j--) {
      dp[j + v[i]] = min(dp[j] + a[i], dp[j + v[i]]);
    }
  }
  for (int i = smv; i >= 0; i--)
    if (dp[i] <= w) {
      cout << i;
      return 0;
    }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define test()                                                                 \
  ull t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define pb push_back
#define mkp make_pair
#define nl cout << endl
#define MOD 1000000007
#define loop(i, start, end) for (ll i = start; i < end; i++)
#define N 100001
#define all(v) v.begin(), v.end()
#define oa(a, n)                                                               \
  for (int i = 0; i < n; i++)                                                  \
    cout << a[i] << " ";                                                       \
  nl
#define ov(a)                                                                  \
  for (int i = 0; i < a.size(); i++)                                           \
    cout << a[i] << " ";                                                       \
  nl
int main() {
  fastio();

  ll n, w, smv;
  cin >> n >> w;

  vector<ll> a(n + 1), v(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i + 1] >> v[i + 1], smv += v[i + 1];

  vector<ll> dp(smv + 10, 1e18);
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = smv - v[i]; j >= 0; j--) {
      dp[j + v[i]] = min(dp[j] + a[i], dp[j + v[i]]);
    }
  }
  for (int i = smv; i >= 0; i--)
    if (dp[i] <= w) {
      cout << i;
      return 0;
    }

  return 0;
}