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

Python: mininf = -10**8
inf = 10**8
count = 0

while(1):
    count += 1
    W = int(input())
    if W == 0:
        break
    N = int(input())

    dp = [0 for j in range(W+1)]
    for _ in range(N):
        v,w = [int(i) for i in input().split(",")]
        for i in range(w,W+1)[::-1]:
            dp[i] = max(dp[i], dp[i-w]+v)
    value = 0
    weight = 0
    for i in range(W+1):
        if value < dp[i]:
            value = dp[i]
            weight = i

    print("Case {}:".format(count))
    print(value)
    print(weight)

Python: c=0
for W in iter(input,'0'):
 c+=1
 W=int(W)
 d=[0]*-~W
 for _ in[0]*int(input()):
  v,w=map(int,input().split(','))
  for i in range(W,w-1,-1):
   if d[i]<d[i-w]+v:d[i]=d[i-w]+v
 print(f'Case {c}:\n{d[W]}\n{d.index(d[W])}')

Python: c=0
while True:
    W=int(input())#重さの上限
    if W==0:
        break
    c+=1
    N=int(input())#宝物の数

    vl=[0]#価値のリスト
    wl=[0]#重さのリスト
    for i in range(N):
        [v,w]=list(map(int,input().split(",")))#vとwのリストを作る
        vl.append(v)
        wl.append(w)
    #横がW+1で縦がN+1の表を作成する
    dp=[[0 for i in range(W+1)]for j in range(N+1)]
    #表を埋める
    for i in range(N+1):
        for j in range(W+1):
            if i==0 or j==0:
                dp[i][j]=0
            else:
                if wl[i]<=j:
                    dp[i][j]=max(dp[i-1][j-wl[i]]+vl[i],dp[i-1][j])
                else:
                    dp[i][j]=dp[i-1][j]

    #print(dp)
    ls=dp[N].copy()
    ls.reverse()
    #print(ls)
    max_w=W
    for i in range(1,W+1):
        if ls[i]==dp[N][W]:
            max_w-=1
        else:
            break
    print("Case "+str(c)+":")
    print(dp[N][W])
    print(max_w)

Python: #!/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import division, print_function
try:
    input = raw_input
    range = xrange
except NameError:
    pass
# 上記のコードはPython2とPython3の違いを一部吸収するためのもの


def main():
    value = [0] * 1000
    weight = [0] * 1000
    casenum = 1
    while True:
        W = int(input())
        if(W == 0):
            return
        N = int(input())
        dp = [[0 for x in range(W+1)] for y in range(N+1)]
        for x in range(0, N):
            value[x], weight[x] = map(int, input().split(','))
        for i in range(0, N):
            for w in range(0, W+1):
                if w - weight[i] >= 0:
                    use = dp[i][w - weight[i]] + value[i]
                else:
                    use = 0
                unuse = dp[i][w]
                dp[i+1][w] = max(use, unuse)
        print("Case " + str(casenum) + ":")
        for w in range(W+1):
            if(dp[N][w] == dp[N][W]):
                print(str(dp[N][w]) + "\n" + str(w))
                break
        casenum += 1
main()

Python: c=0
for W in iter(input,'0'):
 c+=1
 W=int(W)
 dp=[0]*-~W
 for _ in[0]*int(input()):
  v,w=map(int,input().split(','))
  for i in range(W,w-1,-1):
   if dp[i]<dp[i-w]+v:dp[i]=dp[i-w]+v
 for i in range(W+1):
  if dp[W]==dp[i]:print(f'Case {c}:\n{dp[W]}\n{i}');break

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

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)

    dp = [[[-1, -1] for _ in range(W+1)] for __ in range(N+1)]
    v, w = ns(vs, ws, 0, W, dp)
    print('Case ' + str(casenum) + ':')
    print(v)
    print(-w)
    casenum += 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):
            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] = dp[i+1][capa][1]
                if dp[i][capa][0] != dp[i+1][capa][0]:
                    dp[i][capa][1] += int(data[i][1])

    print("Case " + str(count) +":")
    print(max(max(dp))[0])
    print(max(max(dp))[1])
    count += 1

Python: import re
def solve(w,v):
    n = len(v)
    val0 = 0
    wgt0 = 0
    for i in range(1,2**n):
        s = bin(i)
        s1 = '0'*n + re.sub(r'0b', '', s)
        t = list(map(int, list(s1[-n:])))
        val = wgt = 0
         
        for i in range(n):
            if t[i]==1:
                val += v[i][0]  
                wgt += v[i][1]  
                if wgt > w:
                    break
        if wgt > w:
            continue
        if val > val0 or (val==val0 and wgt<wgt0):
            val0 = val
            wgt0 = wgt
    return(val0,wgt0)

case=0
while True:
    case += 1
    f = []
    wlim = int(input().strip())
    if wlim==0:
        break
    n = int(input().strip())
    for i in range(n):
        v,w = map(int,input().strip().split(','))
        f.append((v,w))
    
    val,wgt = solve(wlim,f)
    
    print('Case %d:' % case)
    print(val)
    print(wgt)