Problem p03164 (C++)

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

// #define int             long long
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;

int32_t main() {
  // c_p_c();
  int n, w;
  cin >> n >> w;
  vi a(n + 1);
  vi v(n + 1);
  int maxvalue = n * 100;
  int dp[n + 1][maxvalue + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> v[i];
  }
  // for(int i=1;i<=n;i++)cout<<a[i]<<" "<<v[i]<<endl;

  for (int i = 0; i <= maxvalue; i++)
    dp[1][i] = inf;
  dp[1][0] = 0;
  dp[1][v[1]] = a[1];
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= maxvalue; j++) {
      if (v[i] <= j) {
        dp[i][j] = dp[i - 1][j];
        dp[i][j] = min(dp[i][j], a[i] + dp[i - 1][j - v[i]]);
      } else
        dp[i][j] = dp[i - 1][j];
      // cout<<dp[i][j]<<" ";
    } // cout<<endl;
  }
  int ans = 0;
  for (int i = 0; i <= maxvalue; i++) {
    if (dp[n][i] <= w)
      ans = i;
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

// #define int             long long
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update> pbds;

int32_t main() {
  // c_p_c();
  int n, w;
  cin >> n >> w;
  vi a(n + 1);
  vi v(n + 1);
  int maxvalue = n * 1000;
  int dp[n + 1][maxvalue + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> v[i];
  }
  // for(int i=1;i<=n;i++)cout<<a[i]<<" "<<v[i]<<endl;

  for (int i = 0; i <= maxvalue; i++)
    dp[1][i] = inf;
  dp[1][0] = 0;
  dp[1][v[1]] = a[1];
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j <= maxvalue; j++) {
      if (v[i] <= j) {
        dp[i][j] = dp[i - 1][j];
        dp[i][j] = min(dp[i][j], a[i] + dp[i - 1][j - v[i]]);
      } else
        dp[i][j] = dp[i - 1][j];
      // cout<<dp[i][j]<<" ";
    } // cout<<endl;
  }
  int ans = 0;
  for (int i = 0; i <= maxvalue; i++) {
    if (dp[n][i] <= w)
      ans = i;
  }
  cout << ans;
  return 0;
}