Problem p03734 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

// output
#define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
const int INF = 1e18;

using namespace std;

int N, W, w, v, num;
vector<vector<int>> vec(4);
int f(int a, int b, int c, int d) {
  int res = 0;
  vector<int> v = {a, b, c, d};
  REP(i, 4) chmin(v[i], (int)vec[i].size());
  REP(i, 4) {
    REP(j, v[i]) { res += vec[i][j]; }
  }
  return res;
}

signed main() {
  cin >> N >> W;

  REP(i, N) {
    cin >> w >> v;
    if (i == 0)
      num = w;
    vec[w - num].push_back(v);
  }

  REP(i, N) sort(ALL(vec[i]), greater<int>());

  int ans = 0;
  REP(i, N + 1) {
    if (num * i > W)
      break;
    REP(j, N + 1) {
      if (num * i + (num + 1) * j > W)
        break;
      REP(k, N + 1) {
        if (num * i + (num + 1) * j + (num + 2) * k > W)
          break;
        REP(l, N + 1) {
          if (num * i + (num + 1) * j + (num + 2) * k + (num + 3) * l > W)
            break;
          ans = chmax(ans, f(i, j, k, l));
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

// output
#define SPBR(w, n) std::cout << (w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
const int INF = 1e18;

using namespace std;

int N, W, w, v, num;
vector<vector<int>> vec(4);
int f(int a, int b, int c, int d) {
  int res = 0;
  vector<int> v = {a, b, c, d};
  REP(i, 4) chmin(v[i], (int)vec[i].size());
  REP(i, 4) {
    REP(j, v[i]) { res += vec[i][j]; }
  }
  return res;
}

signed main() {
  cin >> N >> W;

  REP(i, N) {
    cin >> w >> v;
    if (i == 0)
      num = w;
    vec[w - num].push_back(v);
  }

  REP(i, 4) sort(ALL(vec[i]), greater<int>());

  int ans = 0;
  REP(i, N + 1) {
    if (num * i > W)
      break;
    REP(j, N + 1) {
      if (num * i + (num + 1) * j > W)
        break;
      REP(k, N + 1) {
        if (num * i + (num + 1) * j + (num + 2) * k > W)
          break;
        REP(l, N + 1) {
          if (num * i + (num + 1) * j + (num + 2) * k + (num + 3) * l > W)
            break;
          ans = chmax(ans, f(i, j, k, l));
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}