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.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        long W = Long.parseLong(sc.next());
        Map<Long, List<Long>> map = new TreeMap<>();
        long w = Long.parseLong(sc.next());
        long basew = w;
        for (int i = (int)w; i <= w + 3; i++) {
            List<Long> base = new ArrayList<>();
            map.put((long)i, base);
        }
        long v = Long.parseLong(sc.next());
        map.get(w).add(v);
        for (int i = 1; i < N; i++) {
            w = Integer.parseInt(sc.next());
            v = Long.parseLong(sc.next());
            map.get(w).add(v);
        }
        int[] listsize = new int[map.size()];
        int ii = 0;
        for (Long key : map.keySet()) {
            listsize[ii] = map.get(key).size() + 1;
            ii++;
        }
        for (Map.Entry<Long, List<Long>> list : map.entrySet()) {
            Collections.sort(list.getValue(), Comparator.reverseOrder());
            long sum = 0;
            List<Long> sumList = new ArrayList<>();
            for (long l : list.getValue()) {
                sum += l;
                sumList.add(sum);
            }
            sumList.add(0, 0L);
            map.put(list.getKey(), sumList);
        }
        long ans = 0;
        for (int i = 0; i < listsize[0]; i++) {
            for (int j = 0; j < listsize[1]; j++) {
                for (int k = 0; k < listsize[2]; k++) {
                    for (int l = 0; l < listsize[3]; l++) {
                        long sumw = basew * i + (basew + 1) * j + (basew + 2) * k + (basew + 3) * l;
                        if (sumw <= W) {
                            long sum1 = map.get(basew).get(i);
                            long sum2 = map.get(basew + 1).get(j);
                            long sum3 = map.get(basew + 2).get(k);
                            long sum4 = map.get(basew + 3).get(l);
                            long sumv = sum1 + sum2 + sum3 + sum4;
                            ans = Math.max(ans, sumv);
                        }
                    }
                }
            }
        }
        System.out.println(ans);
    }
}