Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\n"
#define pb push_back
#define sq(a) (a) * (a)
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \
  cerr << #v << " : ";                                                         \
  for (auto x : v)                                                             \
    cerr << x << ' ';                                                          \
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1 == 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n >> 1;
  }
  return result;
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS

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

  ll v[n], wt[n];

  ll tot = 0;

  for (ll i = 0; i < n; i++) {
    cin >> wt[i] >> v[i];
    tot += v[i];
  }

  ll ans = 0;

  vector<ll> min(tot, -1);

  tot = 0;
  min[0] = 0;

  for (ll i = 0; i < n; i++) {
    tot += v[i];

    for (ll j = tot; j >= v[i]; j--) {
      if (min[j - v[i]] != -1) {
        ll req = min[j - v[i]] + wt[i];
        if (req <= w)
          ans = max(ans, j);
        if (min[j] == -1 || min[j] > req)
          min[j] = req;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long int ull;
#define endl "\n"
#define pb push_back
#define sq(a) (a) * (a)
#define debug(x) cerr << #x << '=' << (x) << endl;
#define debugv(v)                                                              \
  cerr << #v << " : ";                                                         \
  for (auto x : v)                                                             \
    cerr << x << ' ';                                                          \
  cerr << endl;
#define MOD 1000000007
#define PI 3.141592653589793238
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll exp(ll x, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1 == 1)
      result = (result * x) % mod;
    x = (x * x) % mod;
    n = n >> 1;
  }
  return result;
}

int32_t main() {
  IOS

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

  ll v[n], wt[n];

  ll tot = 0;

  for (ll i = 0; i < n; i++) {
    cin >> wt[i] >> v[i];
    tot += v[i];
  }

  ll ans = 0;

  vector<ll> min(tot, -1);

  tot = 0;
  min[0] = 0;

  for (ll i = 0; i < n; i++) {
    tot += v[i];

    for (ll j = tot; j >= v[i]; j--) {
      if (min[j - v[i]] != -1) {
        ll req = min[j - v[i]] + wt[i];
        if (req <= w)
          ans = max(ans, j);
        if (min[j] == -1 || min[j] > req)
          min[j] = req;
      }
    }
  }

  cout << ans << endl;

  return 0;
}