Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define vv vector
#define pp pair
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
typedef pair<ll, ll> pll;
typedef vv<ll> vll;
typedef vv<pll> vpll;
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// adds two more functions to set
//(1)*(set.find_by_order(k))[kth element in the sorted set]
//(2)set.order_of_key(k)[count of elements strictly less than k]
#define LG(args...)                                                            \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    err(istream_iterator<string>(_ss), args);                                  \
    cerr << endl;                                                              \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "(" << *it << ":" << a << ") ";
  err(++it, args...);
}
//:::::::::::::::::::::::::::::::::::::::::::::
const ll INF = 9e18;
const ll MOD = 1e9 + 7;
// #define dbg

#define val ff
#define wt ss
// #define MXN 109
// #define MXW 100009
ll n, W;
vpll v;

int main() {
  fastio;
  // for(auto& i: dp){
  // 	for(auto& j: i){
  // 		j= INF;
  // 	}
  // }
  cin >> n >> W;
  v.resize(n);
  ll wmax = 0;
  for (auto &i : v) {
    cin >> i.wt >> i.val;
    wmax += i.wt;
  }

  // so need for dp with state elements allowed and weight
  // hence to save space make only 1D dp
  // dp[i] at any time is the maximum value u can acheive with
  //        exactly i as weight
  // So firstly only 1 item is allowed and then allowed lost grows to
  // n size.
  // dp state will be like dp[wt_already + w[i]] = max(self, dp[wt_already] +
  // val[i]) makes sense but the problem is order of execution so notice that
  // though dp is 1D but there id a hidden state of allowed elements hence the
  // dp[wt_already] should be the one when allowed items were till i-1 so when
  // we add another item and inc it by val[i] we will be eligible to call it a
  // state for i allowed items Since allowed items increase hence loop goes
  // for(i :0-> n-1) now notice to update a dp of higher weight we need dp state
  // of previuos i in lower dp states hence update larger wights first hence rev
  // for loop for weights see possible updates in weight can be wmax - w[i] to
  // zero , becuase its the range of wt_already

  vll dp(wmax + 1, 0);

  auto maxself = [](ll &a, ll b) { a = max(a, b); };

  for (ll i = 0; i < n; i++) {
    for (ll j = wmax - v[i].wt; j >= 0; j--) {
      maxself(dp[j + v[i].wt], dp[j] + v[i].val);
    }
  }

  ll ans = 0;
  for (ll i = 0; i <= W; i++) {
    maxself(ans, dp[i]);
    // LG(i,dp[i])
  }
  cout << ans;
}

Fixed submission:
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define vv vector
#define pp pair
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
typedef pair<ll, ll> pll;
typedef vv<ll> vll;
typedef vv<pll> vpll;
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// adds two more functions to set
//(1)*(set.find_by_order(k))[kth element in the sorted set]
//(2)set.order_of_key(k)[count of elements strictly less than k]
#define LG(args...)                                                            \
  {                                                                            \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    err(istream_iterator<string>(_ss), args);                                  \
    cerr << endl;                                                              \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "(" << *it << ":" << a << ") ";
  err(++it, args...);
}
//:::::::::::::::::::::::::::::::::::::::::::::
const ll INF = 9e18;
const ll MOD = 1e9 + 7;
// #define dbg

#define val ff
#define wt ss
// #define MXN 109
// #define MXW 100009
ll n, W;
vpll v;

int main() {
  fastio;
  // for(auto& i: dp){
  // 	for(auto& j: i){
  // 		j= INF;
  // 	}
  // }
  cin >> n >> W;
  v.resize(n);
  ll wmax = 0;
  for (auto &i : v) {
    cin >> i.wt >> i.val;
    wmax += i.wt;
  }

  // so need for dp with state elements allowed and weight
  // hence to save space make only 1D dp
  // dp[i] at any time is the maximum value u can acheive with
  //        exactly i as weight
  // So firstly only 1 item is allowed and then allowed lost grows to
  // n size.
  // dp state will be like dp[wt_already + w[i]] = max(self, dp[wt_already] +
  // val[i]) makes sense but the problem is order of execution so notice that
  // though dp is 1D but there id a hidden state of allowed elements hence the
  // dp[wt_already] should be the one when allowed items were till i-1 so when
  // we add another item and inc it by val[i] we will be eligible to call it a
  // state for i allowed items Since allowed items increase hence loop goes
  // for(i :0-> n-1) now notice to update a dp of higher weight we need dp state
  // of previuos i in lower dp states hence update larger wights first hence rev
  // for loop for weights see possible updates in weight can be wmax - w[i] to
  // zero , becuase its the range of wt_already

  vll dp(wmax + 1, 0);

  auto maxself = [](ll &a, ll b) { a = max(a, b); };

  for (ll i = 0; i < n; i++) {
    for (ll j = wmax - v[i].wt; j >= 0; j--) {
      maxself(dp[j + v[i].wt], dp[j] + v[i].val);
    }
  }

  ll ans = 0;
  for (ll i = 0; i <= min(W, wmax); i++) {
    maxself(ans, dp[i]);
    // LG(i,dp[i])
  }
  cout << ans;
}