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 Go (1.14.1) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func Scanner() string {
	sc.Scan()
	return sc.Text()
}

func intScanner() int {
	n, _ := strconv.Atoi(Scanner())
	return n
}

func floatScanner() float64 {
	n, _ := strconv.ParseFloat(Scanner(), 64)
	return n
}
func stringToInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}
func stringSliceScanner(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = Scanner()
	}
	return a
}
func intSliceScanner(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = intScanner()
	}
	return a
}
func larger(a, b int) int {
	if a < b {
		return b
	} else {
		return a
	}
}
func smaller(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}
func max(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x < a[i] {
			x = a[i]
		}
	}
	return x
}
func min(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x > a[i] {
			x = a[i]
		}
	}
	return x
}
func abs(x int) int {
	if x < 0 {
		x = -x
	}
	return x
}

var mod int

func main() {
	mod = 1000000007
	buf := make([]byte, 0)
	sc.Buffer(buf, mod)
	sc.Split(bufio.ScanWords)
	n, w := intScanner(), intScanner()
	x, v := intScanner(), intScanner()
	zero := []int{v}
	one := make([]int, 0, n)
	two := make([]int, 0, n)
	three := make([]int, 0, n)
	for i := 1; i < n; i++ {
		y := intScanner()
		if y == x {
			zero = append(zero, intScanner())
		} else if y == x+1 {
			one = append(one, intScanner())
		} else if y == x+2 {
			two = append(two, intScanner())
		} else if y == x+3 {
			three = append(three, intScanner())
		}
	}
	sort.Sort(sort.Reverse(sort.IntSlice(zero)))
	sort.Sort(sort.Reverse(sort.IntSlice(one)))
	sort.Sort(sort.Reverse(sort.IntSlice(two)))
	sort.Sort(sort.Reverse(sort.IntSlice(three)))
	for i := 1; i < len(zero); i++ {
		zero[i] = zero[i-1] + zero[i]
	}
	for i := 1; i < len(one); i++ {
		one[i] = one[i-1] + one[i]
	}
	for i := 1; i < len(two); i++ {
		two[i] = two[i-1] + two[i]
	}
	for i := 1; i < len(three); i++ {
		three[i] = three[i-1] + three[i]
	}
	ans := 0
	for i := 0; i <= len(zero); i++ {
		for j := 0; j <= len(one); j++ {
			for k := 0; k <= len(two); k++ {
				for l := 0; l <= len(three); l++ {
					s := x*i + (x+1)*j + (x+2)*k + (x+3)*l
					if s <= w {
						z := 0
						if i > 0 {
							z += zero[i-1]
						}
						if j > 0 {
							z += one[j-1]
						}
						if k > 0 {
							z += two[k-1]
						}
						if l > 0 {
							z += three[l-1]
						}
						ans = larger(ans, z)
					}
				}
			}
		}
	}
	fmt.Println(ans)
}