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>
using namespace std;
int main(){
  int Case = 1;
  while(1){
    int w,n;
    int t[1001];
    int copy[1001];
    int takara[1000][2];
    for(int i=0;i<1001;i++){
      t[i]=0;
      copy[i]=0;
    }
    cin >> w;
    if ( w == 0)break;
    cin >> n;
    for(int i=0;i<n;i++){
      int kati,omosa;
      char conma;
      cin >> kati>>conma>>omosa;
      takara[i][0]=kati;
      takara[i][1]=omosa;
    }
    for(int i=1;i<=w;i++){
      if (i >= takara[0][1])copy[i] = takara[0][0]; 
    }
    for(int i=1;i<n;i++){
      for(int j=0;j<=w;j++){
        if (j <takara[i][1])t[j] = copy[j];
        else if (copy[j] < takara[i][0]+copy[j-takara[i][1]])t[j] = takara[i][0]+copy[j-takara[i][1]];
        else t[j] =copy[j]; 
      }
      for(int j=0;j<=w;j++){
        copy[j]=t[j];
      }
    }
    int max = t[w],maxweight = w;
    for(int i=w-1;i>=0 ;i--){                      
      if ( t[i] >= max ){
        max = t[i];
        maxweight = i;
      }  
    }
    cout << "Case " << Case++ <<":" <<endl <<max <<  endl<< maxweight << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int W, N, w[1005], v[1005];
P dp[1005][10005];

P solve(int i, int j)
{
	if (dp[i][j].first >= 0) return dp[i][j];
	if (i == N) return P(0, 0);
	P res;
	if (j < w[i]) res = solve(i + 1, j);
	else
	{
		P a = solve(i + 1, j);
		P b = solve(i + 1, j - w[i]);
		b.first += v[i]; b.second += w[i];
		if (a.first != b.first) res = max(a, b);
		else res = min(a, b);
	}

	return dp[i][j] = res;
}

int main()
{
	int cse = 0;
	while (cin >> W >> N, W)
	{
		cse++;
		MS(dp, -1);
		char c;
		REP(i, N) cin >> v[i] >> c >> w[i];
		cout << "Case " << cse << ":" << endl << solve(0, W).first << endl << solve(0, W).second << endl;

	}
}

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

using namespace std;

int main() {
	int n, W;
	char dummy;
	int count = 1;

	while( true ) {

		cin >> W;
		if(W == 0) {
			break;
		}
		cin >> n;

		vector< int > item_val(n);
		vector< int > item_wei(n);
		vector< vector< int > > nap(n + 1, vector< int >(W + 1, 0));

		for(int i = 0; i < n; i++) {
			cin >> item_val[i] >> dummy >> item_wei[i];
		}

		for(int i = 0; i < n; i++) {
			for(int j = 0; j <= W; j++) {
				if(j + item_wei[i] <= W) {
					nap[i + 1][j + item_wei[i]] = max(nap[i + 1][j + item_wei[i]], nap[i][j] + item_val[i]);
				}
				nap[i + 1][j] = max(nap[i + 1][j], nap[i][j]);
			}
		}

		int max_weight = W;
		int max_value = 0;
		for(int j = 0; j <= W; j++) {
			if(max_value < nap[n][j]) {
				max_value = nap[n][j];
				max_weight = j;
			}
		}

		cout << "Case :" << count << endl;
		cout << max_value << "\n" << max_weight << endl;
		count++;

	}
}

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(50,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;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 W,N;
  int count = 0;
  while(cin >> W){
    if(W==0) break;
    cin >> N;
    count++;
    int item[W+1];
    for(int i=0;i<=W;i++) item[i] = 0;

    int v,w;
    char c;
    for(int i=0;i<N;i++){
      cin >> v >> c >> w;
      for(int j=W-w;j>=0;j--)
        if(item[j]+v>item[j+w]) item[j+w] = item[j]+v;
    }
    int ansv, answ;
    ansv = answ = -1;
    for(int i=0;i<=W;i++){
      if(item[i] > ansv){
        ansv = item[i];
        answ = i;
      }
    }
    cout << "Case " << count << ":" << endl << ansv << endl << answ << endl;
  }
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
const int INF=-1;
int main(){
    int W,cnt=0;
    while(cin>>W,W){
        cnt++;
        int dp[1000],n;
        cin>>n;

        for(int i=0;i<1000;i++)dp[i]=INF;

        dp[0]=0;
        for(int i=0;i<n;i++){
            int v,w;
            scanf("%d,%d",&v,&w);
            for(int j=W-1;j>=0;j--){
                if(dp[j]==INF)continue;
                if(j+w>W)continue;
                dp[j+w]=max(dp[j+w],dp[j]+v);

            }
        }
        pair<int,int>P;
        P.first=0;
        for(int i=0;i<W;i++){
            if(dp[i]>P.first){
                P.first=dp[i];
                P.second=i;
            }
        }
        cout<<"Case "<<cnt<<":"<<endl;
        cout<<P.first<<endl<<P.second<<endl;
    }
    return 0;

}