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<string>
#include<stdio.h>
using namespace std;

int MinW,MaxV;

void Func(int*w,int*v,int n,int i,int limw,int sumw,int sumv){
	
	if( sumw > limw )return;
	
	if( sumv > MaxV ){
		MaxV = sumv;
		MinW = sumw;
	}
	if( sumv == MaxV && sumw<MinW ){
			MinW = sumw;
	}
	for(int j=i;j<n;++j){
		Func(w,v,n,j+1,limw,sumw+w[j],sumv+v[j]);
	}
}


int main(){
	int W,N;
	int i,c=0;
	int *weight,*value;
	
	while(cin>>W,W){
		++c;
		cin>>N;
		
		MaxV=0;
		MinW=W;
		
		weight = new int[N];
		value = new int[N];
		
		for(i=0;i<N;++i){
			scanf("%d,%d",&value[i],&weight[i]);
		}
		
		Func(weight,value,N,0,W,0,0);
		
		printf("Case %d:\n%d\n%d\n",c,MaxV,MinW);	
		
	}
}

C++: #include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;

ll value[1000], weight[1000];
ll dp[1001][1001]; // dp[?¬?????????????][??????] = ?????§??????

int main() {
	int W;
	int c = 1;
	while ( cin >> W, W ) {
		int N; cin >> N;
		for (int i = 0; i < N; ++i) scanf("%d,%d", &value[i], &weight[i]);

		for (int i = 0; i < 1001; ++i) {
			for (int j = 0; j < 1001; ++j) {
				dp[i][j] = 0;
			}
		}
//		fill(&dp[0][0], &dp[0][0]+1501*1501, 0);

		for (int i = 0; i < N; ++i) {
			for (int w = 0; w <= W; ++w) {
				dp[i+1][w] = max(dp[i+1][w], dp[i][w]);
				if (w + weight[i] <= W) {
					dp[i+1][w+weight[i]] = max(dp[i+1][w+weight[i]], dp[i][w]+value[i]);
				}
			}
		}
		ll av = 0, aw = 0;
		for (int w = 0; w < W; ++w) {
			if (dp[N][w] > av) {
				av = dp[N][w];
				aw = w;
			}
		}
		printf("Case %d:\n", c++);
		printf("%d\n", av);
		printf("%d\n", aw);
	}
}

C++: #include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
using namespace std;
int inf = 1000000000;
// dp[i][j] = i ???????????§??§?????? j ??§?????????
// dp[i][j] = dp[i][j-?] + v[i];

int dp[1001][1001];

int main(){

  int W, N, l;
  l = 1;
  while(cin >> W){
    if( W == 0 ) break;
    cin >> N;
    vector<int> v(N);
    vector<int> w(N);
    for(int i=0; i<N; i++) scanf("%d,%d", &v[i], &w[i]);

    for(int i=0; i<1001; i++) for(int j=0; j<1001; j++) dp[i][j] = 0;

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

    int answ = 1;
    for(int i=1; i<=W; i++){
      if( dp[N][answ] < dp[N][i] ){
        answ = i;
      }
    }

    cout << "Case " << l << ":" << endl;
    cout << dp[N][W] << endl;
    cout << answ << endl;

    l++;
  }




  return 0;
}

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

using namespace std;

void solve()
{
	int W;
	int N;
	int count = 0;
	while(cin >> W, W)
	{
		count++;
		cin >> N;
		vector<int> Value(N);
		vector<int> Weight(N);
		for(int i = 0; i < N; ++i)
		{
			scanf("%d,%d", &Value[i], &Weight[i]);
		}
		int maxVal = 0;
		vector< vector<int> > dp(N + 1, vector<int>(W + 1));
		for(int i = 0; i < N; ++i)
		{
			for(int j = 0; j < W; ++j)
			{
				if(j < Weight[i])				
				{
					dp[i + 1][j] = dp[i][j];
				}
				else
				{
					dp[i + 1][j] = max(dp[i][j], dp[i][j - Weight[i]] + Value[i]);
					if(maxVal < dp[i + 1][j])
					{
						maxVal = dp[i + 1][j];
					}
				}
			}
		}
		int index = 0;
		for(int i = 0; i < W + 1; ++i)
		{
			for(int j = 0; j < N + 1; ++j)
			{
				if(dp[j][i] == maxVal)
				{
					index = i;
					break;
				}
			}
		}
		cout << "Case " << count << ":" << endl;
		cout << maxVal << endl;
		cout << index << endl;
	}
}

int main()
{
	solve();
	return(0);
}