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)

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int N, v[110], w0[110], w1[110], w2[110], w3[110];
long long W, w[110];

long long max(int len, ...) {
    long long i, _max, num;
    va_list ap;
    va_start(ap, len);
 
    _max = va_arg(ap, long long);
    for (i = 1; i < len; i++) {
        num = va_arg(ap, long long);
        if (num > _max)
            _max = num;
    }
    va_end(ap);
    return _max;
}

int llcmp(const void *a, const void *b) {
    if (*(long long *)a < *(long long *)b) return 1;
    else if (*(long long *)a > *(long long *)b) return -1;
    else return 0;
}

int main(void) {
    int a, b, c, d, i, j, k, l, m;
    scanf("%d%lld", &N, &W);
    for (i=0;i<N;i++) scanf("%lld%d", &w[i], &v[i]);

    j = 0; k = 0; l = 0; m = 0;
    for (i=0;i<N;i++) {
        if (w[i] == w[0]) w0[++j] = v[i];
        else if (w[i] == w[0]+1) w1[++k] = v[i];
        else if (w[i] == w[0]+2) w2[++l] = v[i];
        else w3[++m] = v[i];
    }
    qsort(w0+1,j,sizeof(long long),llcmp);
    qsort(w1+1,k,sizeof(long long),llcmp);
    qsort(w2+1,l,sizeof(long long),llcmp);
    qsort(w3+1,m,sizeof(long long),llcmp);
    long long (*dp)[k+1][l+1][m+1] = (long long(*)[k+1][l+1][m+1])malloc((j+1)*(k+1)*(l+1)*(m+1)*sizeof(long long));
    long long (*weight)[k+1][l+1][m+1] = (long long(*)[k+1][l+1][m+1])malloc((j+1)*(k+1)*(l+1)*(m+1)*sizeof(long long));
    memset(dp,0,sizeof(dp));
    memset(weight,0,sizeof(weight));
    for (a=0;a<=j;a++) for (b=0;b<=k;b++) for (c=0;c<=l;c++) for (d=0;d<=m;d++) {
        if (a>0) if (weight[a-1][b][c][d] + w[0] <= W && dp[a][b][c][d] < dp[a-1][b][c][d] + w0[a]) {
//            printf("%d\n", w0[a]);
            dp[a][b][c][d] = dp[a-1][b][c][d] + w0[a];
            weight[a][b][c][d] = weight[a-1][b][c][d] + w[0];
        } else if (dp[a][b][c][d] < dp[a-1][b][c][d]) {
            dp[a][b][c][d] = dp[a-1][b][c][d];
            weight[a][b][c][d] = weight[a-1][b][c][d];
        }
        if (b>0) if (weight[a][b-1][c][d] + w[0]+1 <= W && dp[a][b][c][d] < dp[a][b-1][c][d] + w1[b]) {
            dp[a][b][c][d] = dp[a][b-1][c][d] + w1[b];
            weight[a][b][c][d] = weight[a][b-1][c][d] + w[0]+1;
        } else if (dp[a][b][c][d] < dp[a][b-1][c][d]) {
            dp[a][b][c][d] = dp[a][b-1][c][d];
            weight[a][b][c][d] = weight[a][b-1][c][d];
        }
        if (c>0) if (weight[a][b][c-1][d] + w[0]+2 <= W && dp[a][b][c][d] < dp[a][b][c-1][d] + w2[c]) {
            dp[a][b][c][d] = dp[a][b][c-1][d] + w2[c];
            weight[a][b][c][d] = weight[a][b][c-1][d] + w[0]+2;
        } else if (dp[a][b][c][d] < dp[a][b][c-1][d]) {
            dp[a][b][c][d] = dp[a][b][c-1][d];
            weight[a][b][c][d] = weight[a][b][c-1][d];
        }
        if (d>0) if (weight[a][b][c][d-1] + w[0]+3 <= W && dp[a][b][c][d] < dp[a][b][c][d-1] + w3[d]) {
            dp[a][b][c][d] = dp[a][b][c][d-1] + w3[d];
            weight[a][b][c][d] = weight[a][b][c][d-1] + w[0]+3;
        } else if (dp[a][b][c][d] < dp[a][b][c][d-1]) {
            dp[a][b][c][d] = dp[a][b][c][d-1];
            weight[a][b][c][d] = weight[a][b][c][d-1];
        }
//        printf("%d,%d,%d,%d:%lld\n",a,b,c,d,dp[a][b][c][d]);
    }

    printf("%lld\n", dp[j][k][l][m]);

    return 0;
}