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 <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int N, W;
int w[110];
int v[110];

//int dp[110][330][100];
int dp[110][330];

int search_max(int now, int maxnum, int weight, int id) {
  if (dp[now][id] != -1) {
     return dp[now][id];
   }

  if (now == maxnum) {
    if (weight < w[now]) {
      return dp[now][id] = 0;
      // return 0;
    } else {
      return dp[now][id] = v[now];
      // return v[now];
    }
  }

  else {
    int tempin, tempout, tempid;
    if (w[now] <= weight) {
      tempin = search_max(now + 1, maxnum, weight - w[now], id+w[now]-w[0]+1) + v[now];
      
    } else {
      tempin = -1;
    }
    tempout = search_max(now + 1, maxnum, weight, id);
    // printf("num:%d in:%d out:%d\n", now, tempin, tempout);
    if (tempin > tempout) {
      // printf("%d, %d\n", now, tempin);
      return dp[now][id] = tempin;
      // return tempin;
    } else {
      // printf("%d, %d\n", now, tempout);
      return dp[now][id] = tempout;
      // return tempout;
    }
  }
}

/*
int search_max(int now, int maxnum, int weight, int id, int count) {
  if (dp[now][id][count] != -1) {
     return dp[now][id][count];
   }

  if (now == maxnum) {
    if (weight < w[now]) {
      return dp[now][id][count] = 0;
      // return 0;
    } else {
      return dp[now][id][count] = v[now];
      // return v[now];
    }
  }

  else {
    int tempin, tempout, tempid, tempcount;
    if (w[now] <= weight) {
      tempid =id + w[now] - w[0];
      tempcount = count + 1;
      tempin = search_max(now + 1, maxnum, weight - w[now], tempid, tempcount) + v[now];
      
    } else {
      tempin = -1;
    }
    tempout = search_max(now + 1, maxnum, weight, id, count);
    // printf("num:%d in:%d out:%d\n", now, tempin, tempout);
    if (tempin > tempout) {
      // printf("%d, %d\n", now, tempin);
      return dp[now][tempid][tempcount] = tempin;
      // return tempin;
    } else {
      // printf("%d, %d\n", now, tempout);
      return dp[now][id][count] = tempout;
      // return tempout;
    }
  }
}
*/

/*
int search(const int i, const int n, const int sum_v, const int sum_w)
{
  if (i == n) {
    return sum_v;
  }

  x[i] = 0;
  const double v0 = search(i + 1, n, sum_v, sum_w);

  x[i] = 1;
  const double v1 = search(i + 1, n, sum_v + v[i], sum_w + w[i]);

  return (v0 > v1) ? v0 : v1;
}
*/
/*
void solve_dp2() {
  for (int j = 0; j <= W; j++) {
    dp[n][j] = 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[0][W] << endl;
}
*/

int main() {
  int i, j, l;
  int ans;

  for (i = 0; i < 110;i++){
    for (j = 0; j < 330;j++){
      dp[i][j] = -1;
    } }
  /*
    for (i = 0; i < 110;i++){
    for (j = 0; j < 330;j++){
      for (l = 0; l < 100; l++) {
        dp[i][j][l] = -1;
      }
    } }
*/

  scanf("%d %d", &N, &W);
  for (i = 0; i < N; i++) {
    scanf("%d %d", &w[i], &v[i]);
  }

  //ans = search_max(0, N - 1, W, 0, 0);
  ans = search_max(0, N - 1, W, 0);
  printf("%d", ans);

  return 0;
}