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: # 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] = dp[i+1][capa][1]
                if dp[i][capa][0] != dp[i+1][capa][0]:
                    dp[i][capa][1] = int(data[i][1])+dp[i+1][capa-int(data[i][1])][1]
    print("Case " + str(count) +":")
    print(max(max(dp))[0])
    print(max(max(dp))[1])
 
    count += 1

Python: for c in range(1,51):
    W = int(input())
    if W == 0: break
    N = int(input())
    dp = [0 for w in range(W+1)]
    for i in range(N):
        v,w = map(int,input().split(','))
        for j in range(W-w,-1,-1):
            if dp[j] == 0 and j > 0: continue
            dp[j+w] = max(dp[j+w], dp[j] + v)
    ansv = answ = 0
    for w,v in enumerate(dp):
        if v > ansv:
            ansv = v
            answ = w
    print('Case ' + str(c) + ':')
    print(ansv)
    print(answ)

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