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<cstdio>
#include<vector>

using namespace std;

int N,W;

struct Treasurea
{

	int value;
	int weight;
	int flag;

	Treasurea()
	{

		value = 0;
		weight = 0;
		flag = false;

	}

};

struct Memo
{

	Treasurea record;
	bool flag;

	Memo()
	{

		flag = false;

	}

};

Memo memo[1001][1001];

Treasurea knapsack(vector<Treasurea> &treasurea, int correntWeight, int correntValue, int kind)
{

	if(correntWeight < 0)
	{

		Treasurea temp;
		temp.value = correntValue - treasurea[kind - 1].value;
		temp.weight = W - (correntWeight + treasurea[kind - 1].weight);
		return temp;

	}
	else if(treasurea.size() == kind)
	{

		Treasurea temp;
		temp.value = correntValue;
		temp.weight = W - correntWeight;
		return temp;

	}
	//else if(memo[correntWeight][kind].flag) return memo[correntWeight][kind].record;


	Treasurea total;

	for(int i = 0; i < N; ++i)
	{

		if(treasurea[i].flag) continue;
			treasurea[i].flag = true;

		Treasurea temp = knapsack(treasurea,correntWeight - treasurea[i].weight, correntValue + treasurea[i].value, i + 1);

		if( (total.value < temp.value) || (total.value == temp.value && total.weight > temp.weight))
			total = temp;

		treasurea[i].flag = false;

	}

	memo[correntWeight][kind].record = total; 
	memo[correntWeight][kind].flag = true;

	return total;

}

int main()
{

	int case_count = 0;
	
	while(cin >> W && W != 0)
	{
		
		cin >> N;

		vector<Treasurea> treasurea(N);
		Treasurea result;

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

		result = knapsack(treasurea,W,0,0);

		cout << "Case " << ++case_count << ":" << endl << result.value << endl << result.weight << endl;

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

	}

}

C++: #include<bits/stdc++.h>
using namespace std;

int W,n,v,w;
int dp[1001],casenum;

int main(){
  casenum = 1;
  while(scanf("%d",&W), W){
    scanf("%d",&n);
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++){
      scanf("%d,%d",&v,&w);
      for(int i=W;i>=0;i--){
	if(i+w <= W){
	  dp[i+w] = max(dp[i+w], dp[i] + v);
	}
      }
    }

    int ansv = 0, answ = 0;
    for(int i=0;i<=W;i++){
      if(ansv < dp[i]){
	ansv = dp[i]; answ = i;
      }
    }
    printf("Case %d:\n%d\n%d\n",casenum, ansv, answ);
    casenum++;
  }
}

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

#pragma warning(disable: 4996)

using namespace std;

#define MAX_N 1000
#define MAX_W 1000

int W, N, c, v[MAX_N], w[MAX_N], dp[MAX_N + 1][MAX_W + 1];

int main()
{
	while (true)
	{
		c++;

		cin >> W;

		if (!W) { break; }

		cin >> N;

		for (int i = 0; i < N; i++)
		{
			scanf("%d,%d", &v[i], &w[i]);
		}

		for (int j = 0; j <= MAX_W; j++)
		{
			dp[0][j] = 0;
		}

		for (int i = 1; i <= MAX_N; i++)
		{
			for (int j = 0; j <= MAX_W; j++)
			{
				dp[i][j] = -(1 << 28);
			}
		}

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

		cout << "Case " << c << ':' << endl;
		cout << dp[N][W] << endl;
		cout << distance(dp[N], lower_bound(dp[N], dp[N] + W + 1, dp[N][W])) << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;

int main(){
	int W,N,cou=0;
	int tr[1001],we[1001];
	while(cin >> W , W){
		cou++;
		cin >> N;
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		P dp[1010][1001];
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				dp[i][j] = P(0,0);
			}
		}
		dp[0][0] = P(0,0);
		for(int i=1;i<=N;i++){
			dp[i][0] = P(0,0);
			for(int j=0;j<i;j++){
				if(dp[i-1][j].second+we[i] <= W){
					P p;
					p = P(dp[i-1][j].first+tr[i],dp[i-1][j].second+we[i]);
					if(p.first == dp[i-1][j+1].first){
						if(p.second < dp[i-1][j+1].second) dp[i][j+1] = p;
						else dp[i][j+1] = dp[i-1][j+1];
					}
					else if(p.first > dp[i-1][j+1].first){
						dp[i][j+1] = p;
					}
					else dp[i][j+1] = dp[i-1][j+1];
				}
				else dp[i][j+1] = dp[i-1][j+1];
			}
		}
		int ans1=0,ans2=0;
		for(int i=0;i<=N;i++){
			if(dp[N][i].first == ans1){
				if(dp[N][i].second < ans2) ans2 = dp[N][i].second;
			}
			else if(dp[N][i].first > ans1){
				ans1 = dp[N][i].first;
				ans2 = dp[N][i].second;
			}
		}

		cout << "Case " << cou << ":" << endl;
		cout << ans1 << endl;
		cout << ans2 << endl;
	}

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

#define SAFE_DELETE( x ){ if( x ){ delete x; x = NULL; } }

struct tCell{
	tCell() : value( 0 ), weight( 0 ), unitV( 0.0 ), prev( NULL ), next( NULL ){}

	int value, weight;
	double unitV;
	tCell *prev, *next;
};

class MyArray{
public:
	MyArray() : mSize( 0 ), head( NULL ), tail( NULL ){
		head = new tCell;
		tail = new tCell;
		head->next = tail;
		tail->prev = head;
	}
	~MyArray(){
		init();
		SAFE_DELETE( tail );
		SAFE_DELETE( head );
	}

	void init(){
		for ( int i = mSize; i > 0; --i ){
			tCell* cell = tail->prev;
			cell->prev->next = cell->next;
			cell->next->prev = cell->prev;
			SAFE_DELETE( cell );
		}
		mSize = 0;
	}
	void pushBack( int value, int weight ){
		++mSize;
		tCell* cell = new tCell;
		cell->value = value;
		cell->weight = weight;
		cell->unitV = static_cast< double >( value ) / weight;
		cell->prev = tail->prev;
		cell->next = tail;
		cell->prev->next = cell;
		cell->next->prev = cell;
	}
	int size(){ return mSize; }

	tCell operator[]( int index ){
		tCell* cell = head->next;
		for ( int i = 0; i < index; ++i ){
			cell = cell->next;
		}
		return *cell;
	}

private:
	int mSize;
	tCell *head, *tail;
};

int main(){

	MyArray treList;
	int time = 1, c;
	int v = 0, w = 0;
	char d;

	while ( cin >> c ){
		if ( !c ) break;
		int n;
		treList.init();
		cin >> n;
		for ( int i = 0; i < n; ++i ){
			cin >> v >> d >> w;
			treList.pushBack( v, w );
		}

		int maxV = 0, maxW = 0;
		int* table = new int[ ( n+1 ) * ( c+1 ) ];
		for ( int i = 0; i < n+1; ++i ){
			if ( i != 0 ){
			v = treList[ i - 1 ].value;
			w = treList[ i - 1 ].weight;
			}
			for ( int j = 0; j < c+1; ++j ){
				if ( i == 0 || j == 0 ){
					table[ i * ( c+1 ) + j ] = 0;
					continue;
				}
				table[ i * ( c+1 ) + j ] = table[ ( i-1 ) * ( c+1 ) + j ];
				if ( ( j - w >= 0 ) && ( table[ ( i-1 ) * ( c+1 ) + ( j-w ) ] + v > table[ ( i-1 ) * ( c+1 ) + j ] ) ){
					table[ i * ( c+1 ) + j ] = table[ ( i-1 ) * ( c+1 ) + ( j-w ) ] + v;
					if ( table[ i * ( c+1 ) + j ] > maxV ){
						maxV = table[ i * ( c+1 ) + j ];
						maxW = j;
					}else if ( table[ i * ( c+1 ) + j ] == maxV && j < maxW ){
						maxW = j;
					}
				}
			}
		}

		cout << "Case " << time << ":" << endl;
		cout << maxV << endl << maxW << endl;
		++time;

		delete[] table;
	}

	return 0;
}