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: Runtime Error)

use std::io::stdin;

#[allow(dead_code)]
fn input_strs() -> Vec<char> {
    let mut s = String::new();
    match stdin().read_line(&mut s) {
        Ok(_) => s.chars().collect(),
        Err(_) => panic!("err"),
    }
}

#[allow(dead_code)]
fn input_ints() -> Vec<u64> {
    let mut st = String::new();
    match stdin().read_line(&mut st) {
        Ok(_) => st.split_whitespace().map(|s| s.parse().unwrap()).collect(),
        Err(_) => panic!("err"),
    }
}

// struct Baggage {
//     min_on: u64,
//     min_off: u64,
//     on_w: u64,
//     off_w: u64,
// }

#[allow(dead_code)]
fn calc(data: Vec<Vec<u64>>, w: usize) -> i64 {
    let mut memo: Vec<Vec<i64>> = vec![vec![-1; data.len()]; w + 1];

    let n = data.len() - 1;

    for i in 0..n + 1 {
        memo[0][i] = 0;
    }

    for i in 0..w + 1 {
        memo[i][n] = if i as u64 >= data[n][0] {
            data[n][1] as i64
        } else {
            0
        };
    }

    fn dp(data: &Vec<Vec<u64>>, memo: &mut Vec<Vec<i64>>, n: usize, w: usize) -> i64 {
        if memo[w][n] != -1 {
            return memo[w][n];
        }

        let off = dp(data, memo, n + 1, w);

        let res = if (w as u64) < data[n][0] {
            off
        } else {
            std::cmp::max(dp(data, memo, n + 1, w - data[n][0] as usize) + data[n][1] as i64,
                          off)
        };

        memo[w][n] = res;
        res
    }

    dp(&data, &mut memo, 0, w)
}

#[allow(unused_variables)]
fn solve() {
    let l = input_ints();
    let n = l[0];
    let w = l[1] as usize;

    let mut knap = Vec::new();

    for _ in 0..n {
        let wv = input_ints();
        knap.push(wv);
    }

    println!("{}", calc(knap, w));

}

fn main() {
    solve();
}