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

import java.util.Scanner;

// ナップザック問題を解くプログラム
class Solver{
    int num;
    int capacity, answer;
    int[] weight, value;
    int[][] dp;
    boolean[] knapsack;
    // コンストラクタ
    public Solver(int num, int[] weight, int[] value, int capacity) {
        this.num = num;
        this.weight = weight;
        this.value = value;
        this.capacity = capacity;
        dp = new int[num + 1][capacity + 1];
        knapsack = new boolean[num + 1];
    }
    int solve() {
        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < dp[0].length; j++) {
                dp[i][j] = -1;
            }
        }
        if (weight[0] <= capacity) {
            dp[1][weight[0]] = value[0];
        }
        dp[1][0] = 0;
        dp[0][0] = 0;
 
        for (int i = 2; i <= num; i++) {
            for (int k = 0; k <= capacity; k++) {
                if (k < weight[i - 1]) {
                    dp[i][k] = dp[i - 1][k];
                }
                else {
                    dp[i][k] = Math.max(dp[i - 1][k], dp[i - 1][k - weight[i - 1]] + value[i - 1]);
                }
            }
        }
        int res = 0;
        for (int x : dp[num]) {
            res = Math.max(x, res);
        }
 
        return answer = res;
    }
 
    // 価値の総和がsumValueとなる組み合わせすべて出力する
    // [1, n]までの品物を出力する
    void showKnapsack(int sumValue, int n) {
        if (n < 1) {
            int cnt = 0;
            for (int i = 0; i < knapsack.length; i++) {
                if (knapsack[i]) {
                    System.out.print((cnt++ > 0 ? " " : "") + i);
                }
            }
            System.out.println();
            return;
        }
        for (int i = 0; i <= capacity; i++) {
            if (dp[n][i] == sumValue) {
                int sub = i - weight[n - 1];
                if (dp[n - 1][i] == sumValue) {
                    knapsack[n] = false;
                    showKnapsack(sumValue, n - 1);
                }
                if (sub >= 0 && dp[n - 1][sub] >= 0) {
                    knapsack[n] = true;
                    showKnapsack(sumValue - value[n - 1], n - 1);
                }
            }
        }
    }
}
 
// ナップザック問題を解くプログラム
 
 
public class Main{
    public static void main(String[] args) {
        int num, capacity, answer;  //データ数
        int[] weight, value;
        // 入力
        Scanner stdIn = new Scanner(System.in);
        num = stdIn.nextInt();
        capacity = stdIn.nextInt();
        weight = new int[num];
        value = new int[num];
        for (int i = 0; i < num; i++) {
            value[i] = stdIn.nextInt();
            weight[i] = stdIn.nextInt();

        }
 
        Solver s = new Solver(num, weight, value, capacity);
        answer = s.solve();
        System.out.println(answer);
        // s.showKnapsack(answer, num);
    }
}