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>
using namespace std;

struct treasure{
	int value;
	int weight;
};
bool comp(treasure t1, treasure t2){
	if(t1.weight <= t2.weight) return true;
	else return false;
}

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	while(cin >> w){
		int n;
		int cost[maxw + 1];
		int best[maxw + 1];
		for(int i = 0; i <= maxw; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		vector<treasure> treasures;
		cin >> n;
		for(int i = 0; i < n; i++){
			treasure t;
			char c;
			cin >> t.value >> c >> t.weight;
			treasures.push_back(t);
		}
		sort(treasures.begin(), treasures.end(), comp);
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= treasures[j].weight)
					if(cost[k] < cost[k - treasures[j].weight] + treasures[j].value)
					{
						cost[k] = cost[k - treasures[j].weight] + treasures[j].value;
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - treasures[best[l]].weight >= 0;){
			if(l - treasures[best[l]].weight >= 0){
				total += treasures[best[l]].weight;
				l -= treasures[best[l]].weight;
			}
		}
		cout << total << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 int dp[1001][1001];
int main(){
    int cnt=0;
    int N,W;
    int w[1000],v[1000];
    while(cin>>W,W){
        cnt++;
        cin>>N;
        fill(dp[0],dp[0]+1001*1001,-1);
        dp[0][0]=0;
        for(int i=0;i<N;i++)(cin>>v[i]).ignore()>>w[i];
        for(int i=0;i<N;i++){
            for(int j=0;j<=W;j++){
                int res;
                if(j-w[i]<0)res=dp[i][j];
                else if(dp[i][j-w[i]]==-1)res=dp[i][j];
                else res=max(dp[i][j],dp[i][j-w[i]]+v[i]);
                dp[i+1][j]=res;
            }
        }
        for(int i=W;i>=0;i--){
            if(dp[N][i]!=-1&&dp[N][i]>dp[N][i-1]){
                cout<<"Case "<<cnt<<":"<<endl;
                cout<<dp[N][i]<<endl<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

C++: //0042 A Thief
//vñFibvTbNâè
//Ql http://algorithms.blog55.fc2.com/blog-category-6.html

#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;

int main(void)
{

	const int MAX_WEIGHT = 1000 + 1;
	const int MAX_OBJECT = 1000 + 1;
	const int MAX_SCORE = 10000;

	int caseNum = 1;
	while(1)
	{
		int totalWeight, totalN;
		int i, j;
		static int c[MAX_OBJECT][MAX_WEIGHT];	//X^bNÉæçÈ¢ÌÅÃIÌæÉu­
		for(i = 0; i < MAX_OBJECT; i++)
			for(j = 0; j < MAX_WEIGHT; j++)
				c[i][j] = 0;
		char dummy;

		cin >> totalWeight;
		if(totalWeight == 0)
			break;
		cin >> totalN;

		int score, w;
		for(i = 0; i < totalN; i++)
		{
			cin >> score >> dummy >> w;

			//eÊjÌibvTbNÉüé©AüéÈçAüêéÆ«üêÈ¢Æ«ÇÁ¿ª¢©
			for(j = 0; j <= totalWeight; j++)
			{
				//ibvTbNÉüèÜ·@©ÂAüê½Æ«ÍüêÈ¢Æ«æè¾µÜ·
				if(w<=j && c[i][j-w]+score > c[i][j])
				{
					c[i+1][j] = c[i][j-w] + score;
				}
				//»ÌiÍgíÈ¢
				else
				{
					c[i+1][j] = c[i][j];
				}
			}
		}

		cout << "Case " << caseNum << ":" << endl;
		caseNum++;


		w = totalWeight;
		//¯¶¿lÅæèy¢àÌª é©²×é
		while(1)
		{
			if(c[totalN][totalWeight] == c[totalN][w-1])
				w--;
			else
				break;
		}
		cout << c[totalN][totalWeight] << endl << w << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>

using namespace std;

int N,W;

struct Treasurea
{

  int value;
  int weight;
  int flag;

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

  Treasurea& operator+=(const Treasurea &obj)
  {
    value += obj.value;
    weight += obj.weight;
  };

};

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(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);

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

      /*
      if((total.value < knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).value ) || (total.value == knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).value && total.weight > knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).weight))
         total = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);
      */




      treasurea[i].flag = false;

    }

  return total;

}

int main()
{

  int case_number = 0;
  vector<Treasurea> treasurea(N);
  Treasurea result;

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

      cin >> N;

      for(int i = 0; i < N; ++i)
        {

          Treasurea temp;
          scanf("%d,%d",&temp.value,&temp.weight);
          treasurea.push_back(temp);

        }

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

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

      treasurea.erase(treasurea.begin(),treasurea.end());

    }

  return 0;

}