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 Rust (1.15.1) (judge verdict: Accepted)

use std::cmp;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.read();
    let w: usize = sc.read();
    let (vs, base_w) = {
        let wv: Vec<(usize, usize)> = (0..n).map(|_| (sc.read(), sc.read())).collect();
        let (w0, _) = wv[0];
        let mut vs = vec![vec![]; 4];
        for t in &wv {
            let (w, v) = *t;
            vs[w - w0].push(v);
        }
        for i in 0..4 {
            vs[i].sort();
            vs[i].reverse();
        }
        (vs, w0)
    };
    let (w0, w1, w2, w3) = (vs[0].len(), vs[1].len(), vs[2].len(), vs[3].len());


    let mut dp = vec![vec![vec![vec![0; w3 + 1]; w2 + 1]; w1 + 1]; w0 + 1];
    for i0 in 0..(w0 + 1) {
        for i1 in 0..(w1 + 1) {
            for i2 in 0..(w2 + 1) {
                for i3 in 0..(w3 + 1) {
                    if i0 < w0 {
                        dp[i0 + 1][i1][i2][i3] = cmp::max(dp[i0 + 1][i1][i2][i3], dp[i0][i1][i2][i3] + vs[0][i0])
                    }
                    if i1 < w1 {
                        dp[i0][i1 + 1][i2][i3] = cmp::max(dp[i0][i1 + 1][i2][i3], dp[i0][i1][i2][i3] + vs[1][i1])
                    }
                    if i2 < w2 {
                        dp[i0][i1][i2 + 1][i3] = cmp::max(dp[i0][i1][i2 + 1][i3], dp[i0][i1][i2][i3] + vs[2][i2])
                    }
                    if i3 < w3 {
                        dp[i0][i1][i2][i3 + 1] = cmp::max(dp[i0][i1][i2][i3 + 1], dp[i0][i1][i2][i3] + vs[3][i3])
                    }
                }
            }
        }
    }

    let mut ans = 0;
    for i0 in 0..(w0 + 1) {
        for i1 in 0..(w1 + 1) {
            for i2 in 0..(w2 + 1) {
                for i3 in 0..(w3 + 1) {
                    if (i0 + i1 + i2 + i3) * base_w + i1 + 2 * i2 + 3 * i3 > w { continue; }
                    ans = cmp::max(ans, dp[i0][i1][i2][i3]);
                }
            }
        }
    }

    println!("{}", ans);
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

impl Scanner {
    fn new() -> Scanner {
        Scanner { ptr: 0, length: 0, buf: vec![0; 1024], small_cache: vec![0; 1024] }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool { b == b'\n' || b == b'\r' || b == b'\t' || b == b' ' }

    fn read<T>(&mut self) -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)]).parse().unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}