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 <algorithm>
#include <vector>
using namespace std;

struct treasure{
	int value;
	int weight;
};

bool comp(treasure t1, treasure t2){
	if(t1.weight < t2.weight){
		return true;
	}else if(t1.weight == t2.weight){
		if(t1.value < t2.value){
			return true;
		}
	}
	return false;
}

int main(){
	int count = 1;
	int w;
	int n;
	char c;
	const int nmax = 1001;
	int cost[nmax];
	int costd[nmax];
	int best[nmax];
	int bestd[nmax];
	while(cin >> w){
		if(w == 0) break;
		for(int i = 0; i < nmax; i++){
			cost[i] = 0;
			costd[i] = 0;
			best[i] = 0;
			bestd[i] = 0;
		}
		vector<treasure> vt;
		cin >> n;
		for(int i = 0; i < n; i++){
			treasure t;
			cin >> t.value >> c >> t.weight;
			vt.push_back(t);
		}
		treasure t;
		t.value = -1;
		t.weight = -1;
		vt.push_back(t);
		sort(vt.begin(), vt.end(), comp);
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= w; j++){
				if(vt[i].weight <= j ){
					if(cost[j] < cost[j-vt[i].weight] + vt[i].value){
						if(i != best[j-vt[i].weight]){
							costd[j] = cost[j-vt[i].weight] + vt[i].value;
							bestd[j] = i;
						}else {
							costd[j] = cost[j-vt[i].weight];
							bestd[j] = best[j-vt[i].weight];
						}
					}
				}
			}
			for(int j = 1; j <= w; j++){
				cost[j] = costd[j];
				best[j] = bestd[j];
			}
		}
		int i = w;
		while(i > 0 && cost[i] == cost[i-1]) i--;
		cout << "Case " << count++ << ":" << endl;
		cout << cost[w] << endl;
		cout << i << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    cin>>n;
    vector<int>in(m,0);
    rep(i,n){
      int a,b;
      char d;
      cin>>a>>d>>b;
      for(int i=m;i>0;i--){
	if(i-b<0)continue;
	if(i!=b&&!in[i-b])continue;
	in[i]=max(in[i-b]+a,in[i]);
      }
      //rep(i,m+1)cout<<in[i]<<" "<<i<<endl;
      //cout<<" "<<endl;
    }
    int ans1=0,ans2=0;
    for(int i=m-1;i>0;i--){
      if(ans1<=in[i]){ans1=in[i];ans2=i;}
    }
    cout<<"Case "<<co<<":"<<endl;
    cout<<ans1<<endl<<ans2<<endl;
  }
}

C++: #include<iostream>
using namespace std;
int main(){
  int dp[1001][1001];
  int n,W;
  int v[1000],w[1000];
  int num=0;
  while(cin>> W,W){
    num++;
    cin>> n;
    for(int i=0;i<n+1;i++){
      dp[0][i]=0;
    }

    for(int i=0;i<n;i++){
      char c;
      cin>> v[i]>> c>> w[i];
    }

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

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

    cout<< "Case "<< num<< ":"<< endl;
    cout<< mv<< endl;
    cout<< mw<< endl;
  }
}

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

using namespace std;

int main( void )
{
	int map[1001], save[1001];
	int w; long long count;
	count = 1;
	while(cin >> w, w) {
		++w;
		for(int i=0; i < w; ++i) map[i] = save[i] = 0;
		int n, value, weight;
		cin >> n;
		scanf("%d,%d", &value, &weight);
		save[weight] = map[weight] = max(save[weight], value);
		for(int i=1; i < n; ++i) {
			scanf("%d,%d", &value, &weight);
			save[weight] = max(save[weight], value);
			for(int j=0; j < w; ++j) {
				if(map[j]) {
					int ww = j + weight;
					if(ww < w)
						save[ww] = max(save[ww], map[j] + value);
					else
						save[j] = max(save[j], map[j]);
				}
			}
			for(int j=0; j < w; ++j) {
				map[j] = save[j]; save[j] = 0;
			}
		}
		int Max = 0, idx;
		for(int i=1; i < w; ++i) {
			if(Max < map[i]) {
				Max = map[i];
				idx = i;
			}
		}
		cout << "Case " << count++ << ':' << endl;
		cout << Max << endl;
		cout << idx << endl;
	}

	return 0;
}

C++: //Solution for aoj:0042 A Thief
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
using namespace std;
int w, n, c;
char a;
int trs[1001][2];
int dp[1001][1001];
int main(){
	while (cin >> w){
		if (!w)
			return 0;
		c++;
		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> trs[i][0] >> a >> trs[i][1];
		}
		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= w; j++){
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
				if (j >= trs[i][1])
					dp[i][j] = max(dp[i][j], dp[i - 1][j - trs[i][1]] + trs[i][0]);
			}
		}
		for (int i = 1; i <= w; i++){
			if (dp[n][i] == dp[n][w]){
				cout << "Case " << c << ':' << endl << dp[n][i] << endl << i << endl;
				break;
			}
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;

int main(){
    int w;
    while(cin >>w && w){
        int n;
        cin >>n;
        vector< vector<int> > dp(n + 1, vector<int>(w + 1));
        REP(i, dp.size()){
            REP(j, dp[i].size()){
                dp[i][j] = 0;
            }
        }
        dp[0][0] = 0;
        //input begin
        vector<P> j(n + 1);
        string str;
        getline(cin, str);
        FOR(i, 1, n + 1){
            getline(cin, str);
            stringstream ss(str);
            int a, b;
            char c;
            ss >>a >>c >>b;
            j[i].first = a;
            j[i].second = b;
        }
        //input end

        FOR(i, 1, dp.size()){
            int value = j[i].first, weight = j[i].second;
            REP(j, dp[i].size()){
                if(j >= weight && j - weight >= 0){
                    dp[i][j] = max(dp[i][j], dp[i - 1][j - weight] + value);
                } else{
                    dp[i][j] = max(dp[i][j], dp[i - 1][j]);
                }
            }
        }

        int ans = -1;
        REP(i, dp[0].size()){
            if(dp[n][i] > ans){
                ans = dp[n][i];
            }
        }
        cout <<ans <<endl;
    }
    return 0;
}