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)

#include <bits/stdc++.h>

using namespace std;

#define ll long long

//#define TEST

signed main(){
    int n,w;
    cin >> n >> w;
    int* weight = new int[n];
    int* value = new int[n];
    for (int i = 0;i < n;++i)
        cin >> weight[i] >> value[i];

    int w1 = weight[0];
    vector<int> items1; // 重さw1のitemのvalue
    vector<int> items2; // 重さw1+1のitemのvalue
    vector<int> items3; // 重さw1+2のitemのvalue
    vector<int> items4; // 重さw1+3のitemのvalue
    for (int i = 0;i < n;++i){
        if (weight[i] == w1)
            items1.push_back(value[i]);
        else if (weight[i] == w1 + 1)
            items2.push_back(value[i]);
        else if (weight[i] == w1 + 2)
            items3.push_back(value[i]);
        else if (weight[i] == w1 + 3)
            items4.push_back(value[i]);
    }
#ifdef TEST
    cout << "--------items1--------" << endl;
    for (auto wi : items1)
        cout << wi << " ";
    cout << endl;
#endif

    sort(items1.begin(),items1.end());
#ifdef TEST
    cout << "----items1(sorted)----" << endl;
    for (auto wi : items1)
        cout << wi << " ";
    cout << endl;
#endif
    sort(items2.begin(),items2.end());
    sort(items3.begin(),items3.end());
    sort(items4.begin(),items4.end());

    vector<ll> m1{0}; // m1[i] : 重さw1の中からi個取るときの最大値
    vector<ll> m2{0}; // m2[i] : 重さw1+1の中からi個取るときの最大値
    vector<ll> m3{0}; // m3[i] : 重さw1+2の中からi個取るときの最大値
    vector<ll> m4{0}; // m4[i] : 重さw1+3の中からi個取るときの最大値

    for (int i = items1.size() - 1;i >= 0;--i){
        auto last = m1.end() - 1;
        m1.push_back(*last + items1[i]);
    }
#ifdef TEST
    cout << "----------m1----------" << endl;
    for (auto wi : m1)
        cout << wi << " ";
    cout << endl;
#endif
    for (int i = items2.size() - 1;i >= 0;--i){
        auto last = m2.end() - 1;
        m2.push_back(*last + items2[i]);
    }
    for (int i = items3.size() - 1;i >= 0;--i){
        auto last = m3.end() - 1;
        m3.push_back(*last + items3[i]);
    }
    for (int i = items4.size() - 1;i >= 0;--i){
        auto last = m4.end() - 1;
        m4.push_back(*last + items4[i]);
    }


    ll ans = 0;
    for (int i = 0;i < m1.size();++i){
        for (int j = 0;j < m2.size();++j){
            for (int k = 0;k < m3.size();++k){
                for (int l = 0;l < m4.size();++l){
                    ll wei = 0;
                    wei += (ll)w1 * i;
                    wei += ((ll)w1 + 1) * j;
                    wei += ((ll)w1 + 2) * k;
                    wei += ((ll)w1 + 3) * l;
                    if (wei <= w)
                        ans = max(ans,m1[i] + m2[j] + m3[k] + m4[l]);
                }
            }
        }
    }

    cout << ans << endl;

    return 0;
}