Problem:
Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^9

1 \leq w_i \leq W

1 \leq v_i \leq 10^3

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 sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

1 1000000000
1000000000 10

Sample Output 2

10

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

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

class Program
{
    static void Main(string[] args)
    {
        const long MOD = 1000000007;
        var sc = new Scanner();
        int N = sc.nextInt();
        int W = sc.nextInt();
        var w = new int[N];
        var v = new int[N];
        for (int i = 0; i < N; i++)
        {
            w[i] = sc.nextInt();
            v[i] = sc.nextInt();
        }

        // ☆☆本質情報☆☆
        // Knapsack1では
        // dp[i,j]は「iまで詰めた時、重さj以下で詰められる最大価値」
        // 
        // Knapsack2では
        // 重さの入力範囲がデカすぎる→重さを配列の添字にできない。
        // 重さと最大価値を入れ替える
        // dp[i,j]は「iまで詰めた時、価値jで詰められる最小の重さ」
        //
        var dp = new long[N, N * 1000 + 1];
        // 配列の初期値0じゃなくてMaxValue
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N * 1000 + 1; j++)
            {
                dp[i, j] = long.MaxValue;
            }
        }
        // dp[0,0]の初期値は0
        dp[0, 0] = 0;

        for (int i = 0; i < N; i++)
        {
            // 価値を1ずつ変えて、その時の重さをチェック
            for (int j = 0; j <= N * 1000; j++)
            {
                // 1つ目詰める時だけ処理違う
                if (i == 0)
                {
                    dp[i, v[i]] = w[i];
                    break;
                }
                else
                {
                    // 最大価値になりうるのは
                    // １．「i - 1まで詰めた時の重さ」
                    dp[i, j] = Math.Min(dp[i, j], dp[i - 1, j]);
                    // もしくは、
                    // ２．「重量制限以内で詰められるなら詰めた時の値」
                    if (dp[i - 1, j] != long.MaxValue && dp[i - 1, j] + w[i] <= W) dp[i, j + v[i]] = dp[i - 1, j] + w[i];
                }
            }
        }

        // 答えはdp[N - 1, j]をjの最大から見ていってdp[N - 1, j] <= Wになるとこのj
        int ans = 0;
        for (int i = N * 1000; 0 <= i; i--)
        {
            if (dp[N - 1, i] <= W)
            {
                ans = i;
                break;
            }
        }
        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        Console.WriteLine(ans);
        Console.Out.Flush();
    }
}

// chokudai's Scanner
class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }

    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }

    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}