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

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define SIZE 101
#define SACK_SIZE 10001
int S, N;
int w[SIZE], v[SIZE], dp[SIZE][SACK_SIZE];
//int opt_val;
int mmax(int a, int b){
	if (a > b) return a;
	else return b;
}void init(){
	int i, j;
	for (i = 0; i < SIZE; i++){
		for (j = 0; j < SACK_SIZE; j++){
			dp[i][j] = -1;
		}
	}
}
int solve(int index, int cap){

	if (dp[index][cap] >= 0){
		return dp[index][cap];
	}
	if (index == N){
		return 0;
	}
	int left, right;

	left = solve(index + 1, cap);

	if (cap - w[index] >= 0){
		right = solve(index + 1, cap - w[index]) + v[index];
	}
	else
	{
		right = 0;
	}

	dp[index][cap] = mmax(left, right);
	return dp[index][cap];
}
int main(void){

	while (scanf("%d %d", &N, &S) != EOF){
		for (int i = 0; i < N; i++){
			scanf("%d %d", &v[i], &w[i]);
		}
		init();
		int value = solve(0, S);
		printf("%d\n", value);
	}
	return 0;
}