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 C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;

namespace AOJ.Codes
{
    //問題の名称... Combinatorial - 0-1 Knapsack Problem
    //問題文のURL... http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_B&lang=jp#
    //所要時間... AAA
    //学んだこと... AAA
    //教室内の大まかな着席位置... 左 x 中央


    class Knapsack
    {
        static void Main(string[] args)
        {
            //読み込み
            var x = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var N = x[0];
            var W = x[1];

            var items = new List<int[]>();
            for (int i = 0; i < N; i++)
            {
                var item = Console.ReadLine().Split().Select(int.Parse).ToArray();
                items.Add(item);
            }

            //計算
            var memo = new int[N, W];
            for (int i = 0; i < N; i++)
                for (int j = 0; j < W; j++)
                    memo[i, j] = -1;

            var ans = GetVic(N, W, items, ref memo);

            //出力
            Console.WriteLine(ans);

            //Enterを押して終了
            Console.ReadLine();
        }


        public static int GetVic(int i, int c, List<int[]> items, ref int[,] memo)
        {
            if (i <= 0 || c <= 0)
                return 0;


            if (memo[i-1, c-1] != -1)
                return memo[i - 1, c - 1];

            var v = items[i - 1][0];
            var w = items[i - 1][1];
            var Vi_1c = GetVic(i - 1, c, items, ref memo);

            if (c - w < 0)
            {
                memo[i - 1, c - 1] = Vi_1c;
                return Vi_1c;
            }
            else
            {
                var ans = Math.Max(v + GetVic(i - 1, c - w, items, ref memo), Vi_1c);
                memo[i - 1, c - 1] = ans;
                return ans;

            }

        }

    }
}