Problem:
Huge Knapsack Problem

You have N items that you want to put them into a knapsack. Item i has value vi and weight wi.

You want to find a subset of items to put such that:

The total value of the items is as large as possible.

The items have combined weight at most W, that is capacity of the knapsack.

Find the maximum total value of items in the knapsack.

Input

N W
v1 w1
v2 w2
:
vN wN

The first line consists of the integers N and W. In the following N lines, the value and weight of the i-th item are given.

Output

Print the maximum total values of the items in a line.

Constraints

1 ≤ N ≤ 40

1 ≤ vi ≤ 1015

1 ≤ wi ≤ 1015

1 ≤ W ≤ 1015

Sample Input 1

4 5
4 2
5 2
2 1
8 3

Sample Output 1

13

Sample Input 2

2 20
5 9
4 10

Sample Output 2

9

Solution in Rust (judge verdict: Accepted)

use std::io::Read;

fn enumerate(p: &[(u64, u64)]) -> Vec<(u64, u64)> {
    assert!(p.iter().all(|p| p.0 > 0 && p.1 > 0));
    let mut a = vec![(0, 0)];
    for &(v, w) in p.iter() {
        let mut b = a.clone();
        for b in b.iter_mut() {
            b.0 += v;
            b.1 += w;
        }
        let mut c = vec![];
        while !a.is_empty() && !b.is_empty() {
            let &(p, q) = a.last().unwrap();
            let &(r, s) = b.last().unwrap();
            if q >= s {
                c.push((p, q));
                a.pop();
            } else {
                c.push((r, s));
                b.pop();
            }
        }
        for p in a.into_iter().rev().chain(b.into_iter().rev()) {
            c.push(p);
        }
        c.reverse();
        a = c;
    }
    let mut max = a[0].0;
    for a in a.iter_mut().skip(1) {
        max = std::cmp::max(max, a.0);
        a.0 = max;
    }
    a
}

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let limit: u64 = it.next().unwrap().parse().unwrap();
    let p: Vec<(u64, u64)> = (0..n).map(|_| {
        let v: u64 = it.next().unwrap().parse().unwrap();
        let u: u64 = it.next().unwrap().parse().unwrap();
        (v, u)
    }).collect();
    let m = n / 2;
    let (l, r) = p.split_at(m);
    let (f, b) = (enumerate(l), enumerate(r));
    let mut ans = 0;
    let mut i = 0;
    let mut j = b.len();
    while i < f.len() && j > 0 {
        if f[i].1 + b[j - 1].1 <= limit {
            ans = std::cmp::max(ans, f[i].0 + b[j - 1].0);
            i += 1;
        } else {
            j -= 1;
        }
    }
    println!("{}", ans);
}

fn main() {
    run();
}