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 <fstream>
#include <string> 
#include <cmath>  
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <functional>
using namespace std;
using ll = long long;

int main() {
	int N;
	ll W,w0,w,v,n0,n1,n2,n3,Wsum,Vsum;
	cin >> N >> W;
	vector<pair<ll, ll> > pairs0, pairs1, pairs2, pairs3;
	vector<ll> v0, v1, v2, v3, Vmax;
	cin >> w0 >> v;
	pairs0.push_back(make_pair(w0, v));

	for (int i = 1; i < N; i++) {
		cin >> w >> v;
		if (w == w0) pairs0.push_back(make_pair(w, v));
		if (w == w0 + 1) pairs1.push_back(make_pair(w, v));
		if (w == w0 + 2) pairs2.push_back(make_pair(w, v));
		if (w == w0 + 3) pairs3.push_back(make_pair(w, v));
	}

	if (w0 > W) {
		cout << 0 << endl;
		return 0;
	}

	sort(pairs0.begin(), pairs0.end());
	sort(pairs1.begin(), pairs1.end());
	sort(pairs2.begin(), pairs2.end());
	sort(pairs3.begin(), pairs3.end());
	
	n0 = min(W / w0, (ll)pairs0.size());
	n1 = min(W / (w0 + 1), (ll)pairs1.size());
	n2 = min(W / (w0 + 2), (ll)pairs2.size());
	n3 = min(W / (w0 + 3), (ll)pairs3.size());
	
	for (ll i = 0; i < n0; i++) { v0.push_back(pairs0[i].second); }
	for (ll i = 0; i < n1; i++) { v1.push_back(pairs1[i].second); }
	for (ll i = 0; i < n2; i++) { v2.push_back(pairs2[i].second); }
	for (ll i = 0; i < n3; i++) { v3.push_back(pairs3[i].second); }

	for (ll i = 0; i < n0 + 1; i++) {
		for (ll j = 0; j < n1 + 1; j++) {
			for (ll k = 0; k < n2 + 1; k++) {
				for (ll l = 0; l < n3 + 1; l++) {
					Wsum = i*w0 + j*(w0 + 1) + k*(w0 + 2) + l*(w0 + 3);
					if (Wsum <= W) {
						Vsum = 0;
						cout << i << "," << j << "," << k << "," << l << endl;
						if (i != 0) Vsum += accumulate(v0.begin(), v0.begin() + i, 0);
						if (j != 0) Vsum += accumulate(v1.begin(), v1.begin() + j, 0);
						if (k != 0) Vsum += accumulate(v2.begin(), v2.begin() + k, 0);
						if (l != 0) Vsum += accumulate(v3.begin(), v3.begin() + l, 0);
						Vmax.push_back(Vsum);
					}
				}
			}
		}
	}
	sort(Vmax.begin(), Vmax.end(), greater<int>());
	cout << Vmax[0] << endl;
	return 0;
}