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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
        MyReader r = new MyReader();
        int N, W;
        {int[]a = r.ii(); N = a[0]; W = a[1];}
        int[] w = new int[N];
        int[] v = new int[N];
        ArrayList<Integer>[] things = new ArrayList[4];
        for(int i = 0; i < 4; i++)
            things[i] = new ArrayList<>();
        for(int i = 0; i < N; i++){
            int[] a = r.ii();
            w[i] = a[0];
            v[i] = a[1];
            things[w[i]-w[0]].add(v[i]);
        }
        for(int i = 0; i < 4; i++)
            things[i].sort((x, y) -> -x+y);

        int w0 = w[0];
        int w1 = w0+1;
        int w2 = w1+1;
        int w3 = w2+1;
        long max = 0;

        for(int i = 0, v0 = 0; i <= Math.min(W/w0, things[0].size()); i++){
            if(i > 0)
                v0 += things[0].get(i-1);
            for(int j = 0, v1 = 0; j <= Math.min((W-i*w0)/w1, things[1].size()); j++){
                if(j > 0)
                    v1 += things[1].get(j-1);
                for(int k = 0, v2 = 0; k <= Math.min((W-i*w0-j*w1)/w2, things[2].size()); k++){
                    if(k > 0)
                        v2 += things[2].get(k-1);
                    for(int l = 0, v3 = 0; l <= Math.min((W-i*w0-j*w1-k*w2)/w3, things[3].size()); l++){
                        if(l > 0)
                            v3 += things[3].get(l-1);
                        max = Math.max(max, v0+v1+v2+v3);
                    }
                }
            }
        }
        println(max);
    }
    static void print(Object o){
        System.out.print(o.toString());
    }

    static void println(Object o){
        System.out.println(o.toString());
    }

    static int Int(String s){
        return Integer.parseInt(s);
    }

    static long Long(String s){
        return Long.parseLong(s);
    }
    static class MyReader extends BufferedReader{
        MyReader(){
            super(new InputStreamReader(System.in));
        }

        String s() throws IOException{
            return readLine();
        }

        String[] ss() throws IOException{
            return s().split(" ");
        }

        int i() throws IOException{
            return Int(s());
        }

        int[] ii() throws IOException{
            String[] ss = ss();
            int size = ss.length;
            int[] ii = new int[size];
            for(int j = 0; j < size; j++) ii[j] = Int(ss[j]);
            return ii;
        }

        long l() throws IOException{
            return Long(s());
        }

        long[] ll() throws IOException{
            String[] ss = ss();
            int size = ss.length;
            long[] ll = new long[size];
            for(int j = 0; j < size; j++) ll[j] = Long(ss[j]);            
            return ll;
        }
    }
}