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