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 <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <assert.h>
#include <deque>
using namespace std;

typedef unsigned long long UL;
typedef long long LL;
#define LP(i, a, b) for (int i = int(a); i < int(b); i++)
#define LPE(i, a, b) for (int i = int(a); i <= int(b); i++)
typedef pair<int, int> PII;
typedef vector<vector<PII> > WAL;
typedef vector<vector<int> > SAL;
#define Ep 1e-11

#define INF 1e16

const int MaxSize = 100 + 10;
int N, W;
int size[4];
LL vs[4][MaxSize];
LL ans = 0;
LL w1 = -1;

void calc(int si, LL rW, LL pV) {

	if (si < 3)
		calc(si + 1, rW, pV); //no s

	LP(i, 0, size[si])
	{
		LL curW = (w1 + si) * (i + 1);

		if (curW > rW)
			continue;

		LL curV = vs[si][i] + pV;

		//cout << si << " " << rW << " " << pV << " " << curV << endl;
		ans = min(ans, curV);

		if (si < 3)
			calc(si + 1, rW - curW, curV);
	}
}

int main() {

	ios_base::sync_with_stdio(false);
	//freopen("/Users/george/A_1.in", "r", stdin);

	cin >> N >> W;

	LL w, v;

	LP(i, 0, N)
	{
		cin >> w >> v;

		if (w1 < 0)
			w1 = w;

		int si = w - w1;

		//cout <<"PRE-SORT!" << endl;
		vs[si][size[si]] = -v;
		size[si]++;
	}

	LP(si, 0, 4)
	{
		//cout << "SORT!" << endl;

		sort(vs[si], vs[si] + size[si]);

		LP(i, 1, size[si])
			vs[si][i] += vs[si][i - 1];

		/*
		 LP(i, 0, size[si])
		 cout << vs[si][i] << " ";
		 cout << endl;
		 */

	}

	calc(0, W, 0);

	cout << -ans;

	return 0;
}