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)

#![allow(non_snake_case)]

fn main() {
    let stdin = std::io::stdin();
    let mut stdin = my::io::InputReader::new(stdin.lock());

    let N: usize = stdin.read();
    let W: usize = stdin.read();
    let (w, v): (Vec<usize>, Vec<i32>) = (0..N).map(|_| (stdin.read::<usize>(), stdin.read::<i32>())).unzip();

    let w0 = w[0];
    let w: Vec<usize> = w.into_iter().map(|x| x - w0).collect();
    const W_MAX: usize = 3;
    let W_SUM_MAX = N * W_MAX;

    let mut dp = vec![vec![vec![None; W_SUM_MAX + 1]; N + 1]; N + 1];
    for i in 0..N + 1 {
        for k in 0..W_SUM_MAX + 1 {
            dp[i][0][k] = Some(0);
        }
    }
    for i in 1..N + 1 {
        for j in 1..N + 1 {
            for k in 0..W_SUM_MAX + 1 {
                dp[i][j][k] = if w[i - 1] <= k && dp[i - 1][j - 1][k - w[i - 1]].is_some() {
                    std::cmp::max(dp[i - 1][j][k], Some(dp[i - 1][j - 1][k - w[i - 1]].unwrap() + v[i - 1]))
                } else {
                    dp[i - 1][j][k]
                };
                //println!("dp[{}][{}][{}] = {:?}", i, j, k, dp[i][j][k]);
            }
        }
    }
    let mut result = Some(0);
    for j in 1..N + 1 {
        for k in 0..W_SUM_MAX + 1 {
            if k + j * w0 <= W {
                result = std::cmp::max(result, dp[N][j][k]);
            }
        }
    }
    println!("{}", result.unwrap());
}

#[allow(dead_code, unused_must_use)]
mod my {
    pub mod io {
        use std::io::{BufRead, ErrorKind, Result};
        use std::str::{FromStr, from_utf8};
        use std::fmt::Debug;

        pub fn read_word<R: BufRead>(r: &mut R, buf: &mut Vec<u8>)
                                              -> Result<usize> {
            let mut read = 0;
            loop {
                let (done, used) = {
                    let available = match r.fill_buf() {
                        Ok(n) => n,
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
                        Err(e) => return Err(e)
                    };
                    match available.iter().position(|&x| x == b'\n' || x == b' ') {
                        Some(i) => {
                            buf.extend_from_slice(&available[..i]);
                            (true, i + 1)
                        }
                        None => {
                            buf.extend_from_slice(available);
                            (false, available.len())
                        }
                    }
                };
                r.consume(used);
                read += used;
                if done || used == 0 {
                    return Ok(read);
                }
            }
        }

        pub struct InputReader<R: BufRead> {
            reader: R,
            buf: Vec<u8>,
        }

        impl<R: BufRead> InputReader<R> {
            pub fn new(reader: R) -> InputReader<R> {
                InputReader {
                    reader: reader,
                    buf: Vec::<u8>::new(),
                }
            }

            pub fn read_word(&mut self) {
                read_word(&mut self.reader, &mut self.buf);
            }
            
            pub fn read<T>(&mut self) -> T
                where T: FromStr,
                      T::Err: Debug
            {
                self.buf.clear();
                self.read_word();
                from_utf8(&self.buf).unwrap().trim().parse().unwrap()
            }
        }
    }
}