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 <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
using namespace std;

int main()
{
//	cut here before submit 
//	freopen("testcase.thief", "r", stdin);
	string str = "";
	int count = 0;

	while (getline (cin, str ) ){
		int max_wight;
		stringstream ssw (str );
		ssw >> max_wight;
		if (max_wight == 0 ){
			break;
		} // end if
		++count;
		
		int n;
		getline (cin, str );
		stringstream ssn(str);
		ssn >> n;

		vector <int> W (n, 0);
		vector <int> V (n, 0);
		vector <int> DP (max_wight+1, 0 );
		int i,j;

		for (i = 0; i < n; ++i){
			getline (cin, str );
			while (str.find(',') != string::npos ){
				str = str.replace(str.find(','), 1, " ");
			} // end while
			stringstream ss(str);
			ss >> V[i] >> W[i];
		} // end for

		for (i = 0; i < n; ++i){
			vector<int> dp = DP;
			for (j = W[i]; j <= max_wight; ++j){
				int p = j - W[i];
				if (dp[p] + V[i] > DP[j] ){
					DP[j] = dp[p] + V[i];
				} // end if 
			} // end for
		} // end for

		int maxvalue = DP[max_wight];
		int minwight = max_wight;

		for (j = max_wight - 1; j >= 0; --j){
			if (DP[j] >= maxvalue){
				minwight = j;
				maxvalue = DP[j];
			} // end if
		} // end for
		cout << "Case " << count << ':' << endl;
		cout << maxvalue << endl;
		cout << minwight << endl;
			
	} // end while	
	
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#define int long long
using namespace std;
int a[1234], b[1234], dp[1234][1234];
signed main() {
	int w, n, l = 1;
	while (cin >> w, w) {
		cin >> n;
		for (int j = 1; j <= n; j++) {
			string s; cin >> s;
			int pw = 0;
			for (int i = 0; i < s.size(); i++) {
				if (s[i] == ',') {
					a[j] = pw; pw = 0;
				}
				else {
					pw *= 10; pw += s[i] - '0';
				}
			}
			b[j] = pw;
		}
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= w; j++) {
				dp[i][j] = -1;
			}
		}
		dp[0][0] = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= w; j++)dp[i][j] = dp[i - 1][j];
			for (int j = 0; j + b[i] <= w; j++) {
				if (dp[i-1][j] >= 0) {
					dp[i][j + b[i]] = max(dp[i][j + b[i]], dp[i - 1][j] + a[i]);
				}
			}
		}
		int ans1 = 0, ans2 = 0;
		for (int i = 0; i <= w; i++) {
			if (ans1 < dp[n][i]) {
				ans1 = dp[n][i];
				ans2 = i;
			}
		}
		cout << "Case " << l << ":\n";
		cout << ans1 << endl << ans2 << endl;
		l++;
	}
}

C++: #include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<string>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)

int w,n;
vector<int> v;
vector<int> g;

void init(){
	v.clear();
	g.clear();
}
bool input(){
	cin>>w;
	if(w==0)return false;
	cin>>n;
	
	rep(i,n){
		int a,b;
		scanf("%d,%d",&a,&b);
		v.push_back(a);
		g.push_back(b);
	}
	return true;
}

const int W = 1000;
const int N = 1000;
const int INF = 1000000000;
int dp[N][W];

pair<int,int> solve(){
	rep(i,N)rep(j,W)dp[i][j]=0;
	
	dp[0][0] = 0;
	dp[0][g[0]] = v[0];
	
	reps(i,1,n){
		rep(j,w){
			dp[i][j] = dp[i-1][j];
			if(j-g[i]>=0)dp[i][j] = max(dp[i][j], dp[i-1][j-g[i]]+v[i]);
		}
	}
	
	int maxp = 0;
	rep(j,w){
		if(dp[n-1][maxp]<dp[n-1][j])maxp=j;
	}
	
	/*
	rep(i,n){
		rep(j,w)printf("%3d ",dp[i][j]);puts("");
	}*/
	
	return pair<int,int>(dp[n-1][maxp],maxp);
}
int main(){
	for(int i=1;init,input();i++){
		pair<int,int> ans = solve();
		printf("Case %d:\n%d\n%d\n",i,ans.first,ans.second);
	}
}

/*
10
5
60,2
100,4
120,6
210,9
10,1
0

*/

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <utility>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int W,N;
const int MAX_N = 1001;
const int MAX_W = 1001;
int v[MAX_N];
int w[MAX_N];
vector<pii> t;
int dp[MAX_N][MAX_W];

void solve() {
	memset(dp, 0, sizeof(dp));
	repa(i,1,N) {
		repa(j,0,W) {
			if(j + w[i] <= W) {
				dp[i][j + w[i]] = dp[i-1][j] + v[i];
			}
		}
	}

	int maxval = -1;
	int weight = 0;
	repa(i,0,W) {
		if(maxval < dp[N][i]) {
			maxval = dp[N][i];
			weight = i;
		}
	}
	cout <<	maxval << endl;
	cout << weight << endl;
}

int main() {
	int c = 0;
	while(cin>>W, W) {
		cin >> N;
		rep(i,N) {
			scanf("%d,%d", &v[i], &w[i]);
		}

		printf("Case %d:\n", ++c);
		solve();
	}
}

C++: #include<stdio.h>
int dp[1001];
int max(int a,int b){return a>b?a:b;}
int main(){
	int n,W;
	int i,j;
	int c=1;
	while(1){
	scanf("%d",&W);
	if(W==0)return 0;
	scanf("%d",&n);
	for(i=0;i<=W;i++)dp[i]=-1;
	dp[0]=0;
	for(i=0;i<n;i++){
		int w,v;
		scanf("%d,%d",&v,&w);
		for(j=W-w;j>=0;j--){
			dp[j+w]=max(dp[j+w],dp[j]+v);
		}
	}
	int m=0;
	for(i=1;i<=W;i++){
		if(dp[i]>dp[m])m=i;
	}
	printf("Case %d:\n",c);
	printf("%d\n%d\n",dp[m],m);
	c++;
	}
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define LOOP(i,x,n) for(int i=x;i<n;i++)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=9000000009;

int dp[10004][10004];

signed main(){
  int W,N;
  int c=1;
  while(cin>>W>>N,W){
    vector<int> v(N),w(N);
    char a;
    REP(i,N)cin>>v[i]>>a>>w[i];
    dp[0][w[0]]=v[0];
    LOOP(i,1,N){
      REP(j,W){
        dp[i][j]=dp[i-1][j];
        if(j-w[i]>=0)dp[i][j]=max(dp[i][j],dp[i-1][j-w[i]]+v[i]);
      }
    }
    int ans=-INF,wei;
    REP(i,W){
      if(ans<dp[N-1][i]){
        ans=dp[N-1][i];
        wei=i;
      }
    }
    c++;
    cout<<"Case "<<c<<":"<<endl;
    cout<<ans<<endl;
    cout<<wei<<endl;
  }
  return 0;
}