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 <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <complex>
#include <functional>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cctype>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(c,i) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int loop=1,weight_max;cin>>weight_max,weight_max;loop++){
		int n;
		cin>>n;
		vi value(n),weight(n);
		rep(i,n)
			scanf("%d,%d",&value[i],&weight[i]);
		
		vi dp(weight_max+1);
		rep(i,n){
			vi dp2(dp.size());
			repi(j,weight[i],dp.size())
				dp2[j]=max(dp[j],dp[j-weight[i]]+value[i]);
			dp=dp2;
		}
		
		int res_value=0;
		int res_weight=0;
		rep(i,dp.size()){
			if(dp[i]>res_value){
				res_value=dp[i];
				res_weight=i;
			}
		}
		cout<<"Case "<<loop<<":"<<endl;
		cout<<res_value<<endl;
		cout<<res_weight<<endl;
	}
	
	return 0;
}

C++: #include <fstream>
#include <iostream>
#include <string.h>
#include <stdio.h>
#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 <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<string,string> P;


vector<P> graph; 
set<string> delay;
set<string> delay2;

int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int targetNum=0;
  int W,N;    
  while(cin >> W, W){
    targetNum++;
    cin >> N;

    int v[N], w[N];
    REP(i,N){
      int co,we;
      char b;
      cin >> co >> b >> we;
      v[i] = co;
      w[i] = we;
    }

    int dp[N+1][W+1];
    memset(dp,0,sizeof(dp));

    REP(i,N){
      REP(j,W+1){
	if( j - w[i] < 0 )
	  dp[i+1][j] = dp[i][j];
	else{
	  dp[i+1][j] = max( dp[i][j], dp[i][j-w[i]] + v[i] );
	}
      }
    }

    cout << "Case " << targetNum << ":" << endl;

    int ri=0,rj=0;
    REP(i,N){
      REP(j,W+1){
	if( dp[ri][rj] < dp[i][j] ){
	  ri = i;
	  rj = j;
	}
      }
    }

    cout << dp[ri][rj] << endl << rj-1 << endl;
  }

  return 0;
}