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 <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

void solve(int W,int N){
    vector<int> v(N),w(N);
    rep(i,N) scanf("%d,%d",&v[i],&w[i]);

    vector<vector<int>> dp(N+10,vector<int>(W+10,0));
    for(int i=0;i<N;i++){
        // use v[i],w[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]);
            }else{
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }
        }
    }

    for(int i=0;i<=W;i++) if(dp[N][i]==dp[N][W]){
        printf("%d\n%d\n",dp[N][W],i);
        return;
    }
    assert(false);
}

int main(){
    int W,N;
    int cnt=0;
    while(1){
        cin>>W;
        if(W==0) break;
        cin>>N;
        printf("Case %d:\n",++cnt);
        solve(W,N);
    }
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define EXIST(v,i) (find(all(v), i) != (v).end())
#define ERASE(v,i) (v).erase(remove(all(v), i), (v).end())
#define INDEX(v,i) (find(all(v), i) - (v).begin())
#define rep(i,n) for(int i = 0; i < (int)n; ++i)
#define debug(x) cerr << #x << " = " << (x) << endl;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

const int INF = 100000000;
const double PI = 3.14159265358979323846;
const double EPS = 1e-10;

int main() {
	int CaseSet = 1;
	for(int W; scanf("%d", &W), W;) {
		int N; scanf("%d", &N);
		vector<int> value(N);
		vector<int> weight(N);
		rep(i,N) scanf("%d,%d", &value[i], &weight[i]);

		int dp[N][W+1];
		memset(dp, 0, sizeof dp);
		for(int j = weight[0]; j <= W; j++) dp[0][j] = value[0];
		for(int i = 1; i < N; i++) {
			for(int j = weight[i]; j <= W; j++) {
				int t = dp[i - 1][j - weight[i]] + value[i];
				dp[i][j] = max(dp[i][j], t);
			}
		}

		int vans = *max_element(dp[N - 1], dp[N - 1] + W);
		int ind = find(dp[N - 1], dp[N - 1] + W, vans) - dp[N - 1];

		cout << "Case " << CaseSet++ << ":" <<endl;
		cout << vans << endl;
		cout << ind << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>

using namespace std;




int main(){
	int dp[1001];
	int i, j;
	int Mw,n,v,w,t=1;
	char gomi;
	int saidai;


	while (1){

		fill(dp, dp + 1000, 0);
		cin >> Mw;
		if (Mw == 0){
			break;
		}
		cin >> n;
		for (i = 1; i <= n; i++){
			cin >> v;
			cin >> gomi;
			cin >> w;
			if (dp[w] < v){
				dp[w] = v;
				for (j = w-1; j >= 1; j--){
					if (w + j>1001){
						continue;
					}
					if (dp[j] == 0){
						continue;
					}
					dp[w + j] = max(dp[w] + dp[j], dp[w + j]);
				}
			}
		}

		for (i = Mw,saidai=0; i>=0; i--){
			if (dp[saidai] <= dp[i]){
				saidai = i;
			}
		}


		cout << "Case " << t << ":" << endl;
		cout << dp[saidai] << endl;
		cout << saidai << endl;
		t++;


	}

}