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

struct Treasure{
	int Weight;
	int Worth;
};
vector<int> TempMaxWorth;

int GetMaxWorth(const int MaxWeight,vector<Treasure> nowTreasureCollection, vector<Treasure>::iterator nowIterator,const vector<Treasure>& AllTreasureCollection){
	 vector<Treasure>::iterator rootIterator(nowIterator);
	for (; nowIterator!=AllTreasureCollection.end(); ++nowIterator){
		nowTreasureCollection.push_back(*(nowIterator));
		const int NowWorth = accumulate(nowTreasureCollection.cbegin(),
			nowTreasureCollection.cend(),
			0,
			[](const int q, const Treasure& p){return q + p.Worth; }
		);
		const int NowWeight = accumulate(nowTreasureCollection.cbegin(),
			nowTreasureCollection.cend(),
			0,
			[](const int q, const Treasure& p){return q + p.Weight; });
		if (NowWeight <= MaxWeight&&NowWorth > TempMaxWorth[NowWeight]){
			TempMaxWorth[NowWeight] = NowWorth;
			replace_if(TempMaxWorth.begin() + NowWeight, TempMaxWorth.end(), [=](int n){return NowWorth > n; }, NowWorth);
			if((++nowIterator)!=AllTreasureCollection.end())GetMaxWorth(MaxWeight, nowTreasureCollection, nowIterator,AllTreasureCollection);
			nowIterator--;
		}
		nowTreasureCollection.pop_back();
	}
	((nowIterator)) = (rootIterator);
	return 0;
}

int main(){
	int MaxWeight;
	int TreasureNum;
	for (int i = 1; cin >> MaxWeight;++i){
		if (MaxWeight == 0)return 0;
		cin  >> TreasureNum;
		TempMaxWorth.resize(MaxWeight + 1);
		fill(TempMaxWorth.begin(), TempMaxWorth.end(), 0);
		vector<Treasure> AllTreasureCollection;
		for (int j = 0; j < TreasureNum; ++j){
			Treasure T;
			cin >> T.Worth;
			cin.get();
			cin >> T.Weight;
			AllTreasureCollection.push_back(T);
		}	
		vector<Treasure> NowTreasureCollection = {};
		GetMaxWorth(MaxWeight, NowTreasureCollection, AllTreasureCollection.begin(), AllTreasureCollection);
		cout << "Case " << i << ":" << endl << TempMaxWorth[MaxWeight] << endl << find(TempMaxWorth.cbegin(), TempMaxWorth.cend(), TempMaxWorth[MaxWeight]) - TempMaxWorth.cbegin() << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define onen(i,n) for(int i=1;i<n+1;i++)

const int MAXW = 1001, MAXN = 1001;
int dp[MAXN][MAXW], w[MAXN], v[MAXN];
int n, maxw;

int main()
{
    int setnum = 1;
    while(cin >> maxw && maxw)
    {
        memset(dp, 0, sizeof(dp));
        char t;
        
        cin >> n;
        REP(n)cin >> v[i] >> t >> w[i];

        rep(i,n)onen(j,maxw)
        {
            dp[i+1][j] = max(dp[i][j], dp[i+1][j-1]);
            if(j>=w[i] ){ dp[i+1][j] = max(dp[i+1][j] , dp[i][j - w[i] ] + v[i]); }
        }

        int maxv = -1, weight = 0;
        onen(j,maxw)if(dp[n][j] > maxv ) { maxv = dp[n][j]; weight = j;}
        
        cout << "Case " << setnum << ":" << endl;
        cout << maxv << endl;
        cout << weight << endl;
        setnum++;
    }

    return 0;

}

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

using namespace std;

typedef int weight_t;
int W,N;

int tbl[2][1001];

int main(){
    int round=0;
    while(scanf("%d",&W)&&W!=0){
        round++;
        scanf("%d",&N);
        //初期化
        for(int i=0;i<=W;i++)
            tbl[0][i] = 0, tbl[1][i] = 0;
        tbl[0][0]=0;



        //動的計画法
        for (int i=0;i<N;i++){
            int v,w;
            scanf("%d,%d",&v,&w);

            // printf("%d",i);
            int index=i%2;
            //jはweightのループ変数
            for (int j=0;j<=W;j++){
                int next_index=(index+1)%2;
                //宝物をとらない選択肢
                tbl[next_index][j] = max(tbl[next_index][j], tbl[index][j]);

                //宝物をとる選択肢
                int new_weight=j+w;
                if (new_weight<=W){
                    tbl[next_index][new_weight]=max(tbl[next_index][new_weight],tbl[index][j]+v);
                }

            }

            // for (int k=0;k<)
        }

        int final_index = N % 2;
        int value = 0, weight = 0;
        for (int i = 0; i <= W; i++)
        {
            if (tbl[final_index][i] > value)
            {
                value = tbl[final_index][i];
                weight = i;
            }
        }

        printf("Case %d:\n%d\n%d\n", round, value, weight);
    }
    return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

int main(){
	int W, N, value, weight, num = 1;
	int v[1000], w[1000];
	int dp[1000][1000]; //[n][w]
		
	while(true){
		scanf("%d", &W);
		if(W == 0) break;
		scanf("%d", &N);
		for(int i = 0; i < N; i++){
			scanf("%d,%d", &v[i], & w[i]);
		}
		for(int i = 0; i < 1000; i++){
			for(int j = 0; j < 1000; j++){
				dp[i][j] = 0;
			}
		}
		
		for(int i = N - 1; i >= 0; i--){
			for(int j = 0; j < W; j++){
				if(j < w[i]) dp[i][j] = dp[i + 1][j];
				else dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
			}
		}
		/*for(int i = 0; i < N; i++){
				cout << v[i] << " " << w[i] << endl;
		}
		for(int i = 0; i <= N; i++){
			for(int j = 0; j < 109; j++){
				cout << dp[i][j] << " " ;
			}
			cout << endl;
		}*/
		value = 0;
		weight = 0;
		for(int j = 0; j < W; j++){
			if(value < dp[0][j]){
				value = dp[0][j];
				weight = j;
			}
		}
		cout << "Case " << num << ":" << endl;
		cout << value << endl << weight << endl;
		num++;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (int i = (int)a; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MOD 1000000007
#define INF 1000000000
#define PI 3.14159265359
#define EPS 1e-12

int W, N, v[1010], w[1010], dp[1010];

int main(void) {
  int ca = 1;
  while (true) {
    cin >> W;
    if(W == 0) break;
    cin >> N;
    REP(i, N) {
      cin >> v[i];
      getchar();
      cin >> 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 a = -INF, b = 0;
    for (int i = 0; i < W+1; i++) {
      //cout << dp[i] << " ";
      if (a < dp[i]) {
        a = dp[i];
        b = i;
      }
    }
    //cout << endl;
    cout << "Case " << ca << ":" << endl << a << endl << b << endl;
    ca++;
  }
  return 0;
}