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: Time Limit Exceeded)

//
//  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>

int main2(int w[], int v[], int n, int wmax);

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 main1(int seed) {
//    long long arr[10] = {-1, 8, 6, 7, 5, 4, 3, 2, 0, 1};
//    sort(arr, 10);
//    printArr(arr, 10);
#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);
#ifdef DB
    N = rand() % 100 + 1; W = rand() % 1000000000;
#endif
    for (int i = 0; i < N; i++) {
        scanf("%d%d", &w[i], &v[i]);
    }
    int w1 = w[0];
#ifdef DB
    for (int i = 1; i < N; i++) {
        do {
            w[i] = w1 + rand() % 4;
        }while(w[i] == w1 + 2 || w[i] == w1 + 3);
        v[i] = rand() / 10000000;
    }
#endif
//    w[0] -= 3;
//    v[9] = 100 * v[9];
    
    //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];
        valuesLen[w_index]++;
    }
    
    for (int i = 0; i < 4; 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);
    
#ifdef DB
    printf("Run an AC code. \n");
    int result2 = main2(w, v, N, W);
    if (result2 != result)
        printf("Not Equal!\nThee seed id %d. \n", seed);
#endif
    return 0;
}

struct ar {
    struct slot {
        int w, v;
    } slot[40001];
} ar[2];
int
add(struct ar *old, int w, int v, int wmax, struct ar *new)
{
    struct slot *oldp = old->slot;
    struct slot *addp = old->slot;
    struct slot *newp = new->slot;
    int current_maxv = -1;
    for (;;) {
        if (oldp->w >= 0) {
            if (addp->w >= 0) {
                int addpw = addp->w + w;
                int addpv = addp->v + v;
                if (oldp->w < addpw)
                    goto putoldp;
                else if (oldp->w > addpw)
                    goto putaddp;
                else {
                    if (oldp->v < addpv) {
                        oldp++;
                        goto putaddp;
                    } else {
                        addp++;
                        goto putoldp;
                    }
                }
            } else
                goto putoldp;
        } else {
            if (addp->w >= 0)
                goto putaddp;
            else
                break;
        }
    putoldp:
        if (current_maxv < oldp->v) {
            *newp++ = *oldp;
            current_maxv = oldp->v;
        }
        oldp++;
        continue;
    putaddp:
        {
            int addpw = addp->w + w;
            int addpv = addp->v + v;
            if (wmax < addpw)
                break;
            if (current_maxv < addpv) {
                newp->v = addpv;
                newp->w = addpw;
                current_maxv = addpv;
                newp++;
            }
            addp++;
            continue;
        }
    }
    newp->w = -1;
    newp->v = -1;
    return current_maxv;
}

int main2(int w[], int v[], int n, int wmax) {
    struct ar *old = &ar[0];
    struct ar *new = &ar[1];
    old->slot[0].w = 0;
    old->slot[0].v = 0;
    old->slot[1].w = -1;
    
    int vmax = -1;
    int i;
    for (i = 0; i < n; ++i) {
        int wi, vi;
//        scanf("%d%d", &wi, &vi);
        wi = w[i];
        vi = v[i];
        vmax = add(old, wi, vi, wmax, new);
        struct ar *tmp = new;
        new = old;
        old = tmp;
    }
    printf("%d\n", vmax);
    return vmax;
}
#include <limits.h>
int main() {
    int r = -1;
    for (int seed = 0; seed < INT_MAX; seed++) {
        r = main1(seed);
    }
    
    return r;
}