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<bits/stdc++.h>
using namespace std;

int main()
{
    int nItem, index = 0;
    long long bagWeight, baseWeight, tmpWeight, tmpVal;
    cin>>nItem>>bagWeight;
    vector<long long> vArr0, vArr1, vArr2, vArr3;
    cin>>baseWeight>>tmpVal;
    vArr0.push_back(tmpVal);
    index ++;
    while(index < nItem)
    {
        cin>>tmpWeight>>tmpVal;
        int tmpIndex = int(tmpWeight - baseWeight);
        if(tmpIndex == 0) vArr0.push_back(tmpVal);
        else if(tmpIndex == 1) vArr1.push_back(tmpVal);
        else if(tmpIndex == 2) vArr2.push_back(tmpVal);
        else if(tmpIndex == 3) vArr3.push_back(tmpVal);
        index ++;
    }
    
    sort(vArr0.begin(), vArr0.end());
    sort(vArr1.begin(), vArr1.end());
    sort(vArr2.begin(), vArr2.end());
    sort(vArr3.begin(), vArr3.end());
    
    int len0 = int(vArr0.size()), len1 = int(vArr1.size()), len2 = int(vArr2.size()), len3 = int(vArr3.size());
    
    long long maxVal = 0, tmpV0 = 0, tmpV1 = 0, tmpV2 = 0, tmpV3 = 0;
    for(int i0 = 0; i0<len0 + 1 && (i0 * baseWeight) <= bagWeight; i0++)
    {
        long long tmp0 = i0 * baseWeight;
        tmpV0 = (i0 == 0 ? 0: tmpV0 + vArr0[i0 - 1]);
        
        for(int i1 = 0; i1 <= len1 && i1<= ((bagWeight - tmp0) >= 0 ? (bagWeight - tmp0): 0)/(baseWeight + 1); i1 ++)
        {
            long long tmp1 = i1 * (baseWeight + 1);
            tmpV1 = (i1 == 0 ? 0: tmpV1 + vArr1[i1 - 1]);
            
            for(int i2 = 0; i2<= len2 && i2<=((bagWeight - tmp0 - tmp1)>= 0? (bagWeight - tmp0 - tmp1): 0)/(baseWeight + 2); i2++)
            {
                long long tmp2 = i2 * (baseWeight + 2);
                tmpV2 = (i2 == 0 ? 0: tmpV2 + vArr2[i2 - 1]);
                
                for(int i3 = 0; i3<= len3 && i3<= ((bagWeight - tmp0 - tmp1 - tmp2)>= 0? (bagWeight - tmp0 - tmp1 - tmp2): 0)/(baseWeight + 3); i3++)
                {
                    tmpV3 = (i3 == 0 ? 0: tmpV3 + vArr3[i3 - 1]);
                    
                    tmpVal = tmpV0 + tmpV1 + tmpV2 + tmpV3;
                    if(tmpVal > maxVal) maxVal = tmpVal;
                }
            }
        }
    }
    cout<< maxVal<<endl;
    return 0;
}