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 <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fr first
#define sc second
using namespace std;

int w,n;
typedef pair<int,int> P;
P dp[1001][1001];
bool chg[1001][1001];
vector <P> dat;

P rec(int idx,int wi){
    if(idx == n){
        return P(0,wi);
    }
    if(chg[idx][wi]) return dp[idx][wi];
    P p;
    if(wi - dat[idx].sc >= 0){
        p = rec(idx+1,wi-dat[idx].sc);
        p.first += dat[idx].fr;
        p = max(p,rec(idx+1,wi));
    }
    else p = rec(idx+1,wi);
    chg[idx][wi] = true;
    return dp[idx][wi] = p;
}

int main(){
    while(cin >> w,w){
        int a = 0;
        memset(dp,-1,sizeof(dp));
        memset(chg,false,sizeof(chg));
        cin >> n;
        for(int i=0;i<n;i++){
            int t,m; scanf("%d,%d",&t,&m); dat.push_back(P(t,m));
        }
        P p = rec(0,w);
        cout << "Case " << ++a << ":\n" << p.fr << endl << w - p.sc << endl;
        dat.clear();
    }
}

C++: #include <iostream>
#include <algorithm>
#include <utility>
#include <string>
#include <sstream>
#define rep(i, a, n) for(int i = a;i < n;i++)
using namespace std;


using P = pair<bool, int>;


int W, N;
int tv[1010];
int tw[1010];


void input(){
	string s;
	stringstream ss;
	cin >> N;
	rep(i, 0, N){
		cin >> s;
		replace(s.begin(), s.end(), ',', ' ');
		ss.clear();
		ss << s;
		ss >> tv[i] >> tw[i];
	}
}


int search(int i, int w){
	if(i == N) return 0;
	if(tw[i] <= w) return max(search(i+1, w-tw[i]) + tv[i], search(i+1, w));
	else if(tw[i] > w) return search(i+1, w);
	return 0;
}


int main(){
	int d = 0;
	while(d++, cin >> W, W){
		input();
		cout << search(0, W) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#define shosu(x) fixed<<setprecision(x)
using namespace std;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,P> pip;
const int inf=(1<<31)-1;

int W,N,num=0;
char t;
vi v,w,dp;

int main(){
	while(1){
		cin>>W;
		if(!W) break;
		num++;
		cin>>N;
		v=vi(N);
		w=vi(N);
		dp=vi(W+1,0);
		for(int i=0;i<N;i++){
			cin>>v[i]>>t>>w[i];
		}
		for(int i=0;i<N;i++){
			for(int j=W;j>=0;j--){
				if(j-w[i]<0) continue;
				else dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
			}
		}
		cout<<"Case "<<num<<':'<<endl;
		for(int i=W;i>0;i--){
			if(dp[i]!=dp[i-1]){
				cout<<dp[i]<<endl;
				cout<<i<<endl;
				break;
			}
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <functional>
#include <sstream>
#include <complex>
#include <climits>
using namespace std;

#define REP(i,a,n) for(int i=(a);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define EPS 1e-8

int dp[2][1010];

int main(){
	int w,n;
	pair<int,int> kati[1000];
	for(int T=1; scanf("%d",&w),w; T++){
		printf("Case %d:\n",T);
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		rep(i,n){
			int a,b; scanf("%d,%d",&a,&b);
			kati[i].first = a;
			kati[i].second = b;
		}
		rep(i,n){
			int cur = i%2;
			int next = (i+1)%2;
			for(int j=kati[i].second; j<=w; j++){
				dp[next][j] = max(dp[next][j],
											max(dp[cur][j-kati[i].second]+kati[i].first,dp[cur][j]));
			}
			rep(i,w+1)dp[cur][i]=0;
		}
		int sum=0,omosa=0;
		rep(i,w+1)if( sum<dp[n%2][i] ){
			sum = dp[n%2][i];
			omosa = i;
		}
		printf("%d\n%d\n",sum,omosa);
	}
	return 0;
}

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

using namespace std;



int main() {
    int furosiki;
    int case_count = 1;

    
    while(cin>>furosiki){
        if(furosiki==0){
            break;
        }
        
        
        
        int num;
        cin>>num;
        size_t bits;
        bits = num;
        bitset<1000> bs;
        
        
        int val[num];
        int wei[num];
        for(int i=0; i<num; i++)scanf("%d,%d",&val[i],&wei[i]);
        int binary_max=1;
        for(int i=0; i<num; i++)binary_max*=2;
        int max_val = 0;
        int max_wei = 0;
        for(int i=0; i<=binary_max; i++){
            bs = i;
            int temp_val=0;
            int temp_wei=0;
            for(int j=0; j<num; j++){
                temp_val+=bs[j]*val[j];
                temp_wei+=bs[j]*wei[j];
            }
            if(temp_wei<=furosiki){
                if(max_val<temp_val){
                    max_val=temp_val;
                    max_wei=temp_wei;
                }else if(max_val==temp_val && max_wei>temp_wei){
                    max_val=temp_val;
                    max_wei=temp_wei;
                }
            }
        }
        cout<<"case"<<case_count<<":"<<endl;
        cout<<max_val<<endl;
        cout<<max_wei<<endl;
        case_count++;
    }
    
    
    return 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;
            }
        }
    }
}