Problem:
0-1 Knapsack Problem II

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 ≤ 100

1 ≤ vi ≤ 100

1 ≤ wi ≤ 10,000,000

1 ≤ W ≤ 1,000,000,000

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.Linq;//リストの使用
using System.Collections.Generic;
using System.Text;//テキストの高速出力に必要
class Program
{
	static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');
		long n = long.Parse(input[0]);
		long w = long.Parse(input[1]);
    long[,] dp = new long[n+1,10001];//dp[見た個数,価値]=重さの最小値

    long[,] vertexes = new long[n,2];//ここに受け取る
    for(long i = 0; i < n; i++)
    {
      long[] nums = Array.ConvertAll(Console.ReadLine().Split(' '),long.Parse);//１行ごとに受け取る
      vertexes[i,0] = nums[0];
      vertexes[i,1] = nums[1];
    }

    for(int i = 0; i <= n; i++)//初期化
    {
      for(int j = 0; j <= 10000; j++)
      {
        dp[i,j] = long.MaxValue;
      }
    }
    for(int i = 0; i <= n; i++)
    {
      dp[i,0] = 0;
    }

    for(int i = 1; i <= n; i++)
    {
      for(int j = 0; j <= 10000; j++)
      {
        if(dp[i-1,j] != long.MaxValue)
        {
          dp[i,j] = Math.Min(dp[i,j], dp[i-1,j]);
        }
        
        if(j-vertexes[i-1,0] >= 0)//取るならば
        {
          if(dp[i-1,j-vertexes[i-1,0]] != long.MaxValue)
          {
            dp[i,j] = Math.Min(dp[i,j], dp[i-1,j-vertexes[i-1,0]] + vertexes[i-1,1]);
            //Console.WriteLine(dp[i,j]+" "+i+" "+j);
          }
        }
      }
    }

    for(int i = 10000; i >= 0; i--)
    {
      if(dp[n,i] <= w)//重さが制限を満たすならば
      {
        Console.WriteLine(i);
        break;
      }
    }
	}
}