Problem:
0-1 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 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 ≤ 100

1 ≤ vi ≤ 1000

1 ≤ wi ≤ 1000

1 ≤ W ≤ 10000

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)

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

pub trait InputValue {
    fn parse(s: &str) -> Self;
}

pub fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

pub fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

pub fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
    ($($t:ty),*) => {
        $(
            impl InputValue for $t {
                fn parse(s: &str) -> $t { s.parse().unwrap() }
            }
        )*
    }
}
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
    ($($t:ident),*) => {
        impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
            fn parse(s: &str) -> ($($t),*) {
                let mut tokens = s.split_whitespace();
                let t = ($($t::parse(tokens.next().unwrap())),*);
                t
            }
        }
    }
}
parse_tuple!(A, B);
parse_tuple!(A, B, C);

fn bound_check(x: i32, y: i32, width: i32, height: i32) -> bool {
    if x < 0 || y < 0 || x >= width || y >= height {
        return false;
    } else {
        return true;
    }
}

// ===

static DX: &'static [i32] = &[0, 1, 0, -1];
static DY: &'static [i32] = &[-1, 0, 1, 0];

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_B&lang=jp

// ナップザック問題　index以降のものを価値weight以下でとって最大の物を返す
fn search(
    ws: &Vec<i32>, vs: &Vec<i32>, dp: &mut Vec<Vec<i32>>,
    index: usize, weight: usize
) -> i32 {
    if index == ws.len() { return 0; }
    else if dp[index][weight] != -1 { return dp[index][weight]; }
    else if ws[index] > weight as i32 { return search(ws, vs, dp, index + 1, weight); }
    else {
        let a1 = search(ws, vs, dp, index + 1, weight);
        let a2 = search(ws, vs, dp, index + 1, weight - ws[index] as usize) + vs[index];
        let ret = max(a1, a2);
        dp[index][weight] = ret;
        return ret;
    }
}

fn main() {
    let (n, w): (usize, usize) = read();
    let mut vs: Vec<i32> = vec![0; n];
    let mut ws: Vec<i32> = vec![0; n];
    for i in 0..n { 
        let (v, w) = read();
        vs[i] = v; ws[i] = w;
    }

    let mut dp: Vec<Vec<i32>> = vec![vec![-1; 10010]; 110];

    println!("{}", search(&ws, &vs, &mut dp, 0, w));
}