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::str::FromStr;
use std::fmt::Debug;
use std::cmp;
#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where T: FromStr, <T as FromStr>::Err : Debug{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().split_whitespace().map(|c| T::from_str(c).unwrap()).collect()

}

fn main(){
    let line = read_as_vec::<usize>();
    let (n, capacity) = (line[0], line[1]);
    let mut objects: Vec<Vec<u64>> = vec![vec![];4];

    let line = read_as_vec::<usize>();
    let (w, v) = (line[0], line[1] as u64);

    let mut bias = w;
    objects[0].push(v);

    for i in 1..n{
        let line = read_as_vec::<usize>();
        let (w, v) = (line[0], line[1] as u64);

        objects[w-bias].push(v);
    }

    for i in 0..4{
        objects[i].sort();
        objects[i].reverse();
    }

    let mut acc_obj: Vec<Vec<u64>> = vec![vec![];4];

    for i in 0..4{
        acc_obj[i] = vec![0;objects[i].len()+1];
    }

    for i in 0..4{
        let mut acc = 0;
        for j in 0..objects[i].len(){
            acc += objects[i][j];
            acc_obj[i][j+1] = acc;
        }
    }

    //println!("{:?}", acc_obj);
    let mut ans = 0;
    for n0 in 0..acc_obj[0].len(){
        if n0 * bias > capacity{
            continue;
        }
        for n1 in 0..acc_obj[1].len(){
            if n0* bias + n1 * (bias+1)> capacity{
                continue;
            }
            for n2 in 0..acc_obj[2].len(){
                if n0* bias + n1 * (bias+1) + n2*(bias+2) > capacity{
                    continue;
                }

                let n3 = (capacity - n0* bias - n1 * (bias+1) - n2*(bias+2))/(bias+3);
                //println!("{}, {}, {}, {}", n0, n1, n2, n3);
                if n3 >= acc_obj[3].len(){
                    ans = cmp::max(ans, acc_obj[0][n0] + acc_obj[1][n1] + acc_obj[2][n2]);
                }else{
                    ans = cmp::max(ans, acc_obj[0][n0] + acc_obj[1][n1] + acc_obj[2][n2] + acc_obj[3][n3]);
                }
            }
        }
    }

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