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<iostream>
using namespace std;

typedef struct{
  int g;
  int ye;
}SET;

int main(){
  
  int n,w;
  int cnt=1;
  while(1){
    
    scanf("%d",&w);
    if(w==0)break;
    scanf("%d",&n);
    SET data[1111]={0}; 
    int memo[1111][11111]={{0}};
    int max_y=0;
    int max_g=1111;
    
    for(int i=0;i<n;i++){
      scanf("%d,%d",&data[i].ye,&data[i].g);
    }
    memo[0][data[0].g]=data[0].ye;
    for(int i=1;i<n;i++){
      if(data[i].ye>memo[i-1][data[i].g]){
	memo[i][data[i].g]=data[i].ye;
      }
      for(int j=1;j<=w;j++){
	if(memo[i-1][j]>0){
	  if(memo[i-1][j]>memo[i][j]){
	    memo[i][j]=memo[i-1][j];
	  }
	  if(j+data[i].g<=w){
	    memo[i][j+data[i].g]=memo[i-1][j]+data[i].ye;
	  }
	}
	if(max_y<=memo[i][j]){
	  if(max_y==memo[i][j]){
	    if(max_g>j){
	      max_g=j;
	    }
	  }
	  else{
	    max_g=j;
	    max_y=memo[i][j];
	  }
	}
      }    
    }
    printf("Case %d:\n%d\n%d\n",cnt,max_y,max_g);
    cnt++;
  }
}

C++: #include<stdio.h>

int main()
{
	for(int c=1;;c++)
	{
		int w,n,kati[100],weit[100],weitk=0,katik=0;
		scanf("%d",&w);
		if(w==0)
		{
			return 0;
		}
		scanf("%d",&n);
		
		int katisum[100][100]={0},weitsum[100][100]={0};
		for(int x=0;x<n;x++)
		{
			scanf("%d,%d",&kati[x],&weit[x]);
		}
		for(int y=0;y<n;y++)
		{
			for(int z=y;z<n;z++)
			{
				for(int r=y;r<=z;r++)
				{
					katisum[y][z]+=kati[r];
					weitsum[y][z]+=weit[r];
				}
			}
		}

		for(int y=0;y<n;y++)
		{
			for(int z=0;z<n;z++)
			{
				if(w>=weitsum[y][z]&&katik<katisum[y][z])
				{
					weitk=weitsum[y][z];
					katik=katisum[y][z];
				}
				
			}
		}
		for(int y=0;y<n;y++)
		{
			for(int z=0;z<n;z++)
			{
				if(katik==katisum[y][z]&&weitk>weitsum[y][z])
				{
					weitk=weitsum[y][z];
				}
			}
		}
		printf("Case %d\n%d\n%d\n",c,katik,weitk);
	}
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
	int w;
	int c = 1;

	while ( true )
	{
		cin >> w;

		if ( w == 0 )
		{
			break;
		}
		
		int n;
		cin >> n;
		cin.ignore( 1 );

		vector<int> price, weight;

		for ( int i = 0; i < n; i++ )
		{
			string buffer;
			
			cin >> buffer;

			for ( unsigned int i = 0; i < buffer.length(); i++ )
			{
				if ( buffer[i] == ',' )
				{
					buffer[i] = ' ';
					break;
				}
			}

			int input;
			istringstream iss( buffer );
			
			iss >> input;
			price.push_back( input );

			iss >> input;
			weight.push_back( input );
		}

		vector< vector<int> > dp;
		dp.push_back( vector<int>() );

		dp[0].resize( w + 1 );
		fill( dp[0].begin(), dp[0].end(), 0 );

		for ( unsigned int i = 1; i <= (unsigned)n; i++ )
		{
			dp.push_back( vector<int>() );
			dp[i].resize( w + 1 );
			fill( dp[i].begin(), dp[i].end(), 0 );

			for ( unsigned int j = 0; j <= (unsigned)w; j++ )
			{
				if ( weight[i] <= (int)j )
				{
					dp[i][j] = max( dp[i-1][j-weight[i]] + price[i], dp[i-1][j] );
				}
				else
				{
					dp[i][j] = dp[i-1][j];
				}
			}
		}
		
		vector<int> result = *dp.rbegin();

		unsigned int res = 0;
		int m = result[0];

		for ( unsigned int i = 1; i < result.size(); i++ )
		{
			if ( m < result[i] )
			{
				m = result[i];
				res = i;
			}
		}
		
		cout << "Case " << c++ << ":" << endl;
		cout << m << endl;
		cout << res << endl;
	}

	return 0;
}

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;

}