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

C++: #include <bits/stdc++.h>
using namespace std;

#define  req(i,r,k) for(int i=k;i<r;i++)
#define  rep(i,r) req(i,r,0)

const long long int INF = 100000000000000;

long long int dp[1001][1001];

int main() {

    for(int a=1;1;a++){
        int w,n;
        scanf(" %d",&w);
        if(w==0) break;
        scanf(" %d",&n);
        pair<int,int> t[n];
        char c;
        rep(i,n){
            scanf(" %d%c%d",&t[i].first,&c,&t[i].second);
        }

        rep(i,w){
            dp[0][i] = 0;
            dp[i][0] = 0;
        }

        dp[0][0] = 0;
        rep(i,n) {
            rep(j,w) {
                if(j >= t[i].second) {
                    dp[i+1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first);
                }
                else{
                    dp[i+1][j] = dp[i][j];
                }
            }
        }
        long long int maxvalue=0,thenweight=0;

        rep(i,w+1){
            //cout << dp[i] << " " << i << " " << thenweight << endl;
            if(maxvalue < dp[n][i]){
                maxvalue = dp[n][i];
                thenweight = i;
            }
        }

        //printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight);
        cout << "Case " << a << ':' << endl;
        cout << maxvalue << endl;
        cout << thenweight << endl;
    }
    return 0;
}