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: Wrong Answer)

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

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (b)-1; i >= (a); i--)
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep(i, n) for(int i = (n)-1; i >= 0; i--)

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define pii pair<int, int>
#define vi vector<int>

using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T>& p){return o<<"("<<p.first<<","<<p.second<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T>& vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}
using ll = long long;
constexpr ll MOD = 1000000007;

constexpr int MAX = 100;
int N;
ll W;
ll w0;
ll v[MAX];
vector<ll> w[4];
vector<ll> w_sum[4];

ll ans(int n)
{
    ll val = W-n*w0;
    if(val < 0){
        return -1;
    }
    else if (val == 0){
        return 0;
    }
    ll maxi = 0;
    for(int i = 0; i < min(n+1, (int)w[0].size()+1); i++){
        const ll v1 = w_sum[0][i];
        for(int j = 0; j < min(n+1-i, (int)w[1].size()+1); j++){
            if(j > val){
                break;
            }
            const ll v2 = w_sum[1][j];
            for(int k = 0; k < min(n+1-i-j, (int)w[2].size()+1); k++){
                const int l = n - i - j - k;
                if(l <= (int)w[3].size()){
                    if(j + 2*k+3*l > val){
                        k = w[2].size();
                        break;
                    }
                    const ll v3 = w_sum[2][k];
                    const ll v4 = w_sum[3][l];
                    printf("%d %d %d %d %ld\n", i, j, k , l, v1+v2+v3+v4);
                    chmax(maxi, v1+v2+v3+v4);               
                }
            }
        }
    }
    return maxi;
}

int main()
{
    cin >> N >> W;
    rep(i, N){
        ll w_;
        cin >> w_ >> v[i];
        if(i == 0){
            w0 = w_;
            w[0].pb(v[i]);
        } else {
            w[w_-w0].pb(v[i]);
        }
    }
    sort(w[0].begin(), w[0].end(), greater<ll>());
    sort(w[1].begin(), w[1].end(), greater<ll>());
    sort(w[2].begin(), w[2].end(), greater<ll>());
    sort(w[3].begin(), w[3].end(), greater<ll>());
    for(int i = 0 ; i < 4; i++){
        w_sum[i].resize(w[i].size()+1);
    }
    rep(i, 4){
        w_sum[i][0] = 0;
    }
    rep(i, 4){
        rep1(j, static_cast<int>(w[i].size())){
            w_sum[i][j] = w_sum[i][j-1]+w[i][j-1];
        }
    }
    int inf = 0;
    int sup = N;
    while(inf < sup){
        const int mid = (inf+sup)/2;
        ll val = ans(mid);
        if(val == -1){
            sup = mid;
        } else {
            if(mid+1 == sup){
                if(ans(sup) != -1){
                    inf = sup;
                }
                break;
            }
            inf = mid;
        }
    }
    cout << ans(inf) << endl;
    return 0;
}