Problem:
Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

// clang-format off
#include <bits/stdc++.h>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (size_t i = 0; i < size_t(n); i++)
#define forn1(i,n) for (size_t i = 1; i <= size_t(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()
 
using namespace std;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

int n;
ll w;
vl th[4];

int main() {
    fastio();

    cin >> n >> w;
    ll w1, v1;
    cin >> w1 >> v1;

    th[0].push_back(v1);

    forn(i, n - 1) {
        int w, v;
        cin >> w >> v;
        th[w - w1].push_back(v);
    }

    forn(i, 4) {
        th[i].push_back(0);
        sort(th[i].begin(), th[i].end(), greater<ll>());
    }

    ll ans = 0;
    ll a = 0;
    for (int i = 0; i < th[0].size(); i++) {
        ll b = 0;
        for (int j = 0; j < th[1].size(); j++) {
            ll c = 0;
            for (int k = 0; k < th[2].size(); k++) {
                ll d = 0;
                for (int l = 0; l < th[3].size(); l++) {
                    ll wtotal = 1LL * i * w1 + 1LL * j * (w1 + 1) + 1LL * k * (w1 + 2) + 1LL * l * (w1 + 3);

                    if (wtotal > w) continue;

                    chmax(ans, a + b + c + d);

                    d += th[3][l];
                }

                c += th[2][k];
            }

            b += th[1][j];
        }

        a += th[0][i];
    }

    cout << ans << endl;

    return 0;
}