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: def dfs_dp(w, v, N, W, index, dp):
    if (dp[index][W][0] != -1):
        return dp[index][W]
    if index==N: return [0,0]
    if W<w[index]: return dfs_dp(w, v, N, W, index + 1, dp)

    temp1=dfs_dp(w, v, N, W, index + 1, dp)
    temp=dfs_dp(w, v, N, W - w[index], index + 1, dp)
    temp2=[v[index] + temp[0],w[index]+temp[1]]
    if temp1[0]==temp2[0]:
        if temp1[1]<temp2[1]:
            ret=temp1
        else: ret=temp2
    elif temp1[0]>temp2[0]:
        ret=temp1
    else: ret=temp2

    dp[index][W]=ret
    return ret


count=1
W=int(input())
while(W!=0):
    N=int(input())
    w=[]
    v=[]
    dp=[[[-1,-1] for i in range(W+1)] for j in range(N+1)]
    for i in range(N):
        a,b=input().split(',')
        v.append(int(a))
        w.append(int(b))

    sum=dfs_dp(w,v,N,W,0,dp)

    print("Case "+str(count)+":")
    print(sum[0])
    print(sum[1])
    W=int(input())
    count+=1

Python: c=1
while 1:
    W=int(input())
    if W==0:break
    dp=[0]*(W+1)
    for i in range(int(input())):
        v,w=map(int, input().split(','))
        for j in range(W,w-1,-1):
            if dp[j]<dp[j-w]+v:dp[j]=dp[j-w]+v
    for i in range(W+1):
        if dp[W]==dp[i]:break
    print('Case %d:\n%d\n%d'%(c,dp[W],i))
    c+=1

Python: # coding: utf-8
# Here your code !
count = 1

while 1:
    W = int(input())
    if W == 0:
        break
    N = int(input())
    data =[]
    dp = [[[-1,0]for i in range(W+1)]for i in range(N+1)]
    for i in range(N):
        data.append(input().split(","))

    for i in range(W+1):
        dp[N][i][0] = 0
    for i in reversed(range(N)):
        for capa in range(W+1):
            if int(data[i][1]) > capa:
                dp[i][capa][0] = dp[i+1][capa][0]
                dp[i][capa][1] = dp[i+1][capa][1]
            else:
                dp[i][capa][0] = max(int(data[i][0])+dp[i+1][capa-int(data[i][1])][0],dp[i+1][capa][0])
                dp[i][capa][1] = capa
    print("Case " + str(count) +":")
    print(max(max(dp))[0])
    print(max(max(dp))[1])
    count += 1

Python: caseN = 0
while True:
    caseN += 1
    W = int(input())
    if W == 0: break
    N = int(input())
    tr = [list(map(int, input().split(','))) for _ in range(N)]
    dp = [[0 for x in range(W+1)] for y in range(N+1)]
    for x in range(N):
        for y in range(W+1):
            if y >= tr[x][1]:
                dp[x+1][y] = max(dp[x+1][y], dp[x][y-tr[x][1]]+tr[x][0])
    sum_weight = 0
    sum_value = 0
    for x in range(W+1):
        if dp[N][x] > sum_value:
            sum_weight = x
            sum_value = dp[N][x]
    print("Case ", caseN, ":", sep='')
    print(sum_value, sum_weight, sep='\n')

Python: times=0
while True:
    times+=1
    W=int(input())
    n=int(input())
    if W==0:
        break
    w=[0]*n
    v=[0]*n
    for i in range(n):
        v[i],w[i]=map(int,input().split(","))
    dp=[[0]*(W+1) for i in range(n+1)]
    for i in range(n):
        for j in range(W+1):
            if j<w[i]:
                dp[i+1][j]=dp[i][j]
            else:
                dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i])
    MIN=0
    for i in range(W+1):
        if dp[n][i]==dp[n][W]:
            MIN=i
            break
    print("Case %d:"%times)
    print(dp[n][W])
    print(MIN)

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve(k):
    W = int(readline())
    if W == 0:
        return False
    INF = 10**18
    dp = [-INF]*(W+1)
    dp[0] = 0
    N = int(readline())
    for i in range(N):
        v, w = map(int, readline().split(","))
        for j in range(W-w, -1, -1):
            dp[j+w] = max(dp[j+w], dp[j] + v)
    vm = max(dp)
    wm = dp.index(vm)
    write("Case %d:\n%d\n%d\n" % (k, vm, wm))
    return True
i = 1
while solve(i):
    i += 1

Python: def ns(vs, ws, i, j):
    if i == len(vs):
        return [0, 0]
    else:
        if ws[i] > j:
            return ns(vs, ws, i+1, j)
        else:
            vwoi, wwoi = ns(vs, ws, i+1, j)
            tmp = ns(vs, ws, i+1, j-ws[i])
            return max(ns(vs, ws, i+1, j), [vs[i]+tmp[0], ws[i]+tmp[1]], key = lambda x: x[0])

casenum = 1
while True:
    W = int(input())
    if W == 0:
        break
    N = int(input())
    vs, ws = [], []
    for _ in range(N):
        v, w = map(int, input().split(','))
        vs.append(v)
        ws.append(w)

    v, w = ns(vs, ws, 0, W)
    print('Case ' + str(casenum) + ':')
    print(v)
    print(w)
    casenum += 1

Python: def furoshiki(i, w_limit):
    global treasures
    if not i or not w_limit:
        return (0, 0)

    v, w = treasures[i - 1]
    not_stole = furoshiki(i - 1, w_limit)
    if w_limit < w:
        return not_stole
    stole_v_total, stole_w_total = furoshiki(i - 1, w_limit - w)
    stole_v_total += v
    stole_w_total -= w
    return max(not_stole, (stole_v_total, stole_w_total))


case = 1
while True:
    w = int(input())
    if not w:
        break
    n = int(input())
    treasures = [None] * n
    for i in range(n):
        treasures[i] = tuple(map(int, input().split(',')))

    total_v, total_w = furoshiki(n, w)
    print('Case {}:'.format(case))
    print(total_v)
    print(-total_w)
    case += 1

Python: from itertools import combinations
case = 0
while True :
    limit = int(input())
    if limit == 0 : break
    case += 1
    number = int(input())
    
    value, weight = [], []
    for _ in range(number) :
        a, b = input().split(',')
        value.append(int(a))
        weight.append(int(b))
    
    result = [0, 0]
    for _ in range(1, number+1) :
        temp1 = [sum(_2) for _2 in list(combinations(value, _))]
        temp2 = [sum(_2) for _2 in list(combinations(weight, _))]
        for _2 in range(len(temp1)) :
            if (result[0] < temp1[_2] and temp2[_2] <= limit) or (result[0] == temp1[_2] and temp2[_2] < result[1]) : result = temp1[_2], temp2[_2]
    print('Case {}:'.format(case))
    print(result[0])
    print(result[1])