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 <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }



int W, N;
int w[2000], v[2000];
int dp[2000][2000];

int main() {
	int it = 1;
	while (cin >> W >> N) {
		if (W == 0) break;

		for (int i = 0; i < N; ++i) scanf("%d,%d", &v[i], &w[i]);
		memset(dp, -1, sizeof(dp));

		dp[0][0] = 0;
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j <= W; ++j) {
				int nj = j + w[i];
				if (nj <= W) {
					chmax(dp[i+1][nj], dp[i][j] + v[i]);
				}
				chmax(dp[i+1][j], dp[i][j]);
			}
		}

		int res = -1, wei = -1;
		for (int i = 0; i <= W; ++i)if ( chmax(res, dp[N][i]) ) wei = i;
		cout << "Case " << it << ":" << endl;
		cout << res << endl;
		cout << wei << endl;
		++it;
	}
    
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>

using namespace std;

pair<int, int> max(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.first == b.first ? a.second < b.second : a.first > b.first) {
        return a;
    } else {
        return b;
    }
}

#define MAX_W 1001

int main() {
    int W;
    int case_n = 1;
    while (cin >> W, W != 0) {
        int N; cin >> N;
        pair<int, int> dp[N][W+1];
        for (int i = 0; i < N; i++) 
            for (int j = 0; j <= W; j++) 
                dp[i][j] = pair<int, int>(0, 0); // 価値、 重さ
        int value[N];
        int weight[N];
        for (int i = 0; i < N; i++) {
            scanf("%d,%d", value+i, weight+i);
        }

        for (int i = 1; i < N; i++) {
            for (int j = 0; j <= W; j++) {
                if (j < weight[i]) {
                    dp[i][j] = dp[i-1][j];
                } else {
                    dp[i][j] = max(dp[i-1][j], 
                            make_pair(dp[i-1][j-weight[i]].first + value[i], 
                                      dp[i-1][j-weight[i]].second + weight[i]));
                }
            }
        }

        cout << "Case " << case_n << ":" << endl;
        cout << dp[N-1][W].first << endl;
        cout << dp[N-1][W].second << endl;
        case_n++;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  int w,c=0;
  while(cin >>w,w){
    c++;
    int n,t[1500][2]={0},dp[1500][1500]={0};
    cin >>n;
    for(int i=0; i<n; i++) scanf("%d,%d",&t[i][0],&t[i][1]);
    for(int i=0; i<n; i++){
      for(int j=0; j<=w; j++){
	if(j+t[i][1]<=w) dp[i+1][j+t[i][1]] = max(dp[i][j+t[i][1]],dp[i][j]+t[i][0]);
	dp[i+1][j] = max(dp[i][j],dp[i+1][j]);
      }
    }
    int m=0,ans;
    for(int i=0; i<=w; i++){
      if(m<dp[n][i]){m = dp[n][i];ans = i;}
    }
    cout <<"Case "<<c<<endl<<m<<endl<<ans<<endl;
  }
  return 0;
}

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

int sum[100+1];
int temp[100+1];
int size;

void update(int score, int weight) {
	for (int i = weight; i <= size; i++) {
		int a = sum[i-weight] + score;
		temp[i] = a > sum[i] ? a : sum[i];
	}
	for (int i = weight; i <= size; i++)
		sum[i] = temp[i];
}

int main() {
	int k = 0, n;
	while (cin >> size, size) {
		k++;
		cin >> n;
		for (int i = 0; i <= size; i++)
			sum[i] = temp[i] = 0;
		
		for (int i = 0; i < n; i++) {
			int a, b; scanf("%d,%d", &a, &b);
			update(a, b);
		}

		int w = size;
		while (sum[w-1] == sum[size]) w--;
		cout << "Case " << k << ":" << endl;
		cout << sum[size] << endl;
		cout << w << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <cassert>
#include <algorithm>
#include <functional>
#include <vector>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <sstream>
#include <istream>
#include <cmath>
#include <cstdio>

using namespace std;

#define vci vector<int>
#define vcs vector<string>
#define pb push_back
#define sz size()
#define mapii map<int, int>
#define mapci map<char, int>
#define mapsi map<string, int>
#define all(x) x.begin(), x.end()
#define minit(a, i) memset(a, i, sizeof(a));

#define for_(i, a, b) for (int i=(int)a; i<(int)b; i++)
#define for_d(i, a, b) for (int i=(int)a-1; i>=b; i--)
#define for_r(i, a, b, c) for (int i=(int)a; i<(int)b; i += c)
#define for_dr(i, a, b, c) for (int i=(int)a-1; i>=b; i -= c)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

typedef long long ll;

const int iINF = 2147483647;
const ll lINF = 9223372036854775807;

template <class T> inline void dbg(T t) { cout << t << endl; }



int dp[2][1010];

int main() {
	int casenum = 0;
	int W, n;
	
	while (1) {
		cin >> W;
		if (W==0) break;
		
		casenum++;
		
		cin >> n;
		vci v(n), w(n); char c;
		for_(i, 0, n) cin >> v[i] >> c >> w[i];
		
		minit(dp, 0);
		
		dp[0][w[0]] = v[0];
		for_(i, 1, n) {
			int now = i%2, nxt = (i+1)%2;
			for_(j, 0, W+1) {
				dp[now][j] = max(dp[now][j], dp[nxt][j]);
				if (j+w[i]<=W) {
					dp[now][j+w[i]] = max(dp[now][j+w[i]], dp[nxt][j] + v[i]);
				}
			}
		}
		
		int maxv = 0, maxw = 0;
		for_(j, 0, W+1) {
			if (maxv < dp[(n+1)%2][j]) {
				maxv = dp[(n+1)%2][j];
				maxw = j;
			}
		}
		
		cout << "Case " << casenum << ":" << endl;
		cout << maxv << endl << maxw << endl;
	}
	
	return 0;
}