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)

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <functional>

#define reps(i,s,n) for(int (i) = (s); (i) < (n) ;(i)++)
#define rep(i,n) reps(i,0,n)
using namespace std;
using ll = long long int;
const int INF = 1L << 50;
int main(){
    int N,W;
    cin >> N >> W;
    int w0,v0,weight,value;
    vector<int> w1,w2,w3,w4;
    vector<int> w(N,0), v(N,0);
    vector<ll> s1(N+2,0),s2(N+2,0),s3(N+2,0),s4(N+2,0);
    cin >> w0;
    cin >> v0;
    w1.push_back(v0);
    rep(i,N-1){
        cin >> weight;
        cin >> value;
        switch(weight-w0){
            case 0:
                w1.push_back(value);
                break;
            case 1:
                w2.push_back(value);
                break;
            case 2:
                w3.push_back(value);
                break;
            case 3:
                w4.push_back(value);
                break;
        }
    }
    
    sort(w1.begin(),w1.end(),greater<>());
    sort(w2.begin(),w2.end(),greater<>());
    sort(w3.begin(),w3.end(),greater<>());
    sort(w4.begin(),w4.end(),greater<>());
    
    if(1 <= w1.size()){
        s1[1] = w1[0];
        reps(i,1,w1.size()) s1[i+1] += (s1[i] + w1[i]);
    }

    if(1 <= w2.size()){
        s2[1] = w2[0];
        reps(i,1,w2.size())s2[i+1] += (s2[i] + w2[i]);
    }

    if(1 <= w3.size()){
        s3[1] = w3[0];
        reps(i,1,w3.size())s3[i+1] += (s3[i] + w3[i]);
    }

    if(1 <= w4.size()){
        s4[1] = w4[0];
        reps(i,1,w4.size())s4[i+1] += (s4[i] + w4[i]);
    }

    
    int ans = s1[0]+s2[0]+s3[0]+s4[0];
    reps(i,1,w1.size()+2){
        reps(j,1,w2.size()+2){
            reps(k,1,w3.size()+2){
                reps(l,1,w4.size()+2){
                   if(ans < s1[i-1] + s2[j-1] + s3[k-1] +s4[l-1]){
                        if((i+j+k+l-4)*w0 + (1*(j-1)+2*(k-1)+3*(l-1)) <= W){
                            ans = s1[i-1] + s2[j-1] + s3[k-1] + s4[l-1];
                        }
                    }
                }
            }
        }
    }
    cout << ans << endl;

    return 0;
}