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
}

C++: #include <cstdio>

using namespace std;

void rec(int n, int w, int &v, int &rw,
        const int W, const int N,
        const int lv[], const int lw[])
{
    v = rw = 0;
    if (n == N) {
        // not remain any treasure
        v = 0;
        rw = w;
    } else if (w < lw[n]) {
        // cannot pack treasure[n]
        rec(n + 1, w, v, rw, W, N, lv, lw);
    } else {
        int v1, v2, w1, w2;

        // through treasure[n]
        rec(n + 1, w        , v, rw, W, N, lv, lw);
        v1 = v;
        w1 = rw;
        // pack treasure[n]
        rec(n + 1, w - lw[n], v, rw, W, N, lv, lw);
        v2 = v + lv[n];
        w2 = rw - lw[n];

        // which is good
        if (v1 > v2) {
            v = v1;
            rw = w1;
        } else if (v1 < v2) {
            v = v2;
            rw = w2;
        } else {
            if (w1 >= w2) {
                v = v1;
                rw = w1;
            } else {
                v = v2;
                rw = w2;
            }
        }
    }
    // printf("  (%d, %d, %d, %d) -> (%d, %d)\n", n, w, v, rw, v, rw);
}

#define SIZE 1001
int dp_v[SIZE][SIZE];
int dp_rw[SIZE][SIZE];

void rec_dp(int n, int w, int &v, int &rw,
        const int W, const int N,
        const int lv[], const int lw[])
{
    // memo'd
    if (dp_v[n][w] != -1 && dp_rw[n][w] != -1) {
        v = dp_v[n][w];
        rw = dp_rw[n][w];
        return;
    }

    v = rw = 0;
    if (n == N) {
        // not remain any treasure
        v = 0;
        rw = w;
    } else if (w < lw[n]) {
        // cannot pack treasure[n]
        rec_dp(n + 1, w, v, rw, W, N, lv, lw);
    } else {
        int v1, v2, w1, w2;

        // through treasure[n]
        rec_dp(n + 1, w        , v, rw, W, N, lv, lw);
        v1 = v;
        w1 = rw;
        // pack treasure[n]
        rec_dp(n + 1, w - lw[n], v, rw, W, N, lv, lw);
        v2 = v + lv[n];
        w2 = rw;

        // which is good
        if (v1 > v2) {
            v = v1;
            rw = w1;
        } else if (v1 < v2) {
            v = v2;
            rw = w2;
        } else {
            if (w1 >= w2) {
                v = v1;
                rw = w1;
            } else {
                v = v2;
                rw = w2;
            }
        }
    }
    // printf("  (%d, %d, %d, %d) -> (%d, %d)\n", n, w, v, rw, v, rw);
    dp_v[n][w] = v;
    dp_rw[n][w] = rw;
}

int main()
{
    for (int icase = 1;; ++icase) {
        int W, N;
        scanf("%d", &W);
        if (W == 0)
            break;
        scanf("%d", &N);

        int v[1000], w[1000];
        for (int i = 0; i < N; ++i)
            scanf("%d,%d", &v[i], &w[i]);

        int retv, retw;
        for (int i = 0; i < SIZE; ++i)
            for (int j = 0; j < SIZE; ++j)
                dp_v[i][j] = dp_rw[i][j] = -1;
        rec_dp(0, W, retv, retw, W, N, v, w);

        printf("Case %d:\n%d\n%d\n", icase, retv, W - retw);
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int mp[1001][1001];

int main(){
	int W;char c;
	for(int num = 0;cin >> W&&W;num++){
		int v[1001], w[1001], n;
		cin >> n;
		fill((int*)mp, (int*)(mp+n+1), 0);
		for(int i = 0;i < n;i++)cin >> v[i] >> c >> w[i];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < W+1;j++){
				if(j < w[i])mp[i+1][j] = mp[i][j];
				else mp[i+1][j] = max(mp[i][j], mp[i][j-w[i]]+v[i]);
			}
		}
		int ans = 0;
		for(int i = 1;i < W+1;i++){
			if(mp[n][i] > mp[n][ans])ans = i;
		}
		cout << "Case " << num+1 << ":" << endl;
		cout << mp[n][ans] << endl << ans << endl;
	}
	return 0;
}
/*
1
y eazqyp pnop pngtg ye obmpngt xmybp mr lygw
*/

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
 
#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
 
using namespace std;
 
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
 
int main(void){
    for(int w, n, cnt = 0; cin >> w >> n, w;){
        vi dp(w + 1); fill(ALL(dp), -1);
        dp[0] = 0;

        char c;
        while(n --){
            int tv, tw; cin >> tv >> c >> tw;
            for(int i = w; 0 <= i; i --) if(i + tw <= w && dp[i] >= 0) dp[i + tw] = max(dp[i + tw], dp[i] + tv);
        }
        
        vi::iterator it = max_element(ALL(dp));
        cout << "Case " << ++ cnt << endl << *it << endl << it - dp.begin() << endl;
    }
 
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(void){
    for(int x=1;;x++){
        int l;
        int n;
        int v[1010]={0};
        int w[1010]={0};
        int dp[10][1010]={0};
        int we[10][1010]={0};
        cin >> l;
        if(l==0)break;
        cin >> n;
        for(int i=1;i <= n;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        for(int i=1;i <= n;i++){
            for(int j=0;j <= l;j++){
                if(j < w[i]){
                    dp[2][j] = dp[1][j];
                    we[2][j] = we[1][j];
                }
                else{
                    if(dp[1][j]>dp[1][ j-w[i] ] + v[i]){
                        dp[2][j] = dp[1][j];
                        we[2][j] = we[1][j];
                    }
                    else{
                        dp[2][j] = dp[1][ j-w[i] ] + v[i];
                        we[2][j] = we[1][ j-w[i] ]+w[i];
                    }
                }
            }
            /*for(int k=0;k<=l;k++)cout<<we[i+1][k]<<" ";
            cout<<endl;*/
            for(int k=0;k <= l;k++){
                dp[1][k] = dp[2][k];
                we[1][k] = we[2][k];
            }
        }
        /*for(int i=1;i<=n;i++){
            for(int j=0;j<=l;j++){
            cout<<dp[i][j]<<" ";
            }
            cout<<endl;
        }*/
        cout<<"Case "<<x<<":"<<endl;
        cout<<dp[2][l]<<endl;
        cout<<we[2][l]<<endl;

    }
	return 0;
}