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 <cstdio>
#include <cstring>

using namespace std;
int W,n;
int dno=0;
int v[1000],w[1000];
int dp[1001][1001];
int r(int i,int j){
  int rv;
  if(dp[i][j]>=0) return dp[i][j];
  if(i==n) rv=0;
  else if(j<w[i]) rv=r(i+1,j);
  else  rv=max(r(i+1,j),r(i+1,j-w[i])+v[i]);
  dp[i][j]=rv;
  return rv;
}
int main(){
  while(1){
    dno++;
    cin>>W;
    if(W==0) break;
    cin>>n;
    for(int i=0;i<n;i++) scanf("%d,%d",&v[i],&w[i]);
    memset(dp,-1,sizeof(dp));
    cout<<"Case "<<dno<<":"<<endl;
    int aw=0;
    for(int i=W;i>0;W--){
      if(aw>r(0,W)) break;
      aw=r(0,W);
    }
    cout<<aw<<endl;
    cout<<W+1<<endl;
  }
  return 0;
}

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

const int MAXN = 1004;
const int MAXW = 1004;
int W;
int N;
int P[MAXN];
int C[MAXN];
int dp[MAXW];

int main() {
  while(scanf("%d", &W) != EOF && W) {
    scanf("%d", &N);
    for(int i = 0; i < N; ++i) {
      scanf("%d,%d", &P[i], &C[i]);
    }
    fill(dp,dp+MAXW,0);
    for(int i = 0; i < N; ++i) {
      for(int j = W-C[i]; j >= 0; --j) {
	dp[j+C[i]] = max(dp[j+C[i]], dp[j] + P[i]);
      }
    }
    int res = -1;
    int w = 0;
    for(int j = 0; j <= W; ++j) {
      if(dp[j] > res) {
	res = dp[j];
	w = j;
      }
    }
    static int tc = 0;
    cout << "Case " << ++tc << ":" << endl;
    cout << res << endl;
    cout << w << endl;
  }
  return 0;
}

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

int main(){
	int w, cntcase = 1;
	while (cin >> w, w){
		int n, dp[1000], treas_val, treas_w, ans_val = 0, ans_w = 0;
		cin >> n;
		for (int i = 0; i < w; i++) dp[i] = -1;
		dp[0] = 0;
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &treas_val, &treas_w);
			for (int j = w - 1; j >= 0; j--){
				if (dp[i] != -1 && i + treas_w < w && dp[i + treas_w] < dp[i] + treas_val){
					dp[i + treas_w] = dp[i] + treas_val;
				}
			}
		}
		for (int i = 0; i < w; i++){
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		printf("Case %d:\n%d\n%d\n", cntcase, ans_val, ans_w);
		cntcase++;
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define SIZE(x) ((int)(x).size())
#define UQ(v) v.erase(unique(v,begin(),v,end()),v.end());
#define INF 11922960 
#define pb push_back 
int dxy4[5] = { 0,1,0,-1,0 };
int dxy8[9] = { 0,1,1,0,-1,1,-1,-1,0 };
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)




//vector< vector<int> > v (max1, vector<int>(max2) );
int main() {
	int n, W, count = 0;
	while (1) {
		count++;
		cin >> W >> n;
		if (!W) break;
		vector<vector<int>>dp(n+1, vector<int>(W+1, 0));
		vector<int> v(n + 1, 0);
		vector<int> w(n + 1, 0);


		 char s;
		rep(i, n) { cin >> v[i+1]>> s>>w[i+1]; }

		rep(i, (n+1)) {
			if (!i)continue;
			//cout << i << "-------" << endl;
			rep(j, W-w[i]+1) {
				dp[i][j] = max(dp[i - 1][j],dp[i][j]);
				dp[i][w[i] + j] = max(dp[i - 1][w[i] + j], dp[i - 1][j] + v[i]);
				//cout << dp[i][w[i]+j] << " ";
			}
			//cout << endl;
		}
		int umax = dp[n][W];
		rep(i, W + 1) {
			if (dp[n][i] == umax) {
				cout << "Case " << count <<":"<< endl<< umax << endl << i << endl;
				break;
			}
		}
	}
	return 0;
}

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

int cnt, W, N, w[1000], v[1000], dp[1000][1001];

void solve(){
    for(int i = 0; i < N; i++){
        dp[i+1][w[i]] = v[i];
        for(int j = w[i]+1; j <= W; j++){
            dp[i+1][j] = 0;
            if( dp[i][j-w[i]] ){
                dp[i+1][j] = max(dp[i+1][j], dp[i][j-w[i]] + v[i]);
            }
        }
    }
    int c,v = 0;
    for(int i = 0; i < W; i++){
        if( dp[N][i] > v ){
            v = dp[N][i];
            c = i;
        }
    }
    printf("Case %d:\n%d\n%d\n", cnt, v, c);
}

int main(){
    cnt = 0;
    while( true ){
        cnt++;
        scanf("%d", &W);
        if( !W ) break;
        scanf("%d", &N);
        for(int i = 0; i < N; i++){
            scanf("%d,%d", v+i, w+i);
        }
        solve();
    }
    return 0;
}

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

#define rep(x,to) for(int x=0;x<to;x++)
#define rep2(x,from,to) for(int x=from;x<to;x++)

struct stk{
	int kt,wg,ct; 
	stk(int kt,int wg,int ct) : kt(kt), wg(wg), ct(ct) { }
};
using namespace std;


int main(void){

int w,n,cnt=0;
char k;
	
	while(!cin.eof()){
		cin >> w ;
		if(cin.eof() || w==0) break;
		cin.ignore();
		cin >> n;
		vector<vector<int> >dp(n+1,vector<int>(w+1,0));
		vector<int> tk(n,0);
		vector<int> tw(n,0);

		cnt++;
		rep(i,n) cin >> tk[i] >> k >> tw[i];

		rep(i,n) rep(j,w+1){
			if(tw[i]>j){
				dp[i+1][j]=dp[i][j];
			}else{
				dp[i+1][j]=max(dp[i][j],dp[i][j-tw[i]]+tk[i]);
			}
		}	
//		rep(i,n+1) {rep2(j,1,w+1){
//			cout << dp[i][j] << "\t";
//		} cout <<endl;}

		while(dp[n][w]==dp[n][w-1]){
			w--;
		}
		printf("Case %d:\n%d\n%d\n",cnt,dp[n][w],w);
		dp.clear(); tk.clear(); tw.clear();

	}
	
	return 0;
}

C++: #include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

int max(int a,int b){
	if(a>=b)return a;
	else return b;
}

int min(int a,int b){
	if(a>=b)return b;
	else return a;
}

void swap(int *a,int *b){
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void babbleSort(int *data,int n){
	for(int i=0;i<n;i++){
		for(int j=n-1;j>i;j--){
			if(data[j]>=data[j-1]){
				swap(&data[j],&data[j-1]);
			}
		}
	}
}


using namespace std;

/*
cin.ignore();
	getline(cin,dataStr);
	data = new int[n];
	stringToInteger(&dataStr,data,n);
*/

void stringToInteger(string *str,int *data,int n){
	for(int i=1;i<=n;i++){
		int spaceN = str->find(' ',0);

		data[i] = atoi(str->substr(0,spaceN).c_str());
		str->erase(0,spaceN+1);
	}
}



int dp[1010];
//dp[i] i:重さ




int main(){


	int w,n;
	int cnt=1;
	while(1){
		cin>>w;
		if(w==0)break;

		cin>>n;

		for(int i=0;i<=w;i++){
			dp[i]=0;
		}
		int value,weight;
		for(int i=0;i<n;i++){
			scanf("%d,%d",&value,&weight);
			for(int j=w;j>=0;j--){
				if((j==0||dp[j]>0)&&((j+weight)<=w)){
					int tmp = dp[j]+value;
					dp[j+weight] = max(tmp,dp[j+weight]);
				//	cout<<j+weight<<" "<<dp[j+weight]<<endl;
				}
			}
		}

		int ans=0;
		int ansCnt=0;
		for(int i=0;i<=w;i++){
			if(ans<dp[i]){
				ans=dp[i];
				ansCnt=i;
			}
		}
		cout<<"Case "<<cnt<<":"<<endl;
		cout<<ans<<endl;
		cout<<ansCnt<<endl;
		cnt++;
	}



	return 0;
}