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 <cstdio>
#include <algorithm>
#define MAX_N 1000
#define MAX_W 1000
using namespace std;
int W;
int N; 

int w[MAX_N]; 
int v[MAX_N]; 
int dp[MAX_N + 1][MAX_W + 1];
bool input() 
{
  scanf("%d", &W);
  if(!W) return false;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) 
  {
    scanf("%d,%d", &v[i], &w[i]);
  }
  return true;
}
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]);
      }
    }
  }
}

int main() 
{

  int cnt = 0;

  while (input())
  {
    solve();
    int i;
    for (i = W; i >= 0; i--) 
    {
      if (dp[N][W] != dp[N][i]) break;
    }
    printf("Case %d:\n", ++cnt);
    printf("%d\n", dp[N][W]);
    printf("%d\n", i + 1);
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
//	cut here before submit 
//	freopen("testcase.thief", "r", stdin);
	string str = "";
	int count = 0;

	while (getline (cin, str ) ){
		int max_wight;
		stringstream ssw (str );
		ssw >> max_wight;

		if (max_wight == 0 ){
			break;
		} // end if
		++count;
		
		int n;
		getline (cin, str );
		stringstream ssn(str);
		ssn >> n;

		vector <int> W (n, 0 );
		vector <int> V (n, 0 );
		vector <int> DP (max_wight + 1, 0 );
		int i,j;

		for (i = 0; i < n; ++i){
			getline (cin, str );
			while (str.find (',') != string::npos ){
				str = str.replace ( str.find (','), 1, " " );
			} // end while
			stringstream ss(str);
			ss >> V[i] >> W[i];
		} // end for

		for (i = 0; i < n; ++i){
			vector <int> dp = DP;
			for (j = W[i]; j <= max_wight; ++j){
				int p = j - W[i];
				if (dp[p] + V[i] > DP[j] ){
					DP[j] = dp[p] + V[i];
				} // end if 
			} // end for
		} // end for

		int maxvalue = DP[max_wight];
		int minwight = max_wight;

		for (j = max_wight - 1; j >= 0; --j){
			if (DP[j] >= maxvalue){
				minwight = j;
				maxvalue = DP[j];
			} // end if
		} // end for
		cout << "Case " << count << ':' << endl;
		cout << maxvalue << endl;
		cout << minwight << endl;
			
	} // end while	
	
	return 0;
}

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

const int MAXW = 1000;
const int MAXN = 1000;
int DPV[MAXN + 4][MAXW + 4];
int DPW[MAXN + 4][MAXW + 4];
int W[MAXN + 4], V[MAXN + 4];

void init() {
  for (int i = 0; i < MAXN + 4; i++)
    W[i] = V[i] = 0;

  for (int i = 0; i < MAXN + 4; i++)
    for (int j = 0; j < MAXN + 4; j++)
      DPV[i][j] = DPW[i][j] = 0;
}

void solveV(int n, int w) {
  for (int i = n; i > 0; i--) {
    for (int j = 0; j <= w; j++) {
      if (j < W[i]) {
        DPV[i][j] = DPV[i + 1][j];
        DPW[i][j] = DPW[i + 1][j];
      } else {
        if (DPV[i + 1][j] > DPV[i + 1][j - W[i]]  + V[i]) {
          DPV[i][j] = DPV[i + 1][j];
          DPW[i][j] = DPW[i + 1][j];
        } else if (DPV[i + 1][j] == DPV[i + 1][j - W[i]] + V[i]) {
          DPV[i][j] = DPV[i + 1][j];
          DPW[i][j] = min(DPW[i + 1][j], DPW[i + 1][j - W[i]] + W[i]);
        } else {
          DPV[i][j] = DPV[i + 1][j - W[i]] + V[i];
          DPW[i][j] = DPW[i + 1][j - W[i]] + W[i];
        }
      }
    }
  }
}

int main() {
  int w, Case = 1;
  while (cin >> w and w != 0) {
    init();

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
      scanf("%d,%d", &V[i], &W[i]);

    solveV(n, w);

    cout << "Case " << Case << ":" << endl;
    cout << DPV[1][w] << endl << DPW[1][w] << endl;

    Case++;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>

using namespace std;

struct Treasure
{

  int value;
  int weight;

  Treasure()
  {
    value = 0;
    weight = 0;
  }

  void operator()( Treasure t)
  {

    cout << t.value << "," << t.weight << endl;

  }

  Treasure& operator+=(const Treasure& t )
  {

    this->value += t.value;
    this->weight += t.weight;
    return *this;

  }

  Treasure operator+(const Treasure& t)
  {
    Treasure tmp;
    tmp.value = this->value + t.value;
    tmp.weight = this->weight + t.weight;
    return tmp;
  }

  Treasure operator-(const Treasure& t)
  {
    Treasure tmp;
    tmp.value = this->value - t.value;
    tmp.weight = this->weight - t.weight;
    return tmp;
  }

};

Treasure max(Treasure t1,Treasure t2)
{

  if(t1.value == t2.value)
    {
      return (t1.weight < t2.weight) ? t1 : t2;
    }
  else
    {
      return (t1.value > t2.value) ? t1 : t2;
    }

}

int N,W;

Treasure memo[1000][1000];

vector<Treasure> treasure(1000);

Treasure Knapsack(int currentWeight,int currentCount)
{

  Treasure t1,t2,f;

  if(memo[currentWeight][currentCount].weight)
    {
      return memo[currentWeight][currentCount];
    }
  else if(N == currentCount)
    {
      Treasure tmp;
      return tmp;
    }
  else{

    t1 = Knapsack(currentWeight,currentCount + 1);
    t2 = Knapsack(currentWeight + treasure[currentCount].weight,currentCount + 1) + treasure[currentCount];

    if( W > currentWeight + treasure[currentCount].weight)
      {
        t2 = Knapsack(currentWeight + treasure[currentCount].weight,currentCount + 1) + treasure[currentCount];
      }
    else
      {
        t2 = Knapsack(currentWeight,currentCount + 1);
      }

    memo[currentWeight][currentCount] = max(t1,t2);

    return max(t1,t2);

  }

}

int main()
{

  int caseNumber = 0;

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

     cin >> N;

     for(int i = 0; i < N; ++i)
       { scanf("%d,%d",&treasure[i].value,&treasure[i].weight); }

     Treasure result = Knapsack(0,0);


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

     cout << result.value << endl << result.weight << endl;

   }

 return 0;

}

C++: #include <iostream>
using namespace std;
int main(){
	int W_max,N; int cnt=0;
	while(1){
	cin >> W_max ;
	if(W_max==0) break;
	cin >> N ;
	int weight[1000]={0}; int P[1000]={0}; char ch; int P_max=0;
	int dp[1001][1001]; 
	for(int i=0;i<1001;i++)
		for(int j=0;j<1001;j++) dp[i][j]=-1;//初期化上手くできなかった
	for(int i=0;i<N;i++) cin >> P[i] >> ch >> weight[i] ;
	dp[0][0]=0;
	for(int i=0;i<N;i++){
		for(int j=0;j<=W_max;j++){
			if(dp[i][j]==-1) continue;
			if(j+weight[i]<=W_max) dp[i+1][j+weight[i]]=max(dp[i+1][j+weight[i]],dp[i][j]+P[i]);
			dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
		};
	};
	int W_min=W_max;
	for(int i=0;i<=W_max;i++) if(dp[N][i]>P_max) P_max=dp[N][i];
	for(int i=0;i<=W_max;i++) if(dp[N][i]==P_max) W_min=min(W_min,i);
	cout << "Case " << ++cnt << ":\n" ;
	cout << P_max << '\n' << W_min << '\n' ;
	};
}