Problem p03163 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rloop(i, a, b) for (int i = a; i > b; i--)
#define loopll(i, a, b) for (long long int i = a; i < b; i++)
#define rloopll(i, a, b) for (long long int i = a; i > b; i--)
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mk make_pair
#define intmin INT_MIN
#define intmax INT_MAX
#define cout(x) cout << x << " ";
using namespace std;
void vec(vector<int> &v) {

  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
void vec_ll(vector<long long int> &v) {

  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
int parsestring(string temp) {
  int num = 0;
  int digs = temp.size() - 1;
  for (int i = 0; i <= digs; i++) {
    num += pow(10, digs - i) * (temp[i] - '0');
  }
  return num;
}

int mindig(string &s) {
  int temp = INT_MAX;
  //    int index = -1;
  for (int i = 0; i < s.size(); i++) {
    if (temp > s[i] - '0') {
      temp = s[i] - '0';
      //            index = i;
    }
  }
  return temp;
}
//}
ll power(ll base, ll exp) {
  ll ans = 1;
  for (ll i = 1; i <= exp; i++) {
    ans *= base;
  }
  return ans;
}
//}
//
void factor(ll n, set<ll> &s) {
  ll x = 2;
  while (n != 1) {
    if (n % x == 0) {
      s.insert(x);
      n /= x;
    } else {
      x++;
    }
  }
}

bool prime(int n) {
  bool ok = true;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ok = false;
      break;
    }
  }
  return ok;
}
pair<ll, ll> ass(pair<ll, ll> &a, pair<ll, ll> &b) {
  pair<ll, ll> x;
  x.first = -1e10;
  x.second = -1e10;
  if (a.second < b.first) {
    return x;
  }
  if (b.second < a.first) {
    return x;
  }
  if (a.first <= b.first) {
    x.first = b.first;
    x.second = min(a.second, b.second);
  } else {
    x.first = a.first;
    x.second = min(a.second, b.second);
  }
  return x;
}
int MX = 100005;
vector<vector<int>> adj(MX, vector<int>());
vector<int> subt(MX, 1);
vector<bool> vis(MX, false);
vector<bool> vis2(MX, false);
vector<int> neigh(MX, 0);

int ans = 0;
int dfs(int x) {
  if (vis[x]) {
    return 0;
  }
  vis[x] = true;
  for (int i = 0; i < adj[x].size(); i++) {
    subt[x] += dfs(adj[x][i]);
  }
  return subt[x];
}

void dfs2(int x) {
  if (vis2[x]) {
    return;
  }
  vis2[x] = true;
  for (int i = 0; i < adj[x].size(); i++) {
    if (neigh[x] > 1 && subt[adj[x][i]] % 2 == 0 && !vis2[adj[x][i]]) {
      cout << x << " " << adj[x][i] << " " << neigh[x] << " " << subt[adj[x][i]]
           << endl;
      ans++;
      neigh[x]--;
      neigh[adj[x][i]]--;
      //            cout<<"a "<< x<< " " <<adj[x][i]<<endl;
    }
    dfs(adj[x][i]);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, w;
  cin >> n >> w;
  vector<pair<bool, ll>> poss(1e5 + 10, mk(false, 0));

  poss[0].first = true;
  for (int i = 0; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    for (int j = w; j >= 0; j--) {
      if (poss[j].first) {
        poss[j + u].first = true;
        poss[j + u].second = max(poss[j + u].second, poss[j].second + v);
      }
    }
  }
  ll ans = 0;

  for (int j = w; j >= 0; j--) {
    if (poss[j].first) {
      ans = max(ans, poss[j].second);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rloop(i, a, b) for (int i = a; i > b; i--)
#define loopll(i, a, b) for (long long int i = a; i < b; i++)
#define rloopll(i, a, b) for (long long int i = a; i > b; i--)
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mk make_pair
#define intmin INT_MIN
#define intmax INT_MAX
#define cout(x) cout << x << " ";
using namespace std;
void vec(vector<int> &v) {

  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
void vec_ll(vector<long long int> &v) {

  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
int parsestring(string temp) {
  int num = 0;
  int digs = temp.size() - 1;
  for (int i = 0; i <= digs; i++) {
    num += pow(10, digs - i) * (temp[i] - '0');
  }
  return num;
}

int mindig(string &s) {
  int temp = INT_MAX;
  //    int index = -1;
  for (int i = 0; i < s.size(); i++) {
    if (temp > s[i] - '0') {
      temp = s[i] - '0';
      //            index = i;
    }
  }
  return temp;
}
//}
ll power(ll base, ll exp) {
  ll ans = 1;
  for (ll i = 1; i <= exp; i++) {
    ans *= base;
  }
  return ans;
}
//}
//
void factor(ll n, set<ll> &s) {
  ll x = 2;
  while (n != 1) {
    if (n % x == 0) {
      s.insert(x);
      n /= x;
    } else {
      x++;
    }
  }
}

bool prime(int n) {
  bool ok = true;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ok = false;
      break;
    }
  }
  return ok;
}
pair<ll, ll> ass(pair<ll, ll> &a, pair<ll, ll> &b) {
  pair<ll, ll> x;
  x.first = -1e10;
  x.second = -1e10;
  if (a.second < b.first) {
    return x;
  }
  if (b.second < a.first) {
    return x;
  }
  if (a.first <= b.first) {
    x.first = b.first;
    x.second = min(a.second, b.second);
  } else {
    x.first = a.first;
    x.second = min(a.second, b.second);
  }
  return x;
}
int MX = 100005;
vector<vector<int>> adj(MX, vector<int>());
vector<int> subt(MX, 1);
vector<bool> vis(MX, false);
vector<bool> vis2(MX, false);
vector<int> neigh(MX, 0);

int ans = 0;
int dfs(int x) {
  if (vis[x]) {
    return 0;
  }
  vis[x] = true;
  for (int i = 0; i < adj[x].size(); i++) {
    subt[x] += dfs(adj[x][i]);
  }
  return subt[x];
}

void dfs2(int x) {
  if (vis2[x]) {
    return;
  }
  vis2[x] = true;
  for (int i = 0; i < adj[x].size(); i++) {
    if (neigh[x] > 1 && subt[adj[x][i]] % 2 == 0 && !vis2[adj[x][i]]) {
      cout << x << " " << adj[x][i] << " " << neigh[x] << " " << subt[adj[x][i]]
           << endl;
      ans++;
      neigh[x]--;
      neigh[adj[x][i]]--;
      //            cout<<"a "<< x<< " " <<adj[x][i]<<endl;
    }
    dfs(adj[x][i]);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, w;
  cin >> n >> w;
  vector<pair<bool, ll>> poss(1e5 + 10, mk(false, 0));

  poss[0].first = true;
  for (int i = 0; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    for (int j = w; j >= 0; j--) {
      if (poss[j].first) {
        if (j + u < poss.size()) {
          poss[j + u].first = true;
          poss[j + u].second = max(poss[j + u].second, poss[j].second + v);
        }
      }
    }
  }
  ll ans = 0;

  for (int j = w; j >= 0; j--) {
    if (poss[j].first) {
      ans = max(ans, poss[j].second);
    }
  }
  cout << ans << endl;
}