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 <string>
#include <map>
#include <vector>
#include <set>
#include <iomanip>
#include <stack>
#include <fstream>
#include <cstdint>
#include <cmath>
#include <algorithm>
#include <utility>
#include <numeric>
#include <functional>
using namespace std;

constexpr int INF       = 1000000000;/* 1e+9a */
constexpr int MODULO    = 1000000007;

typedef int64_t ll;

constexpr int dx[4] = { 1, 0, -1, 0 };
constexpr int dy[4] = { 0, 1, 0, -1 };

#define SPRE(x) setprecision(x) // 精度を指定

int main()
{
	cin.tie(0);
    ios::sync_with_stdio(false);
	int N, W;
	vector<vector<ll>> v(4,vector<ll>());
	cin >> N >> W;
	int w1, tmp;
	cin >> w1 >> tmp;
	v[0].push_back(tmp);
	for(int i = 1; i < N; ++i) {
		int w;
		cin >> w >> tmp;
		v[w-w1].push_back(tmp);
	}
	for(int i = 0; i < 4; ++i)
		sort(v[i].rbegin(), v[i].rend());

	ll maxim = 0;
	for(int a = 0; a <= v[0].size(); ++a) {
		ll suma = 0;
		int aw = a * w1;
		if(aw <= W) {
			for(int i = 0; i < a; ++i)
				suma += v[0][i];
			/* cerr << "suma " << suma << '\n'; */
			for(int b = 0; b <= v[1].size(); ++b) {
				ll sumb = 0;
				int bw = b * (w1 + 1);
				if(aw + bw <= W) {
					for(int i = 0; i < b; ++i)
						sumb += v[1][i];
					/* cerr << "sumb " << sumb << '\n'; */
					for(int c = 0; c <= v[2].size(); ++c) {
						ll sumc = 0;
						int cw = c * (w1 + 2);
						if(aw + bw + cw <= W) {
							for(int i = 0; i < c; ++i)
								sumc += v[2][i];
							int le = (W - aw - bw - cw)/(w1+3);
							le = min(le, int(v[3].size()));
							for(int i = 0; i < le ; ++i){
								sumc += v[3][i];
							}
							/* cerr << "sumc " << sumc << '\n'; */
							maxim = max(maxim, suma+sumb+sumc);
						}
					}
				}
			}
		}
	}
	cout << maxim << '\n';
}