Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

struct nap {
  int ct, v;
  nap() {}
  nap(int ct, int v) : ct(ct), v(v) {}
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, W;
  cin >> n >> W;
  vector<nap> dp(40003, nap(0, 0));
  vector<pair<int, int>> wv(n);
  REP(i, n) cin >> wv[i].first >> wv[i].second;
  int w1 = wv[0].first;
  if (w1 > 300) {

    int wct = W / w1;
    int newW = W % w1;
    REP(i, n) wv[i].first -= w1;
    sort(wv.begin(), wv.end(), greater<pair<int, int>>());
    REP(i, n) {
      for (int j = newW - wv[i].first; j >= 0; j--) {
        if (dp[j].ct < wct) {
          if ((dp[j + wv[i].first].v < dp[j].v + wv[i].second &&
               dp[j + wv[i].first].ct == dp[j].ct + 1) ||
              (dp[j + wv[i].first].ct - 1 < dp[j].ct)) {
            dp[j + wv[i].first].v = dp[j].v + wv[i].second;
            dp[j + wv[i].first].ct = dp[j].ct + 1;
          }
        }
      }
    }
    ll ans = 0;
    REP(i, newW + 1) { ans = max((ll)dp[i].v, ans); }
    cout << ans << endl;
  } else {
    if (W > 400 * 100)
      W = 400 * 100;
    REP(i, n) {
      for (int j = W - wv[i].first; j >= 0; j--) {
        if (j == 0 || dp[j].v > 0)
          dp[j + wv[i].first].v =
              max(dp[j + wv[i].first].v, dp[j].v + wv[i].second);
      }
    }

    int ans = 0;
    REP(i, W + 1) { ans = max(dp[i].v, ans); }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

struct nap {
  int ct, v;
  nap() {}
  nap(int ct, int v) : ct(ct), v(v) {}
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, W;
  cin >> n >> W;
  vector<nap> dp(40003, nap(0, 0));
  vector<pair<int, int>> wv(n);
  REP(i, n) cin >> wv[i].first >> wv[i].second;
  int w1 = wv[0].first;
  if (w1 > 300) {

    int wct = W / w1;
    int newW = min(305, W % w1);
    REP(i, n) wv[i].first -= w1;
    sort(wv.begin(), wv.end(), greater<pair<int, int>>());
    REP(i, n) {
      for (int j = newW - wv[i].first; j >= 0; j--) {
        if (dp[j].ct < wct) {
          if ((dp[j + wv[i].first].v < dp[j].v + wv[i].second &&
               dp[j + wv[i].first].ct == dp[j].ct + 1) ||
              (dp[j + wv[i].first].ct - 1 < dp[j].ct)) {
            dp[j + wv[i].first].v = dp[j].v + wv[i].second;
            dp[j + wv[i].first].ct = dp[j].ct + 1;
          }
        }
      }
    }
    ll ans = 0;
    REP(i, newW + 1) { ans = max((ll)dp[i].v, ans); }
    cout << ans << endl;
  } else {
    if (W > 400 * 100)
      W = 400 * 100;
    REP(i, n) {
      for (int j = W - wv[i].first; j >= 0; j--) {
        if (j == 0 || dp[j].v > 0)
          dp[j + wv[i].first].v =
              max(dp[j + wv[i].first].v, dp[j].v + wv[i].second);
      }
    }

    int ans = 0;
    REP(i, W + 1) { ans = max(dp[i].v, ans); }
    cout << ans << endl;
  }
  return 0;
}