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 <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++) {
			memcpy(dp[i], dp[i -1], sizeof dp[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);
			}
		}

#if 0
		rep(i,N) {
			rep(j,W) cerr << " " << dp[i][j];
			cerr << endl;
		}
#endif

#if 0
		int vans = *max_element(dp[N-1], dp[N-1] + W);
		int minweight = find(dp[N-1], dp[N-1] + W, vans) - dp[N-1];
#endif

#if 1
		int vans = dp[N-1][W];
		int minweight = W;
		for(int j = W - 1; j >= 0; --j) {
			if(dp[N-1][j] >= vans) {
				minweight = j;
				vans = dp[N-1][j];
			}
		}
#endif

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

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int DP[1001][1001];
int main(){
	int W,N;
	int C=0;
	while(cin>>W){
		if(!W) break;
		cin>>N;
		int v[1000],w[1000],SUMW;
		for(int i=0;i<N;++i) scanf("%d,%d",&v[i],&w[i]);
		for(int i=0;i<=N;++i) DP[0][i]=0;
		for(int i=0;i<N;++i){
			for(int j=0;j<=W;++j){
				if(w[i]>j){//入らない時
					DP[i+1][j]=DP[i][j];
				}else{//入る時
					DP[i+1][j]=max((DP[i][j]),(DP[i][j-w[i]]+v[i]));
				}
			}
		}
		SUMW=W;
		for(int j=W;j>=0;--j) if(DP[N][W]==DP[N][j])SUMW=j;
		cout<<"case "<<++C<<":\n";
		cout<<DP[N][W]<<"\n"<<SUMW<<"\n";
	}
	return 0;
}

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

long long dp[1000];
int main(){
	long long w, n, ans_w = 0, ans_val = 0;
	for (int dataset = 1; cin >> w, w; dataset++){
		long long t_w, t_val;
		cin >> n;
		for (long long i = 0; i <= w; i++) dp[i] = -1;
		dp[0] = 0;
		for (long long i = 0; i < n; i++){
			scanf("%lld,%lld", &t_val, &t_w);
			for (long long j = w - 1; j >= 0; j--){
				//printf("%d ", dp[j]);
				if (dp[j] == -1 || j + t_w > w || dp[j + t_w] > dp[j] + t_val)
					continue;
				dp[j + t_w] = dp[j] + t_val;
			}
			//puts("");
		}
		for (long long i = 0; i <= w; i++){
			//printf("%d ", dp[i]);
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		//puts("");
		printf("Case %d:\n%lld\n%lld\n", dataset, ans_val, ans_w);
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAX_N 1000
#define MAX_W 1000

int N, W;
int v[MAX_N], w[MAX_N];
int memo[MAX_N][MAX_W];
int resW = 1e9;

int solve(int index, int weight){
  if(memo[index][weight] > 0) return memo[index][weight];

  int res;
  if(index == N) res = 0;
  else if(weight < w[index]) res = solve(index + 1, weight);
  else{
    res = max(solve(index + 1, weight), solve(index + 1, weight - w[index]) + v[index]);
  }
  return memo[index][weight] = res;
}

void askw(int ans, int weight, int index){
  if(ans == 0){
    if(weight < resW) resW = weight;
  }
  if(index == N) return;
  if(ans - v[index] >= 0){
    askw(ans - v[index], weight + w[index], index + 1);
    askw(ans, weight, index + 1);
  }
}

int main(void){
  int cnt = 0, ans;
  while(cin >> W, W){
    cnt++;
    cin >> N;
    for(int i = 0; i < N; i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    memset(memo, -1, sizeof(memo));
    ans = solve(0, W); askw(ans, 0, 0);
    cout << "Case " << cnt << ":" << endl;
    cout << ans << endl;
    cout << resW << endl;
  }
  return 0;
}

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

#define MAX 1001

int dp[MAX][MAX];
int v[MAX], w[MAX];

int main() {
	int W, n, u = 0;
	char t;
	while (cin >> W) {
		if (W == 0) break;
		cin >> n;
		u++;

		dp[MAX][MAX] = { 0 };
		v[MAX] = { 0 };
		w[MAX] = { 0 };
		for (int i = 0; i < n; i++) {
			cin >> v[i] >> t >> w[i];
		}

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

		int mv = 0, mw;
		for (int i = 0; i <= W; i++) {
			if (dp[n][i] > mv) {
				mv = dp[n][i];
				mw = i;
			}
		}

		cout << "Case " << u << ":" << endl;
		cout << mv << endl;
		cout << mw << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
long ww,n,v,*w,**p,i,j,k=1,sw;
char c;
while(cin>>ww){
if(ww==0)break;
cin>>n;
w=new long[n+1];
p=new long*[n+1];
for(i=0;i<=n;i++){p[i]=new long[ww+1];p[i][0]=0;}
for(j=0;j<=ww;j++)p[0][j]=0;
sw=0;
for(i=1;i<=n;i++){
cin>>v>>c>>w[i];
for(j=1;j<=ww;j++){
if(w[i]>j)p[i][j]=p[i-1][j];
else if(p[i-1][j-w[i]]+v>p[i-1][j])p[i][j]=p[i-1][j-w[i]]+v;
else p[i][j]=p[i-1][j];
}
}
for(j=ww;j>=1;j--)if(p[n][j]!=p[n][j-1])break;
for(i=n;i>=1;i--){
if(p[i][j]!=p[i-1][j]){sw+=w[i];j-=w[i];}
}
cout<<"Case "<<k<<":"<<endl;
cout<<p[n][ww]<<endl;
cout<<sw<<endl;
k++;
}
return 0;
}