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++: //
//  others.cpp
//  sample
//
//  Created by MIO-MACMINI on 2015/09/01.
//  Copyright (c) 2015??´ MIO-MACMINI. All rights reserved.
//

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define READ_BY_TEXT false

vector<string> splitByDelimiter(string target, string delim) {
    std::string s = target;
    std::string delimiter = delim;
    size_t pos = 0;
    std::string token;
    vector<string> arr;
    while ((pos = s.find(delimiter)) != std::string::npos) {
        token = s.substr(0, pos);
        arr.push_back(token);
        s.erase(0, pos + delimiter.length());
    }
    arr.push_back(s);
    return arr;
}

int W, N;
int w[1000], v[1000];
int dp[1000][1000];
//vector<int> lines;
//int max_v, min_w;

int recursive(int i, int j) {
    
    if (dp[i][j] != -1) return dp[i][j];
    
    int ret;
    if (i >= N) {
        ret = 0;
    }
    else if (j < w[i]) {
        ret = recursive(i+1, j);
    }
    else {
        ret = max(recursive(i+1, j), recursive(i+1, j-w[i])+v[i]);
    }
    dp[i][j] = ret;
    
    return ret;
}

int calcMinWeight(int i, int j) {
    
    if (i >= N) {
        return 0;
    }
    
    int m_w = W;
    if (dp[i][j] == -1) {
        m_w = min(m_w, calcMinWeight(i+1, j));
    }
    else {
        m_w = min(m_w, calcMinWeight(i+1, j-w[i]) + w[i]);
    }
    
    return m_w;
}

int main() {
    
#if READ_BY_TEXT
    // Get values from test.txt
    std::ifstream ifs("test.txt");
    std::string str;
    if (ifs.fail())
    {
        std::cerr << "??±???" << std::endl;
        return -1;
    }
    getline(ifs, str);
    vector<string> words = splitByDelimiter(str, " ");
    num_d = stoi(words[0]);
    num_n = stoi(words[1]);
    for (int i=0; i<num_n; i++) {
        getline(ifs, str);
        lines.push_back(stoi(str));
    }
#else
    
    int case_v = 1;
    while (true) {
        cin >> W;
        if (!W) break;
        cin >> N;
        string input;
        for (int i = 0; i < N; i++) {
//            getline(cin, input);
            cin >> input;
            vector<string> words = splitByDelimiter(input, ",");
            v[i] = stoi(words[0]);
            w[i] = stoi(words[1]);
        }
        memset(dp, -1, sizeof(dp));
        
        // Solve
        int min_w = W;
        int max_v = recursive(0, W);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j <= W; j++) {
//                cout << "dp[" << i << "][" << j << "] = " << dp[i][j] << endl;
                if (dp[i][j] == max_v) {
//                    cout << "i " << i << " j " << j << endl;
                    min_w = min(min_w, calcMinWeight(i, j));
//                    min_w = min(j, min_w);
                }
            }
        }
        
        cout << "Case " << case_v << ":" << endl;
        cout << max_v << endl;
        cout << min_w << endl;
        case_v++;
    }
    
//    string input;
//    getline(cin, input);
//    vector<string> words = splitByDelimiter(input, " ");
//    num_d = stoi(words[0]);
//    num_n = stoi(words[1]);
//    string input1;
//    for (int i=0; i<num_n; i++) {
//        getline(cin, input1);
//        lines.push_back(stoi(input1));
//    }
#endif
    
//    recursive(0, 0, "");
//    cout << result << endl;
    
    //    cout << "num_d = " << num_d << " num_n = " << num_n << "\n";
    //
    //    // Check parsed contents
    //    for (int i = 0; i < lines.size(); i++) {
    //        cout << "line[" << i << "] " << to_string(lines[i]) << "\n";
    //    }
    
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

void q42() {
	int w, n;
	int c = 0;
	for (; cin >> w >> n;) {
		c++;
		int value[1001];
		int weight[1001];
		int dp[1001];
		for (int i = 0; i < n; i++) {
			int v_i, w_i;
			cin >> v_i >> w_i;
			value[i] = v_i;
			weight[i] = w_i;
		}
		for (int i = 0; i < n; i++) {
			for (int j = w; j >= weight[i]; j--) {
				dp[j] = dp[j - weight[i]] + value[i];
			}
		}
		int max_value = 0;
		int max_weight = 0;
		for (int i = 1; i <= w; i++) {
			if (dp[i] > max_value) {
				max_value = dp[i];
				max_weight = i;
			}
		}
		cout << "Case " << c << ":" << endl;
		cout << max_value << endl;
		cout << max_weight << endl;
	}
}

int main() {
	q42();
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct treasure{
	int value;
	int weight;
};
bool comp(treasure t1, treasure t2){
	if(t1.weight <= t2.weight) return true;
	else return false;
}

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	while(cin >> w){
		int n;
		int cost[maxw + 1];
		int best[maxw + 1];
		for(int i = 0; i <= maxw; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		vector<treasure> treasures;
		cin >> n;
		for(int i = 0; i < n; i++){
			treasure t;
			char c;
			cin >> t.value >> c >> t.weight;
			treasures.push_back(t);
		}
		sort(treasures.begin(), treasures.end(), comp);
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= treasures[j].weight)
					if(cost[k] < cost[k - treasures[j].weight] + treasures[j].value)
					{
						cost[k] = cost[k - treasures[j].weight] + treasures[j].value;
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;(w>0) && (cost[w] == cost[w-1]); w--);
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - treasures[best[l]].weight >= 0;){
			if(l - treasures[best[l]].weight >= 0){
				total += treasures[best[l]].weight;
				l -= treasures[best[l]].weight;
			}
		}
		cout << total << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int a[1001] = { 0 };
int b[1001] = { 0 };
int w, n, mv, mw;
void solve(int W, int f, int sum) {
	f = min(f, n) + 1;
	for (int i = f; i < n; i++) {
		if (W - b[i] < 0) {
			mw = max(w - W, mw);
			if (sum == mv) mw = min(w - W, mw);
			mv = max(sum, mv);
			continue;
		}
		solve(W - b[i], f, sum + a[i]);
	}
}

int main() {
	int u = 0;
	while (scanf("%d", &w)) {
		if (w == 0) break;
		scanf("%d", &n);

		mv = 0;
		mw = w;
		u++;
		for (int i = 0; i < n; i++) {
			scanf("%d,%d", &a[i], &b[i]);
		}

		solve(w, -1, 0);
		printf("Case %d:\n%d\n%d\n", u, mv, mw);
	}
	return 0;
}