Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ll long long int
#define skip cin >> ws;
#define vll vector<ll>
#define vb vector<bool>
#define vpll vector<pair<ll, ll>>
#define vvll vector<vector<ll>>
#define pll pair<ll, ll>
#define vs vector<string>
#define vvpll vector<vector<pair<ll, ll>>>
#define pb push_back
#define pob pop_back()
#define MOD (ll)(1e9 + 7)
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)

using namespace std;

void enter(vll &ar) {
  ll n = ar.size();

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

void show(vll &a) {
  ll n = a.size();

  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}

void SieveOfErat1sthenes(int n, vll &pri) {
  vb prime(n + 1, true);

  for (int p = 2; p * p <= n; p++) {

    if (prime[p] == true) {
      pri.pb(p);

      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}

ll mo(ll a) { return a % MOD; }

bool compare(pll a, pll b) { return a.first > b.first; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, W;
  cin >> n >> W;

  vll w(n), v(n);

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

  vvll mat(W + 1, vll(n + 1));

  for (ll i = 1; i <= W; i++) {
    for (ll j = 1; j <= n; j++) {
      if (w[j - 1] > i)
        mat[i][j] = mat[i][j - 1];
      else {
        mat[i][j] = max(mat[i][j - 1], v[j - 1] + mat[i - v[j - 1]][j - 1]);
      }
    }
  }

  cout << mat[W][n] << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ll long long int
#define skip cin >> ws;
#define vll vector<ll>
#define vb vector<bool>
#define vpll vector<pair<ll, ll>>
#define vvll vector<vector<ll>>
#define pll pair<ll, ll>
#define vs vector<string>
#define vvpll vector<vector<pair<ll, ll>>>
#define pb push_back
#define pob pop_back()
#define MOD (ll)(1e9 + 7)
#define test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)

using namespace std;

void enter(vll &ar) {
  ll n = ar.size();

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

void show(vll &a) {
  ll n = a.size();

  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}

void SieveOfErat1sthenes(int n, vll &pri) {
  vb prime(n + 1, true);

  for (int p = 2; p * p <= n; p++) {

    if (prime[p] == true) {
      pri.pb(p);

      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
}

ll mo(ll a) { return a % MOD; }

bool compare(pll a, pll b) { return a.first > b.first; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, W;
  cin >> n >> W;

  vll w(n), v(n);

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

  vvll mat(W + 1, vll(n + 1));

  for (ll i = 1; i <= W; i++) {
    for (ll j = 1; j <= n; j++) {
      if (w[j - 1] > i)
        mat[i][j] = mat[i][j - 1];
      else {
        mat[i][j] = max(mat[i][j - 1], v[j - 1] + mat[i - w[j - 1]][j - 1]);
      }
    }
  }

  cout << mat[W][n] << "\n";

  return 0;
}