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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>

#define datatype long long int

datatype max(datatype a, datatype b){
	if(a > b){
		return a;
	}
	else{
		return b;
	}
}

datatype min(datatype a, datatype b){
	if(a < b){
		return a;
	}
	else{
		return b;
	}
}

int compare(datatype a, datatype b){
	if(a < b){
		return 1;
	}
	else if(a > b){
		return -1;
	}
	else{
		return 0;
	}
}

datatype *sort_sub(datatype *origin, int N){
	datatype *ans = (datatype *)malloc(sizeof(datatype) * N);
	if(N == 1){
		ans[0] = origin[0];
	}
	else{
		int N1 = N / 2;
		int N2 = (N + 1) / 2;
		datatype *sub1 = sort_sub(&origin[0] , N1);
		datatype *sub2 = sort_sub(&origin[N1], N2);
		int i, j, k;
		for(i = 0, j = 0, k = 0; i < N; i++){
			if((compare(sub1[j], sub2[k]) == -1 && j != N1) || (k == N2)){
				ans[i] = sub1[j];
				j++;
			}
			else{
				ans[i] = sub2[k];
				k++;
			}
		}
		free(sub1);
		free(sub2);
	}
	return ans;
}

datatype *sort(datatype *origin, int N){
	datatype *ans;
	if(N == 0){
		ans = NULL;
	}
	else{
		ans = sort_sub(origin, N);
	}
	free(origin);
	return ans;
}

int main(){
	int N, W, i, j, k, w1, wi;
	scanf("%d%d", &N, &W);
	datatype **v = (datatype **)malloc(sizeof(datatype *) * 4);
	int *vnum = (int *)malloc(sizeof(int) * 4);
	for(i = 0; i < 4; i++){
		v[i] = (datatype *)malloc(sizeof(datatype) * N);
		vnum[i] = 0;
	}
	scanf("%d%lld", &w1, &v[0][0]);
	vnum[0]++;
	for(i = 1; i < N; i++){
		scanf("%d", &wi);
		scanf("%lld", &v[wi - w1][vnum[wi - w1]]);
		vnum[wi - w1]++;
	}
	datatype **vsum = (datatype **)malloc(sizeof(datatype *) * 4);
	for(i = 0; i < 4; i++){
		v[i] = sort(v[i], vnum[i]);
		vsum[i] = (datatype *)malloc(sizeof(datatype) * (vnum[i] + 1));
		vsum[i][0] = 0;
		for(j = 0; j < vnum[i]; j++){
			vsum[i][j + 1] = vsum[i][j] + v[i][j];
		}
	}
	datatype ans = 0;
	for(i = 0; i <= vnum[3] && W - (w1 + 3) * i >= 0; i++){
		for(j = 0; j <= vnum[2] && W - (w1 + 3) * i - (w1 + 2) * j >= 0; j++){
			for(k = 0; k <= vnum[1] && W - (w1 + 3) * i - (w1 + 2) * j - (w1 + 1) * k >= 0; k++){
				ans = max(vsum[3][i] + vsum[2][j] + vsum[1][k] + vsum[0][min(vnum[0], (W - (w1 + 3) * i - (w1 + 2) * j - (w1 + 1) * k) / w1)], ans);
			}
		}
	}
	printf("%lld\n", ans);
	return 0;
}