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

package main

import (
	"fmt"
	"math"
)

func main() {
	n, ww := SingleInt(), SingleInt()
	vals, weis := make([]int, 0, n), make([]int, 0, n)

	for i := 0; i < n; i++ {
		vals = append(vals, SingleInt())
		weis = append(weis, SingleInt())
	}

	// dp[i+1][w] := i 番目までの品物の中から重さが w を超えないように選んだときの、価値の総和の最大値

	// index + 1 に対して、

	dp := make([][]int, n+1)
	dp[0] = make([]int, ww+1)
	for i := 0; i < n; i++ {
		dp[i+1] = make([]int, ww+1)
		for w := 0; w <= ww; w++ {
			if w >= weis[i] {
				dp[i+1][w] = Max(dp[i][w-weis[i]]+vals[i], dp[i][w])
			} else {
				dp[i+1][w] = dp[i][w]
			}
		}
		// fmt.Printf("%#v\n", dp[i+1])
	}

	fmt.Printf("%#v\n", dp[n][ww])

	// a := int64(math.Pow(3, float64(n)))
	// fmt.Printf("%#v\n", (a-(3*dp1[n-1])+(3*dp2[n-1]))%mod)
}

func SingleInt() int {
	var n int
	fmt.Scan(&n)
	return n
}

func Max(nums ...int) int {
	if len(nums) == 0 {
		panic("function max() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Max(float64(res), float64(nums[i])))
	}
	return res
}

func Min64(nums ...int64) int64 {
	if len(nums) == 0 {
		panic("function min() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int64(math.Min(float64(res), float64(nums[i])))
	}
	return res
}