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

#define REP(X, Y) for( int (X) = 0; (X) < (Y); ++(X) )
#define REPT(X, Y, Z) for( int (X) = (Y); (X) < (Z); ++(X) )
#define REPR(X, Y) for( int (X) = (Y); (X) >= 0; --(X) )

int w, n, u[1000], v[1000];
int dp[1001][1000];
int dataset;

int main(){
	while (true){
		scanf("%d", &w);
		if (!w)break;
		scanf("%d", &n);
		++dataset;
		REP(i, n)
			memset(dp[i], -1, (w + 1)*sizeof(int) / sizeof(char)),
			scanf("%d,%d", &v[i], &u[i]);
		dp[0][0] = 0;
		REP(i, n){ //i番目の要素について検証
			REP(j, w+1){
				if (dp[i][j] == -1)continue;
				dp[i + 1][j] = dp[i][j];
				if (j + u[i] > w)continue;
				dp[i + 1][j + u[i]] =
					std::max(
					dp[i + 1][j] + v[i],
					dp[i][j + u[i]]
					);
			}
		}
		int maxu = [&]()->int{
			REPR(i, w)
				if (dp[n][i] != -1)return i;
		}();
		printf("Case %d:\n%d\n%d\n", dataset, dp[n][maxu], maxu);
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
vector<int> wmax;
int vmax;int n;
void dfs(vector<pair<int,int> > pii,int depth,int v,int w){
 
  if(w>n){
    return;
  }else if(v==vmax){wmax.push_back(w);
  }else if(v>vmax){wmax.clear();wmax.push_back(w);vmax=v;}
  if(pii.size()==depth+1)
    return;
  //cout<<pii[depth].first<<" "<<pii[depth].second<<endl;
  dfs(pii,depth+1,v+pii[depth].first,w+pii[depth].second);
  dfs(pii,depth+1,v,w);
  
}
int main(){
  int dataset=1;
  while(cin>>n,n){
    wmax.clear();vmax=0;
    int num;cin>>num;
    vector<pair<int,int> > pii;
    for(int i=0;i<num;i++){
      int a,b;
      scanf("%d,%d",&a,&b);
      if(b<n){
	pii.push_back(make_pair(a,b));
	//cout<<pii[pii.size()-1].first<<" "<<pii[pii.size()-1].second<<endl;
      }
    }
    for(int i=0;i<2;i++)pii.push_back(make_pair(0,0));
    dfs(pii,0,0,0);
    int minw=1000000000;
    //cout<<wmax.size()<<endl;
    for(int i=0;i<wmax.size();i++){
      cout<<wmax[i]<<endl;
      if(minw>wmax[i])
	minw=wmax[i];
    }
    cout<<"Case "<<dataset<<":"<<endl;
    cout<<vmax<<endl;
    cout<<minw<<endl;
    dataset++;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <limits.h>
using namespace std;

typedef pair<int, int> P;

bool compare(const P &l, const P &r) {
  return l.second > r.second;
}

int max(int a, int b) {
  return (a > b) ? a : b;
}

int min(int a, int b) {
  return (a < b) ? a : b;
}

int main() {
  int w, n, c=0;
  P x[1000];
  int buf1[1001];
  int buf2[1001];
  int *p1, *p2, *tmp;
  int a, b;

  while (scanf("%d", &w) && w) {
    ++c;
    scanf("%d", &n);
    for (int i=0; i<n; i++) {
      scanf("%d,%d", &x[i].first, &x[i].second);
    }
    sort(x, x+n, compare);
    memset(buf1, 0, sizeof(int)*(w+1));
    memset(buf2, 0, sizeof(int)*(w+1));
    p1 = buf1;
    p2 = buf2;
    for (int i=0; i<n; i++) {
      int t = w-x[i].second;
      for (int j=0; j<=w; j++) {
        if (p1[j]) {
          p2[j] = max(p1[j], p2[j]);
          if (j<=t) p2[j+x[i].second] = max(p1[j+x[i].second], p1[j]+x[i].first);
        }
      }
      p2[x[i].second] = max(p1[x[i].second], x[i].first);
      tmp = p1;
      p1 = p2;
      p2 = tmp;
    }
    a = 0;
    for (int i=0; i<=w; i++) {
      a = max(a, p1[i]);
    }
    b = INT_MAX;
    for (int i=0; i<=w; i++) {
      if (p1[i]==a) {
        b = min(b, i);
      }
    }
    printf("Case %d:\n", c);
    printf("%d\n", a);
    printf("%d\n", b);
  }
  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;
    int in[1001][1001]={0};
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      rep(j,m+1){
	if(j>=b)
	  in[i][j]=max(in[i][j],in[i-1][j-b]+a);
	in[i][j]=max(in[i][j],in[i-1][j]);
      }
    }
    int max1=0;
    rep(i,m+1)max1=max(max1,in[n][i]);
    rep(i,m+1)
      if(in[n][i]==max1){
	cout<<"Case "<<co<<":"<<endl;
	cout<<max1<<endl<<i<<endl;
	break;
      }
  }
}

/*int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    char d;
    cin>>n;
    vector<int>w(n),v(n);
    vector<vector<int> >dp(n+1);
    rep(i,m+1)dp[0].pb(0);
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      dp[i].pb(0);
      

}
*/

C++: #include <iostream>

using namespace std;

int main() {
	int w, n;
	int value[1000], weight[1000];
	int dp[2][1001];
	for (int case_num = 1; true; case_num++) {
		cin >> w;
		if (w == 0) {
			return 0;
		}
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> value[i];
			cin.ignore(1);
			cin >> weight[i];
		}
		for (int i = 0; i <= 1000; i++) {
			dp[1][i] = 0;
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= w; j++) {
				if (weight[i] > j) {
					dp[i % 2][j] = dp[(i + 1) % 2][j];
				} else {
					dp[i % 2][j] = max(dp[(i + 1) % 2][j], dp[(i + 1) % 2][j - weight[i]] + value[i]);
				}
			}
		}
		for (int j = w; j >= 0; j--) {
			if (dp[(n + 1) % 2][j] != dp[(n + 1) % 2][w]) {
				cout << "Case " << case_num << ':' << endl;
				cout << dp[(n + 1) % 2][w] << endl;
				cout << j + 1 << endl;
				break;
			}
		}
	}
}