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];
    const M: usize = 4; // max(w) - min(w) + 1
    let mut u = vec![Vec::<i32>::new(); M];
    for i in 0..N {
        u[w[i] - w0].push(v[i]);
    }
    let mut acc = vec![vec![0]; M];
    for i in 0..M {
        u[i].sort_by(|x, y| y.cmp(x));
        for j in 0..u[i].len() {
            let x = acc[i][j] + u[i][j];
            acc[i].push(x);
        }
    }
    let mut result = 0;
    for i0 in 0..u[0].len() + 1 {
        for i1 in 0..u[1].len() + 1 {
            for i2 in 0..u[2].len() + 1 {
                for i3 in 0..u[3].len() + 1 {
                    let weight = i0 * (w0 + 0) + i1 * (w0 + 1) + i2 * (w0 + 2) + i3 * (w0 + 3);
                    if weight <= W {
                        let value = acc[0][i0] + acc[1][i1] + acc[2][i2] + acc[3][i3];
                        result = std::cmp::max(result, value);
                    }
                }
            }
        }
    }
    println!("{}", result);
}

#[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()
            }
        }
    }
}