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<cstdio>
#include<string>
#include<vector>
#include<map>

using namespace std;

struct Treasure
{

  long value;
  long weight;
  bool flag;

};


map<int,Treasure> memo;

bool flag;
long limit_weight;
long treasure_number;
Treasure result_treasure;

int AThief(vector<Treasure> &treasure,long &total_value,long &total_weight)
{

  if(limit_weight < total_weight){
    return 0;
  }
  else
    {

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

          if(treasure[i].flag)
            {

              total_value += treasure[i].value;
              total_weight += treasure[i].weight;
              treasure[i].flag = false;

              if(AThief(treasure,total_value,total_weight) == 0)
                {

                  total_value -= treasure[i].value;
                  total_weight -= treasure[i].weight;

                  if(result_treasure.value == total_value && result_treasure.weight > total_weight) result_treasure.weight = total_weight;
                  else if(result_treasure.value < total_value)
                    {
                      result_treasure.value = total_value;
                      result_treasure.weight = total_weight;
                    }
                  flag = false;
                }
              else
                {
                  total_value -= treasure[i].value;
                  total_weight -= treasure[i].weight;
                }

              treasure[i].flag = true;

            }

        }

    }

  //全部足しても耐えられる重さを越えなかった時
  if(flag)
    {
      result_treasure.value = total_value;
      result_treasure.weight = total_weight;
      flag = false;
    }


  return 1;

}


int main()
{

  int dummy;
  Treasure temp;
  int case_number = 0;
  long total_value = 0;
  long total_weight = 0;
  vector<Treasure> treasure;

  scanf("%d",&limit_weight);

  while(limit_weight!=0)
    {

      scanf("%d",&treasure_number);

      treasure.clear();

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

          scanf("%d,%d",&temp.value,&temp.weight);
          temp.flag = true;
          treasure.push_back(temp);

        }

      result_treasure.value = -1;
      result_treasure.weight = 2147483647;
      flag = true;

      dummy = AThief(treasure,total_value,total_weight);

      case_number++;

      cout << "Case " << case_number << ":" << endl;
      cout << result_treasure.value << endl;
      cout << result_treasure.weight << endl;

      cin >> limit_weight;

    }

}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>
#include <ctime>
#define INF 100000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;

/*---------------------------------------------------*/

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

void DEBUG(){
  rep(i,W+1)cout<<i<<": "<<dp[N][i]<<endl;
}

void init(){
  rep(i,1005){
    rep(j,1005){
      dp[i][j]=-INF;
    }
  }
}

void DP(){
  for(int i=0;i<1005;i++)dp[0][i]=0;
  for(int i=0;i<=N;i++){
    for(int j=0;j<=W;j++){
      if(j>=w[i])dp[i+1][j]=max(dp[i][j-w[i]]+v[i],dp[i][j]);
      else dp[i+1][j]=dp[i][j];
    }
  }
}

int main(){
  int cnt=1;
  while(cin>>W){
    init();
    if(W==0)break;
    cin>>N;
    rep(i,N)scanf("%d,%d",&v[i],&w[i]);
    DP();
    //DEBUG();
    int ans_value=0,ans_weight=0;
    rep(i,W+1){
      if(dp[N][i]>ans_value){
	ans_value=dp[N][i];
	ans_weight=i;
      }
    }
    cout<<"Case: "<<cnt<<":"<<endl;
    cout<<ans_value<<endl;
    cout<<ans_weight<<endl;
    cnt++;
  }
  return 0;
}

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

using namespace std;

#define price first
#define weight second

int main(){
	int W;
	int cnt = 1;
	while (cin >> W, W) {
		int N; cin >> N;
		vector<pair<int, int> > tres(N);
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &tres[i].price, &tres[i].weight);
		}
		vector<int> dp(W+1);
		for (int i = 0; i < W+1; i++) { dp[i] = -1; }
		dp[0] = 0;
		for (int i = 0; i < N; i++) {
			for (int j = W; j >= 0; j--) {
				if (dp[j] == -1) { continue; }
				if (j+tres[i].weight <= W) {
					dp[j+tres[i].weight] = max(dp[j]+tres[i].price, dp[j+tres[i].weight]);
				}
			}
		}
		int PRICE = 0;
		int WEIGHT = 0;
		for (int i = W; i >= 0; i--) {
			if (PRICE <= dp[i]) {
				PRICE = dp[i];
				WEIGHT = i;
			}
		}

		cout << "Case " << cnt << ":" << endl;
		cout << PRICE << endl;
		cout << WEIGHT << endl;
		cnt++;
	}
}