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: Runtime Error)

/*
	submission # - User: herp_sy
	https://atcoder.jp/contests/
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>

#define _CRT_SECURE_NO_WARNINGS
#define TLong long long
#define TBMod 1000000007

typedef struct{
	TLong w;
	TLong v;
	bool pack[107];
} TABLE;

// fact(n)
int fact(int n){
	if(n == 1)	return 1;
	else return (n * fact(n - 1));
}

// nCr(n,r)
int nCr(int n, int r){
	if(r == 0 || r == n)	return 1;
	else if(r == 1)	return n;
	return (nCr(n - 1, r - 1) + nCr(n - 1, r));
}

// gcd(a,b)
int gcd(int a,int b){
	if(a == 0) return b;
	else if(b == 0) return a;
	else return gcd(b,a % b);
}

// lcm(a,b)
int lcm(int a,int b){
	return (a * b) / gcd(a,b);
}

// swap(&a, &b);
void swap(int *a,int *b){
	int tmp;
	tmp = *a;	*a = *b;	*b = tmp;
	return;
}

// qsort(array, size, sizeof(int),comp);
int ASC(const int *a,const int *b){
	return (*a - *b);
}

int DESC(const int *a,const int *b){
	return (*b - *a);
}

// append(array,*size,addNum)
void append(int *array,int *size,int num){
	realloc(array, sizeof(int) * (*size + 1));
	array[(*size)] = num;
	++(*size);
}

// pop(array,*size) & nongetpop(array,*size)
int pop(int *array,int *size){
	int temp = array[(*size - 1)];
	nongetpop(array,&(*size));
	return temp;
}

int nongetpop(int *array,int *size){
	realloc(array,sizeof(int) * (*size - 1));
	--(*size);
}

int main(int argc, char const *argv[])
{
	int n;
	int cnt;
	TLong lim,w[105],v[105];
	TLong min = TBMod,max = 0;
	TABLE D[420][105];

	// input
	scanf("%d%lld",&n,&lim);
	for (int i = 1; i <= n; ++i)
	{
		scanf("%lld%lld",&w[i],&v[i]);
		if(min > w[i])	min = w[i];
	}
	for (int i = 1; i <= n; ++i)	w[i] -= min;

	// init D[][]
	for (int i = 0; i < 420; ++i)
	{
		for (int j = 0; j < 105; ++j)
		{
			D[i][j].w = 0;
			D[i][j].v = 0;
			for (int k = 0; k < 103; ++k)	D[i][j].pack[k] = false;
		}
	}

	// 表埋め
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 0; j <= lim; ++j)
		{
			if(j - w[i] < 0){
				D[i][j].w = D[i - 1][j].w;
				D[i][j].v = D[i - 1][j].v;
				for (int k = 0; k < 103; ++k)	D[i][j].pack[k] = D[i - 1][j].pack[k];
			}
			else{	// (1) 真上			// (2) 真上 - w[i]
				if(D[i - 1][j].v < D[i - 1][j - w[i]].v + v[i]){
					D[i][j].w = D[i - 1][j - w[i]].w + w[i];
					D[i][j].v = D[i - 1][j - w[i]].v + v[i];
					for (int k = 0; k < 103; ++k)	D[i][j].pack[k] = D[i - 1][j - w[i]].pack[k];
					D[i][j].pack[i] = true;
				}
				else{
					D[i][j].w = D[i - 1][j].w;
					D[i][j].v = D[i - 1][j].v;
					for (int k = 0; k < 105; ++k)	D[i][j].pack[k] = D[i - 1][j].pack[k];
					D[i][j].pack[i] = true;
				}
			}

			// count up 'true'
			cnt = 0;
			for (int k = 0; k < 103; ++k)
			{
				if(D[i][j].pack[k])	++cnt;
			}

			// check weight
			if(D[i][j].w <= (lim - min * (TLong)cnt) && max < D[i][j].v)	max = D[i][j].v;
			
			// dbg.
			// printf("// cnt: %2d, D[%d][%d].w: %3lld, D.[%d][%d].v: %3lld, max = %3lld\n", cnt,i,j,D[i][j].w,i,j,D[i][j].v,max);
		}
	}
	printf("%lld\n", max);
	return 0;
}