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

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

     memset(memo,0,sizeof(memo));

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

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

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

   }

 return 0;

}

C++: #include <iostream>
#include <map>
#include <stdio.h>
int main(){
  using std::cin;
  using std::cout;
  using std::endl;
  using std::map;
  int weight,n,w,v,s,c=1;
  map<int,int> m;
  while(cin>>weight){
    if(!weight)return 0;
    cin>>n;
    map<int,int>::iterator it;
    for(;n;n--){
      scanf("%d,%d",&v,&w);
      if(m.empty()){m[w]=v;continue;}
      it = m.begin();
      while(it!=m.end()){
	s=(*it).second+v;
	  if((*it).first+w<weight){
	    if(m[(*it).first+w]<s)m[(*it).first+w]=(*it).second+v;
	  }
	it++;
      }
      m[w]=v;
    }
    it=m.end();
    it--;
    cout<<"Case: "<<c++<<endl<<(*it).second<<endl<<(*it).first<<endl;
    m.clear();
  }
}

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

using namespace std;

int main() {
	int W, N;
	char d;
	int counter = 1;
	int takara[1000][2];
	while(1) {
		cin >> W;
		if (W == 0) {
			break;
		}
		cin >> N;
		for (int i = 0; i < N; i++) {
			cin >> takara[i][0] >> d >> takara[i][1];
		}
		vector< vector<int> > dp(W, vector<int> (N+1));
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j < W; j++) {
				dp[j][i] = 0;
			}
		}
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j < W; j++) {
				if (i == 0) {
					continue;
				}
				if (j < takara[i-1][1]) {
					dp[j][i] = dp[j][i-1];
				} else {
					dp[j][i] = max(dp[j][i-1], dp[j-takara[i-1][1]][i-1] + takara[i-1][0]);
				}
			}
		}
		int MAX[2];
		MAX[0] = 0;
		MAX[1] = 10000;
		for (int i = 0; i < W; i++) {
			if (MAX[0] == dp[i][N]) {
				MAX[1] = min(MAX[1],i);
			} else if (MAX[0] < dp[i][N]){
				MAX[0] = dp[i][N];
				MAX[1] = i;
			}
		}
		cout << "Case " << counter << ':' << endl << MAX[0] << endl << MAX[1] << endl;
		counter++;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<climits>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; i++)
#define FOR(i, j, k) for(int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;

int main(){
    int w, c = 1;
    while(cin >>w && w){
        int n;
        cin >>n;
        vector< vector<int> > dp(n + 1, vector<int>(w + 1));
        REP(i, dp.size()){
            REP(j, dp[i].size()){
                dp[i][j] = 0;
            }
        }
        dp[0][0] = 0;
        //input begin
        vector<P> j(n + 1);
        string str;
        getline(cin, str);
        FOR(i, 1, n + 1){
            getline(cin, str);
            stringstream ss(str);
            int a, b;
            char c;
            ss >>a >>c >>b;
            j[i].first = a;
            j[i].second = b;
        }
        //input end

        FOR(i, 1, dp.size()){
            int value = j[i].first, weight = j[i].second;
            REP(j, dp[i].size()){
                if(j >= weight){
                    dp[i][j] = max(dp[i][j], dp[i - 1][j - weight] + value);
                }
                dp[i][j] = max(dp[i][j], dp[i - 1][j]);
            }
        }

        int ans_v = -1, ans_w = 0;
        REP(i, dp[0].size()){
            if(dp[n][i] > ans_v){
                ans_v = dp[n][i];
                ans_w = i;
            }
        }
        cout <<"Case " <<c <<":" <<endl <<ans_v <<endl <<ans_w <<endl;
        c++;
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
main(){
  int b,c,N,K[1010],P[1010],i,m,j,W,dp[1010],h=1,y,g;
  char d;
  while(1){
    y=0;
    b=0;
    cin >> W;
    for(i=0;i<=W;i++){

      dp[i]=0;
    }
    if(W==0){
      break;
    }
    cin >> N;
    for(i=0;i<N;i++){
      cin >> P[i] >> d >> K[i];
      c=K[i];
      dp[K[i]]=P[i];
      for(j=W;j>=0;j--){
	if(dp[j]!=0){
	  if(dp[c+j]!=0){
	    if(dp[c+j]<dp[c]+dp[j]){
	      dp[c+j]=dp[c]+dp[j];
	    }
	    else{
	      continue;
	    }
	  }
	  if(dp[c+j]==0){
	    dp[c+j]=dp[c]+dp[j];
	  }
	}
      }
    }
    for(i=1;i<=W;i++){
      if(dp[i]>b){
	y=i;
	b=dp[i];
      }
      else{
	continue;
      }
    }
    cout << "Case" << " " << h << ":" << endl;
    cout << b << endl << y << endl;
    h++;
  }
}