Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	Case := 1
	for {
		scanner.Scan()
		W, _ := strconv.Atoi(scanner.Text())
		if W == 0 {
			break
		}
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		w := make([]int, n+1)
		v := make([]int, n+1)
		for i := 1; i <= n; i++ {
			scanner.Scan()
			info := strings.Split(scanner.Text(), ",")
			v[i], _ = strconv.Atoi(info[0])
			w[i], _ = strconv.Atoi(info[1])
		}
		// fmt.Println(v)
		// fmt.Println(w)
		dp := make([][]int, n+1)
		for i := range dp {
			dp[i] = make([]int, W+1)
		}
		maxV := -(1 << 21)
		minW := 1 << 21
		for i := 1; i <= n; i++ {
			for j := 1; j <= W; j++ {
				// fmt.Println(dp)
				if j-w[i] >= 0 {
					dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i])
					if maxV < dp[i][j] {
						maxV = dp[i][j]
						minW = j
					} else if maxV == dp[i][j] {
						minW = min(minW, j)
					}
				} else {
					dp[i][j] = dp[i-1][j]
				}
			}
		}
		fmt.Printf("Case %d:\n", Case)
		Case += 1
		fmt.Println(maxV)
		fmt.Println(minW)
	}
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

Python: cnt = 1
while True:
    W = int(input())
    if W == 0:
        break
    N = int(input())
    dp = [0] * (W+1)
    for i in range(N):
        v, w = map(int, input().split(','))
        for j in range(W, w-1, -1):
            dp[j] = max(dp[j], dp[j-w] + v)
    
    av = 0
    aw = 0
    for i in range(W+1):
        if av < dp[i]:
            av = dp[i]
            aw = i

    print("Case {}:".format(cnt))
    print(av)
    print(aw)
    cnt += 1

Python: C_num = 1
while True:
    Nap = []
    W = int(input())

    if W == 0:
        break
    N = int(input())
    for i in range(N):
        v,w = map(int,input().split(","))
        Nap.append([v,w])
    DP = [[0 for j in range(W + 1)] for i in range(N + 1)]
    for i in range(1,N+1):
            for j in range(W+1):
                if j - Nap[i-1][1] >= 0:
                    DP[i][j] = max(DP[i-1][j],DP[i - 1][j - Nap[i - 1][1]] + Nap[i - 1][0])
                else:
                    DP[i][j] = DP[i-1][j]
    omo = DP[N].index(DP[N][W])
    print("Case ",C_num,":",sep = "",end = "\n")
    print(DP[N][W])
    print(omo)

Python: #coding:utf-8

import sys
import copy

def main():
    line = sys.stdin.readline()
    i = 1
    #case
    while int(line) != 0:
        analy(line, i)
        i = i + 1
        line = sys.stdin.readline()

def analy(line, case):
    #整列
    weight = int(line)
    num = int(sys.stdin.readline())
    data = []
    for i in range(num):
        a = sys.stdin.readline()
        b = [int(n) for n in a.split(",")]
        #価値、重さ
        data.append(b)
    #軽い順に並び替える,同じ重さなら価値の低い順になってる
    data.sort(key=lambda x:(x[1], x[0]))
    #重さ重複を削除
    i = 0
    while i < len(data)-1:
        if data[i][1] == data[i+1][1]:
            del data[i]
        else:
            i = i + 1
    num = len(data)
    yar(data, weight, num, case)

def yar(data, weight, num ,case):
    # 価値、重さ
    tmp = [[0,0]]
    kek = []
    calc(tmp, data, weight, kek, num)
    kek.sort(key=lambda x:(x[0], -x[1]))
    kek.reverse()
    print("Case " + str(case) + ':')
    print(kek[0][0])
    print(kek[0][1])

def calc(tmp, data, weight, kek, num):
    for j in range(num):
        a = len(tmp)
        for i in range(a):
        #とりあえず全てにおなじdataのやつを足してくわえる
            tlis = copy.deepcopy(tmp[i])
            tlis[0] = data[j][0] + tlis[0]
            tlis[1] = data[j][1] + tlis[1]
            if tlis[1] <= weight:
                tmp.append(tlis)
            else:
            #軽い順に並べてるのでそれがダメだと残りの要素すべて×、その後も×になる
                for k in range(i,a):
                    kek.append(tmp[i])
                    del tmp[i]
                break
        #軽い順に並び替える,同じ重さなら価値の低い順になってる
        tmp.sort(key=lambda x:(x[1],x[0]))      
    for j in range(len(tmp)):
        kek.append(tmp[j])

if __name__ == "__main__":
    main()

Python: class Knapack(object):
    def __init__(self, data, limit):
        self.limit = limit
        data_size = len(data)
        self.C = [[0]*(limit+1) for _ in range(data_size+1)]
        self.data = data[:]

    def solve(self):
        for i in range(1, len(self.data)+1):
            i_value = self.data[i-1][0]
            i_weight = self.data[i-1][1]
            for w in range(1, self.limit+1):
                if i_weight <= w:
                    if i_value + self.C[i-1][w-i_weight] > self.C[i-1][w]:
                        self.C[i][w] = i_value + self.C[i-1][w-i_weight]
                    else:
                        self.C[i][w] = self.C[i-1][w]
                else:
                    self.C[i][w] = self.C[i-1][w]


if __name__ == '__main__':
    # ??????????????\???
    # limit = 50
    # data = [[60, 10], [100, 20], [120, 30], [210, 45], [10, 4]]
    case_no = 1
    while True:
        limit = int(input())
        if limit == 0:
            break
        num_of_items = int(input())
        data = []
        for i in range(num_of_items):
            data.append(list(map(int, input().split(','))))

        # ????????????????????????????§£???
        ks = Knapack(data, limit)
        ks.solve()

        # ???????????¨???
        target = ks.C[len(data)][limit]
        weight = limit
        for w in range(limit, 0, -1):
            if ks.C[len(data)][w] == target:
                weight = w
        print('Case {0}:'.format(case_no))
        print('{0}\n{1}'.format(target, weight))
        case_no += 1

Python: #coding:utf-8

from __future__ import division, print_function
try:
    input = raw_input
    range = xrange
except NameError:
    pass


def solve(cnt, W, N, treasures):
    dp = [0] + [-1] * W
    for v, w in treasures:
        for j in range(W, w - 1, -1):
            if dp[j - w] >= 0:
                dp[j] = max(dp[j], dp[j - w] + v)

    ans = max(dp)
    print('Case %d:\n%d\n%d' % (cnt, ans, dp.index(ans)))

cnt = 1
while True:
    W = int(input())
    if W == 0:
        break
    N = int(input())
    
    treasures = [tuple(map(int, input().split(','))) for _ in range(N)]
    solve(cnt, W, N, treasures)

    cnt += 1

Python: 

def rec(i, j):
	#print("rec({}, {}) called".format(i, j))
	"""
	if dp[i][j] != -1:
		return dp[i][j]
	"""
	if i in dphash and j in dphash[i]:
		return dphash[i][j]
	if i == N:
		ans = (0, W - j)
	elif j < wl[i]:
		ans = rec(i + 1, j)
	else:
		(v1, w1) = rec(i + 1, j)
		(v2, w2) = rec(i + 1, j - wl[i])
		v2 += vl[i]
		if v1 > v2:
			ans = (v1, w1)
		elif v1 < v2:
			ans = (v2, w2)
		elif v1 == v2:
			if w1 < w2:
				ans = (v1, w1)
			else:
				ans = (v2, w2)
	#dp[i][j] = ans
	if i in dphash:
		dphash[i][j] = ans
	else:
		dphash[i] = {}
		dphash[i][j] = ans

	return ans

case = 1
while True:
	W = int(input())
	if W == 0: break
	N = int(input())
	vl = []
	wl = []
	for _ in range(N):
		v, w = map(int, input().split(','))
		vl.append(v)
		wl.append(w)
	#dp = [[-1 for j in range(2500)] for i in range(2500)]
	dphash = {}
	(v, w) = rec(0, W)

	print("Case {}:".format(case))
	print(v)
	print(w)
	case += 1