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

}