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: Wrong Answer)

import java.io.*;
import java.util.*;

public class Main {
    public static final long mod = (long)1e9+7;
    public static final long INF = Long.MAX_VALUE/4;
    public static final int inf = Integer.MAX_VALUE/4;

    static void solve(InputReader in, PrintWriter out){
        int n = in.ni();
        long W = in.nl(), w = in.nl();
        PriorityQueue<Long> w1 = new PriorityQueue<>(), w2 = new PriorityQueue<>(), w3 = new PriorityQueue<>(), w4 = new PriorityQueue<>();
        w1.add(in.nl());
        for (int i = 0; i < n - 1; i++) {
            long a = in.nl();
            if(a-w==0) w1.add(in.nl());
            else if(a-w==1) w2.add(in.nl());
            else if(a-w==2) w3.add(in.nl());
            else w4.add(in.nl());
        }
        long[] v1 = new long[w1.size()+1], v2 = new long[w2.size()+1], v3 = new long[w3.size()+1], v4 = new long[w4.size()+1];
        v1[0] = 0; int i = 1;
        while(!w1.isEmpty()){ v1[i] = w1.poll()+v1[i-1]; i++; }
        v2[0] = 0; i = 1;
        while(!w2.isEmpty()){ v2[i] = w2.poll()+v2[i-1]; i++; }
        v3[0] = 0; i = 1;
        while(!w3.isEmpty()){ v3[i] = w3.poll()+v3[i-1]; i++; }
        v4[0] = 0; i = 1;
        while(!w4.isEmpty()){ v4[i] = w4.poll()+v4[i-1]; i++; }
        long ans = 0, weight;
        A:for (long j = 0; j < v1.length; j++) {
            weight = j*w;
            if(weight>W) break;
            B:for (long k = 0; k < v2.length; k++) {
                weight += k*(w+1);
                if(weight>W) continue A;
                for (long l = 0; l < v3.length; l++) {
                    weight += l*(w+2);
                    if(weight>W) continue B;
                    ans = Math.max(ans, v1[(int)j]+v2[(int)k]+v3[(int)l]+v4[(int)Math.min((W-weight)/(w+3), v4.length-1)]);
                }
            }
        }
        out.println(ans);
    }

    public static void main(String[] args){
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        solve(in, out);
        out.close();
    }
    public static class InputReader{
        private BufferedReader br;
        private StringTokenizer st;
        public InputReader(InputStream is){
            br = new BufferedReader(new InputStreamReader(is));
            st = null;
        }
        public String ns(){
            if(st == null || !st.hasMoreTokens()){
                try{
                    st = new StringTokenizer(br.readLine());
                }catch (Exception e){
                    throw new RuntimeException(e);
                }
            }
            return st.nextToken();
        }
        public int ni(){
            return Integer.parseInt(ns());
        }
        public long nl(){
            return Long.parseLong(ns());
        }
        public Double nd(){
            return Double.parseDouble(ns());
        }
        public int[] ni(int n){
            int[] a = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = ni();
            }
            return a;
        }
        public long[] nl(int n){
            long[] a = new long[n];
            for (int i = 0; i < n; i++) {
                a[i] = nl();
            }
            return a;
        }
        public double[] nd(int n){
            double[] a = new double[n];
            for (int i = 0; i < n; i++) {
                a[i] = nd();
            }
            return a;
        }
    }
}