Go: package main

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

const MaxW = 1000
const MaxN = 1000

type Tuple struct {
	Value  int
	Weight int
}

func nextLine(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func solve(w int, n int, vws []Tuple) Tuple {
	var dp [MaxN + 1][MaxW + 1]int

	for i := 0; i < n; i++ {
		for j:= w; j > 0; j-- {
			if j < vws[i].Weight {
				dp[i + 1][j] = dp[i][j]
			} else {
				dp[i + 1][j] = int(math.Max(float64(dp[i][j]), float64(dp[i][j - vws[i].Weight] + vws[i].Value)))
			}
		}
	}

	maxValue := dp[n][w]
	minWeight := w

	for j := w - 1; j > 0; j-- {
		if maxValue == dp[n][j] {
			minWeight = j
		} else {
			break
		}
	}

	return Tuple{Value: maxValue, Weight: minWeight}
}

func main() {
	var sc = bufio.NewScanner(os.Stdin)
	index := 1
	for {
		var w, n int
		w, _ = strconv.Atoi(nextLine(sc))
		if w == 0 {
			return
		}
		n, _ = strconv.Atoi(nextLine(sc))

		vws := make([]Tuple, n, n)
		for i := 0; i < n; i++ {
			array := strings.Split(nextLine(sc), ",")
			v, _ := strconv.Atoi(array[0])
			w, _ := strconv.Atoi(array[1])
			vws[i] = Tuple{Value: v, Weight: w}
		}

		t := solve(w, n, vws)
		fmt.Printf("Case %d:\n", index)
		fmt.Println(t.Value)
		fmt.Println(t.Weight)
		index++
	}
}

Python: 
# メモ化+非再帰を用いた場合
case_no = 1
while True:
    W = int(input())
    if W == 0:
        break
    N = int(input())
    treasures = [tuple(map(int, input().split(','))) for _ in range(N)]
    
    V = [[0 for w in range(W+1)] for i in range(N+1)]
    weight = [[0 for w in range(W+1)] for i in range(N+1)]
    for i in range(1, N+1):
        for w in range(W+1):
            vi = treasures[i-1][0]
            wi = treasures[i-1][1]
            if wi <= w and vi != 0:
                l = V[i-1][w-wi] + vi
                r = V[i-1][w]
                if l > r:
                    V[i][w] = l
                    weight[i][w] = weight[i-1][w-wi] + wi
                else:
                    V[i][w] = r
                    weight[i][w] = weight[i-1][w]
            else:
                V[i][w] = V[i-1][w]
                weight[i][w] = weight[i-1][w]
    
    sum_value = 0
    sum_weight = 1e100
    for i in range(1, N+1):
        for j in range(W+1):
            if V[i][j] > sum_value or (V[i][j] == sum_value and weight[i][j] < sum_weight):
                sum_value = V[i][j]
                sum_weight = weight[i][j]
    
    print('Case {}:'.format(case_no))
    print(sum_value)
    print(sum_weight if sum_weight != 1e100 else 0)
    case_no += 1

Python: from sys import stdin

case = 0
while(True):
    case += 1
    W = int(stdin.readline())
    if not W: break
    print("Case %d:"%case)
    N = int(stdin.readline())
    v = [0] * N
    w = [0] * N
    dp = [[0]*(W + 1) for _ in range(N + 1)]

    for k in range(N):
        v[k], w[k] = map(int, stdin.readline().split(","))
    for i in range(N-1,-1,-1):
        for j in range(W+1):
            if j < w[i]:
                dp[i][j] = dp[i + 1][j]
            else:
                dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i])
    print(dp[0][W])
    for i in range(W+1):
        if dp[0][i] == dp[0][W]: print(i); break

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) :
        for a, b in zip(list(combinations(value, _)), list(combinations(weight, _))) :
            if (result[0] < sum(a) and sum(b) <= limit) or (result[0] == sum(a) and sum(b) < result[1]) :
                result = sum(a), sum(b)
    print('Case {}:'.format(case))
    print(result[0])
    print(result[1])

Python: nCase = 0
while True:
    W = int(input())
    if W == 0: break
    nCase += 1
    N = int(input())
    # [(value, weight), ... ]
    items = [tuple(map(int, input().split(','))) for _ in range(N)]
    bestValueFor = [0] * (W + 1) # item -> best value, idx -> capacity 
    for item in items:
        for capa in reversed(range(W-item[1]+1)):
            # if taken wins not taken
            if bestValueFor[capa] + item[0] > bestValueFor[capa + item[1]]:
                bestValueFor[capa + item[1]] = bestValueFor[capa] + item[0]
    bestValue = max(bestValueFor)
    bestWeight = bestValueFor.index(bestValue)
    print('Case ',str(nCase),':',sep='')
    print(bestValue)
    print(bestWeight)

Python: c=1
while 1:
    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-w]+v,dp[j])
    for i in range(W):
        if dp[W]==dp[i]:break
    print('Case %d\n%d\n%d'%(c,dp[W],i))
    c+=1

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()