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

//
//  main.c
//  abc060_d
//
//  Created by Ding Chao on 4/29/17.
//  Copyright © 2017 Ding Chao. All rights reserved.
//
#define DBa
#include <stdio.h>
#include <stdlib.h>

//max value of first i item with Capacity j. m[i][j]
int m[100][4];

long long max(long long a, long long b) {
    return a > b ? a : b;
}

int min(int a, int b) {
    return a < b ? a : b;
}

void sort(long long arr[], int len) {
    //insertion sort
    for (int i = 1; i < len; i++) {
        // 0 to i - 1 is sorted
        for (int j = i; j <= 0; j--) {
            //try to find the place of j so that 0 to i is sorted. 
            if (arr[j] < arr[j - 1]) {
                //move j one step forward
                long long t = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = t;
            }
            else
                break; //ordered. 
        }
    }
}

void printArr(long long arr[], int len) {
#ifdef DB
    for (int i = 0; i < len; i++)
        printf("%lld ", arr[i]);
    printf("\n");
#endif
}

int main() {
#ifdef DB
//    freopen("/Users/DC/ownCloud/CodingContests/AtCoder/abc060/abc060_d/abc060_d/input.txt", "r", stdin);
#endif
    int N, W, w[100], v[100];
    scanf("%d%d", &N, &W);
//    N = 100; W = 1000000000;
    for (int i = 0; i < N; i++) {
        scanf("%d%d", &w[i], &v[i]);
//        w[i] = W;
//        v[i] = 10000000;
    }
    
    int w1 = w[0];
    //values[i][j] is the jth value of them of weight w1 + i
    long long values[4][100]; 
    int valuesLen[4] = {0, 0 ,0 ,0};
    for (int i = 0; i < N; i++) {
        int w_index = w[i] - w1;
        values[w_index][valuesLen[w_index]++] = v[i];
    }
    
    for (int i = 0; i < 3; i++) {
        sort(values[i], valuesLen[i]);
        printArr(values[i], valuesLen[i]);
    }
    
    //maxValue of weight i and j items. 
    long long valueSum[4][101];//0 to 100 items. 
    
#ifdef DB
    printf("Printing valueSum. \n");
#endif
    for (int i = 0; i < 4; i++) {
        valueSum[i][0] = 0;
        for (int j = 1; j <= valuesLen[i]; j++) {
            valueSum[i][j] = valueSum[i][j - 1] + values[i][j - 1];
        }
        printArr(valueSum[i], valuesLen[i] + 1);
    }
    
    long long result = 0;
    
    for (int k0 = 0; k0 <= valuesLen[0] && k0 * w1 <= W; k0++) {
        for (int k1 = 0; k1 <= valuesLen[1]; k1++) {
            for (int k2 = 0; k2 <= valuesLen[2]; k2++) {
                int remain = (W - (k2 * (w1 + 2) + k1 * (w1 + 1) + k0 * w1));
                if (remain >= 0) {
                    int k3 = min(remain / (w1 + 3), valuesLen[3]);
                    long long newResult = valueSum[0][k0] + valueSum[1][k1] 
                        + valueSum[2][k2] + valueSum[3][k3];
                    result = max(result, newResult);
                }
            }
        }
    }

        
    printf("%lld\n", result);
    return 0;
}