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

using namespace std;

int N,W;

struct Treasurea
{

  int value;
  int weight;
  int flag;

  Treasurea()
  {
    value = 0;
    weight = 0;
    flag = false;
  }

};

struct Memo
{

  Treasurea record;
  bool flag;

  Memo()
  {
    record.value = 0;
    record.weight = 0;
    record.flag = false;
    flag = false;
  }

};

Memo memo[1001][1001];

Treasurea temp;

Treasurea knapsack(vector<Treasurea> &treasurea, int correntWeight,int correntValue, int kind)
{

  if(correntWeight < 0)
    {

      //Treasurea temp;
      temp.weight = W - (correntWeight + treasurea[kind - 1].weight);
      temp.value = correntValue - treasurea[kind - 1].value;
      return temp;
    }
  else if(memo[correntWeight][kind].flag) return memo[correntWeight][kind].record;
  else if(kind == treasurea.size())
    {
      //Treasurea temp;
      temp.weight = W - correntWeight;
      temp.value = correntValue;
      return temp;
    }


  Treasurea total;

  for(int i = 0;i < treasurea.size(); ++i)
    {

      if(treasurea[i].flag) continue;

      treasurea[i].flag = true;

      //Treasurea temp = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

      temp = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

      if(total.value < temp.value || (total.value == temp.value && total.weight > temp.weight))
        total = temp;

      treasurea[i].flag = false;

    }

  memo[correntWeight][kind].record = total;
  memo[correntWeight][kind].flag = true;

  return total;

}

int main()
{

  int case_number = 0;

  while(cin >> W && W != 0)
    {

      cin >> N;

      vector<Treasurea> treasurea(N);
      Treasurea result;

      for(int i = 0; i < N; ++i) scanf("%d,%d",&treasurea[i].value,&treasurea[i].weight);

      memset(memo,0,sizeof(memo));

      result = knapsack(treasurea,W,0,0);

      cout << "Case " << ++case_number << ":"<< endl << result.value << endl << result.weight << endl;

    }

  return 0;

}

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

int dp[1010][1010];

int main() {
	int w;
	int x = 1;
	while (cin >> w, w) {
		int n;
		cin >> n;
		int v[1005], m[1005];
		for (int i = 0;i < n;i++) {	scanf("%d,%d", &v[i], &m[i]);}

		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 = 0;i < n;i++) {
			for (int j = 0;j < w;j++) {
				if (dp[i][j] !=-1) {
					dp[i + 1][j] = 0;
					if (j + m[i] <= w)dp[i + 1][j + m[i]] = 0;
					dp[i + 1][j] = dp[i][j];
					dp[i + 1][j + m[i]] = dp[i][j] + v[i];
				}
			}
		}

		int k=0, aw=0;
		for (int i = w;i > -1;i--) {
			if (dp[n][i] >= k) {
				k = dp[n][i];
				aw = i;
			}
		}
		cout << "Case " << x << ":" << endl;
		cout << k << endl << aw << endl;
		x++;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int main()
{
  int W,N,v,w,max,maxi,i,cnt=0;
  while(cin>>W,W){
    cin>>N;
    vector<int> dp(W+1);
    dp[0]=0;
    while(N-->0){
      scanf("%d,%d",&v,&w);
      vector<int> DP=dp;
      for(i=w;i<W;i++){
	if(dp[i-w]+v > DP[i])dp[i]=DP[i-w]+v;
      }
    }
    max=maxi=0;
    for(i=0;i<W;i++)if(max<dp[i]){max=dp[i];maxi=i;}
    cout<<"Case "<<++cnt<<":"<<endl;
    cout<<max<<endl;
    cout<<maxi<<endl;
  }
}

C++: #include<iostream>
#include<queue>
#include<map>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define fr first
#define sc second
#define INF 100
typedef pair<int,int>Pt;
int main(){
  int dp[1001],s[1001],g[1001];
  int n,W,a = 1;
  while(cin >> n && n){
    cin >> W;
    for(int i=0;i<W;i++){
      scanf("%d,%d",&s[i],&g[i]);
    }
    memset(dp,-1,sizeof(dp));
    dp[0] = 0; //Empty
    for(int i=0;i<W;i++){
      for(int j = n-g[i];j>=0;j--){
        if(dp[j] >= 0) dp[j+g[i]] = max(dp[j+g[i]],dp[j]+s[i]);
      }
    }
    int p=0,q=0;
    for(int i=0;i<=n;i++){
      if(dp[i] > p){
        p = dp[i];
        q = i;
      }
    }
    cout << "Case " << a++ << ":" << endl << p << endl << q << endl;
  }
}

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

int pri[1010],wai[1010],dp[2][1010];
int main(){
	int W,N,cas=0;
	while(true){
		cas++;
		cin>>W;
		if(W==0) break;
		cin>>N;
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&pri[i],&wai[i]);
		}
		fill(dp[0],dp[0]+W+1,0);
		fill(dp[1],dp[1]+W+1,0);
		int in=0,ou=1;
		for(int i=1;i<=N;i++){
			fill(dp[ou],dp[ou]+W+1,0);
			for(int j=0;j<=W;j++){
				if(j+wai[i]<=W){
					dp[ou][j+wai[i]]=dp[in][j]+pri[i];
				}
			}
			swap(in,ou);
		}
		int res=0,cnt=0;
		for(int i=0;i<=W;i++){
			if(res<dp[in][i]){
				res=dp[in][i];
				cnt=i;
			}
		}
		cout<<"Case "<<cas<<":"<<endl<<res<<endl<<cnt<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define INF ( 1 << 25 )
int main(){
  int W, N, v[1000], w[1000], dp[1001], cnt = 0;
  char op;
  while(cin >> W >> N , W){
    fill_n(dp,1001,0);
    for(int i = 0 ; i < N ; i++ ) cin >> v[i] >> op >>  w[i];
    for(int i = 0 ; i < N ; i++ ){
      for(int j = W ; j >= w[i] ; j-- ){
        dp[j] = max( dp[j], dp[j - w[i]] + v[i]);
      }
    }
    int ret = 0;
    for(int i = 0 ; i <= W ; i++ ){
      if(dp[i] > dp[ret]) ret = i;
    }
    cout << "Case " << ++cnt << ":" << endl << dp[ret] << endl << ret << endl;
  }
}