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 <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 <cstdio>
#include <algorithm>
using namespace std;

int main(){
	int W;

	for(int t=1 ; scanf("%d", &W) , W ; t++ ){
		int n, v[1002]={0}, w[1002]={0};
		int dp[1002][1002] = {0};
		
		scanf("%d", &n);
		for(int i=0 ; i < n ; i++ ){
			scanf("%d,%d", &v[i] , &w[i] );
		}
		for(int i=0 ; i < n ; i++ ){
			for(int j=0 ; j <= W ; j++ ){
				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] );
				}
			}
		}
		int total;
		for(int j=W ; j >= 0 ; j-- ){
			if( dp[n][W] == dp[n][j] ){
				total = j; 
			}
		}
		printf("Case %d:\n%d\n%d\n", t , dp[n][W] , total );
	}
}

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

int main(){
  int w;
  int t=0;
  while(cin >> w && w){
    int n,dp[2][1001],wait[1000],value[1000];
    cin >> n;
    for(int i=0;i<n;i++) scanf("%d,%d",&value[i],&wait[i]);

    fill(dp[0],dp[2],0);
    for(int i=0;i<n;i++){
      for(int j=wait[i];j<=w;j++) dp[1][j] = max(dp[1][j], dp[0][j-wait[i]]+value[i]);
      for(int j=0;j<=w;j++) dp[0][j] = dp[1][j];
    }

    int ans = 0, answ;
    for(int i=0;i<=w;i++){
      if(ans < dp[0][i]){
        ans = dp[0][i];
        answ = i;
      }
    }

    cout << "Case " << ++t << ':' << endl;
    cout << ans << endl;
    cout << answ << endl;
  }
}

C++: #include <iostream>
#include <bitset>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
//#include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);


using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const Int MOD = 1000000007;

Int mod_pow(Int x, Int n) {
    Int res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x) % MOD; //ビット演算(最下位ビットが1のとき)
        x = (x * x) % MOD;
        n >>= 1; //右シフト(n = n >> 1)
    }
    return res;
}

Int v[1005];
Int w[1005];
Int dp[1005][1005];
vector<Int> ans;
vector<Int> ans2;

void solve(Int W){
    Int N; cin >> N;
    memset(v, 0, sizeof(v));
    memset(w, 0, sizeof(w));
    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < N; i++){
        char c;
        cin >> v[i] >> c >> w[i];
    }
    for (int i = 0; i < N; i++){
        for (int j = 0; j <= W; j++){
            if (j+w[i] <= W) dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]], dp[i][j] + v[i]);
            dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
        }
    }
    ans.push_back(dp[N][W]);
    for (int i = W; i >= 0; i--){
        if (dp[N][i] != dp[N][W]){
            ans2.push_back(i+1);
            break;
        }
    }

}

int main(){
    cin.tie(0);

    while (1){
        Int W; cin >> W;
        if (!W) break;
        solve(W);
    }
    for (int i = 0; i < ans.size(); i++){
        cout << "Case " << i+1 << ":" << endl;
        cout << ans[i] << endl;
        cout << ans2[i] << endl;
    }
}