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<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+1)!=AllTreasureCollection.end())GetMaxWorth(MaxWeight, nowTreasureCollection, nowIterator+1,AllTreasureCollection);
		}
		if (NowWeight <= MaxWeight){
			if ((nowIterator + 1) != AllTreasureCollection.end())GetMaxWorth(MaxWeight, nowTreasureCollection, nowIterator + 1, AllTreasureCollection);
		}
		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);
		}
		sort(AllTreasureCollection.begin(), AllTreasureCollection.end(), [](const Treasure a, const Treasure b){return ((float)(a.Worth) / (float)(a.Weight)) > ((float)(b.Worth) / (float)(b.Weight)); });
		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<sstream>
#include<vector>
#include<map>
#define pb(a) push_back(a)
using namespace std;
typedef pair<int,int> P;
int main(){
  int c=0;
  int w,n,i,j,k;;
  while(cin >> w,w){
    c++;
    cout << "Case "<<c<<":"<<endl;
    cin >> n;
    string s;
    vector<P> v;
    P p,ans;
    for(k=0;k<n;k++){
      cin >> s;
      for(i=0;i<s.size();i++) if(s[i]==',') s[i]=' ';
      stringstream ss(s);
      ss>>p.first >>p.second;
      v.pb(p);
    }
    int dp[1005][1005]={{}};
    fill(dp[0],dp[1004],0);
    for(i=0;i<n;i++){
      //cout << v[i].first << ":" << v[i].second << endl;
      for(j=v[i].second;j<=w;j++){
	dp[i+1][j]=max(dp[i+1][j],dp[i][j-v[i].second]+v[i].first);
      }
    }
    ans.first=0;ans.second=0;
    for(i=0;i<=w;i++){
      //cout << i << ":" << dp[n][i] << endl;
      if(dp[n][i]>ans.first){
	ans.first=dp[n][i];
	ans.second=i;
      }
    }
    cout << ans.first << endl << ans.second << endl;
  }
  return 0;
}

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

int main(){
	int w, cntcase = 1;
	while (cin >> w, w){
		int n, dp[1001], treas_val, treas_w, ans_val = 0, ans_w = 0;
		cin >> n;
		for (int i = 0; i <= w; i++) dp[i] = -1;
		dp[0] = 0;
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &treas_val, &treas_w);
			for (int i = w; i >= 0; i--){
				if (dp[i] != -1 && i + treas_w < w){
					dp[i + treas_w] = dp[i] + treas_val;
					if (ans_val < dp[i] + treas_val){
						ans_val = dp[i] + treas_val;
						ans_w = i + treas_w;
					}
				}
			}
		}
		printf("Case %d:\n%d\n%d\n", cntcase, ans_val, ans_w);
		cntcase++;
	}
	return 0;
}

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

// 入力
int W, n;
int v[1001], w[1001];

int dp[1001][1001];
int ans_weight;

void solve(){
    // 価値の最大値を求める
    for(int i=0 ; i < n ; i++ ){
        for(int j=0 ; j <= W ; j++ ){
            if( j < w[i] ){
                dp[i+1][j] = dp[i][j];
            }else{
                dp[i+1][j] = max( dp[i][j] , dp[i][j-w[i]] + v[i] );
            }
        }
    }
    // 価値の最大値dp[n][W]となる最小の重さの総和を求める
    for(int j=0 ; j <= W ; j++ ){
        if( dp[n][j] == dp[n][W] ){
            ans_weight = j;
            break;
        }
    }
}

int main(){
    for(int t=1 ; scanf("%d", &W) , W ; t++ ){
        // 配列の初期化
        for(int i=0 ; i < 1001 ; i++ ){
            for(int j=0 ; j < 1001 ; j++ ){
                dp[i][j] = 0;
            }
        }
        
        scanf("%d", &n);
        for(int i=0 ; i < n ; i++ ){
            scanf("%d,%d", &v[i], &w[i]);
        }
        solve();
        printf("Case %d:\n", t);
        printf("%d\n%d\n", dp[n][W], ans_weight );
    }
}

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]