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: def f():
 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])}')
f()

Python: cnt=0
while True:
    cnt+=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],dp[j-w]+v)
    wgt=0
    for i in range(W+1):
        if max(dp)==dp[i]:
            wgt=i
            break
    print("Case {0}:".format(cnt))
    print(max(dp))
    print(wgt)

Python: MAX = 1005
  
for cno in range(1, 100):
    lim = int(input())
    if lim == 0: break
    print("Case ", cno, ":", sep='')
    N = int(input())
    V = [[0 for j in range(MAX)] for i in range(2)]
    W = [[0 for j in range(MAX)] for i in range(2)]     
    k1, k2 = 0, 1
    for i in range(1, N+1):
        v, w = map(int, input().split(','))
        for j in range(1, w):
            V[k2][j] = V[k1][j]
            W[k2][j] = W[k1][j]
        for j in range(w, lim+1):
            d = j - w
            if V[k1][d] + v >= V[k1][j]:
                V[k2][j] = V[k1][d] + v
                W[k2][j] = W[k1][d] + w
            else:
                V[k2][j] = V[k1][j]
                W[k2][j] = W[k1][j]
        k1, k2 = k2, k1
    print(V[k1][lim], W[k1][lim], sep='\n')

Python: 

def rec(i, j):
	if dp[i][j] != -1:
		return dp[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
	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(2000)] for i in range(2000)]
	v, w = rec(0, W)
	
	print("Case {}:".format(case))
	print(v)
	print(w)
	case += 1

Python: memo = {}


def furoshiki(i, w_limit):
    global memo, treasures

    if not i or not w_limit:
        return (0, 0)

    if (i, w_limit) in memo:
        return memo[(i, w_limit)]

    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)
    memo.clear()
    case += 1

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)