Problem p03164 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;
typedef long double ldouble;

typedef vector<llong> vecllong;
typedef vector<vecllong> vvecllong;

template <class T> inline bool chmin(T &to, T compare) {
  if (to > compare) {
    to = compare;
    return true;
  }
  return false;
};

template <class T> inline bool chmax(T &to, T compare) {
  if (to < compare) {
    to = compare;
    return true;
  }
  return false;
};

const llong MOD = 1e9 + 7;
const llong INF = 1e18;

#define FOR(i, n) for (llong i = 0; i < n; i++)
#define FORS(i, a, b) for (llong i = a; i < b; i++)
#define FORR(i, n) for (llong i = n; i > 0; i++)

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  llong N, W;
  cin >> N >> W;

  vecllong weight(N + 1, 0);
  vecllong value(N + 1, 0);
  llong maxValue = 0;

  FOR(i, N) {
    cin >> weight[i] >> value[i];
    maxValue += weight[i];
  }

  vvecllong dp(N + 1, vecllong(maxValue + 1, INF)); // dp[i][value] = minWeight
  dp[0][0] = 0;

  FOR(i, N) {
    FOR(v, maxValue + 1) {
      if (v - value[i] >= 0) {
        dp[i + 1][v] =
            min({dp[i][v - value[i]] + weight[i], dp[i][v], dp[i + 1][v]});
      } else {
        dp[i + 1][v] = dp[i][v];
      }
    }
  }

  llong ans = 0;

  for (llong v = maxValue; v >= 0; v--) {
    if (dp[N][v] <= W) {
      ans = v;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;
typedef long double ldouble;

typedef vector<llong> vecllong;
typedef vector<vecllong> vvecllong;

template <class T> inline bool chmin(T &to, T compare) {
  if (to > compare) {
    to = compare;
    return true;
  }
  return false;
};

template <class T> inline bool chmax(T &to, T compare) {
  if (to < compare) {
    to = compare;
    return true;
  }
  return false;
};

const llong MOD = 1e9 + 7;
const llong INF = 1e18;

#define FOR(i, n) for (llong i = 0; i < n; i++)
#define FORS(i, a, b) for (llong i = a; i < b; i++)
#define FORR(i, n) for (llong i = n; i > 0; i++)

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  llong N, W;
  cin >> N >> W;

  vecllong weight(N + 1, 0);
  vecllong value(N + 1, 0);
  llong maxValue = 0;

  FOR(i, N) {
    cin >> weight[i] >> value[i];
    maxValue += value[i];
  }

  vvecllong dp(N + 1, vecllong(maxValue + 1, INF)); // dp[i][value] = minWeight
  dp[0][0] = 0;

  FOR(i, N) {
    FOR(v, maxValue + 1) {
      if (v - value[i] >= 0) {
        dp[i + 1][v] =
            min({dp[i][v - value[i]] + weight[i], dp[i][v], dp[i + 1][v]});
      } else {
        dp[i + 1][v] = dp[i][v];
      }
    }
  }

  llong ans = 0;

  for (llong v = maxValue; v >= 0; v--) {
    if (dp[N][v] <= W) {
      ans = v;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}