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<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 step)
{
	/*
	if(correntWeight < 0)
	{

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

	}
	*/
	if(treasurea.size() == step)
	{

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

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


	Treasurea total;

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

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

		if(correntWeight - treasurea[i].weight >= 0)  temp = knapsack(treasurea,correntWeight - treasurea[i].weight, correntValue + treasurea[i].value, step + 1);
		else
		{
			temp.value = correntValue;
			temp.weight = W - correntWeight;
		}

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

		treasurea[i].flag = false;

	}

	memo[correntWeight][step].record = total; 
	memo[correntWeight][step].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);

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

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

		treasurea.clear();

	}

}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>

#define loop(lp) for(int lp2 = 0; lp2 < lp; lp2 ++)
#define ascent(i, a, b) for(int i = a; i < b; i ++)

#define pb push_back

using namespace std;

int dp[1000][10000];

int main() {
	int w_max, count = 0;
	while(cin >> w_max, w_max) {
		count ++;
		int n;
		cin >> n;

		ascent(i, 0, n) {
			ascent(j, 0, w_max) {
				dp[i][j] = 0;
			}
		}
		

		vector<int> v, w;
		v.clear();w.clear();
		loop(n) {
			int tmp, tmp2;
			scanf("%d,%d", &tmp, &tmp2);
			v.pb(tmp);
			w.pb(tmp2);
		}
		
		int ww = 0;
		ascent(i, 0, n+1) {
			ascent(j, 0, w_max+1) {
				if(j < w[i]) {
					dp[i+1][j] = dp[i][j];
				}
				else {
					int a = dp[i][j];
					int b = dp[i][j-w[i]] + v[i];

					if(a < b) {
						dp[i+1][j] = b;
						ww = max(ww, j);
					}
					else {
						dp[i+1][j] = a;
					}
				}
			}
		}
				
		cout << "Case " << count << ":" << endl << dp[n][w_max] << endl << ww << endl;
	}
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

//dp[今見ているお宝][それまでに入れたお宝の重さ]
int dp[1024][1024];
int W, N;
int v[1024], w[1024];
char comma;
int maxv, maxw;

int rec(int now, int weight)
{
	if (dp[now][weight] >= 0)
	{
		//printf("dp[%d][%d] = %d\n", now, weight, dp[now][weight]);
		return dp[now][weight];
	}

	int res;
	if (now == N)
	{
		res = 0;
		if (res > maxv)
		{
			maxv = res;
			maxw = weight;
		} else if (res == maxv && maxw > weight) {
			maxw = weight;
		}
	} else if (W - weight < w[now]) {
		res = rec(now + 1, weight);
		if (res > maxv)
		{
			maxv = res;
			maxw = weight;
		} else if (res == maxv && maxw > weight) {
			maxw = weight;
		}
	} else {
		res = max(rec(now + 1, weight), rec(now + 1, weight + w[now]) + v[now]);
		if (res > maxv)
		{
			maxv = res;
			maxw = weight;
		} else if (res == maxv && maxw > weight) {
			maxw = weight;
		}
	}

	//printf("dp[%d][%d] = %d\n", now, weight, res);
	return dp[now][weight] = res;
}

int main()
{
	int cnt = 1;
	while (cin >> W, W)
	{
		cin >> N;
		rep(i, N) cin >> v[i] >> comma >> w[i];

		rep(i, 1024) rep(j, 1024) dp[i][j] = -1;
		maxv = -1;
		maxw = 999999999;

		rec(0, 0);

		printf("Case %d:\n", cnt);
		cout << maxv << endl;
		cout << maxw << endl;

		cnt++;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include <utility>

using namespace std;


int main(){
	int dp[1000];
	
	int W,N;
	int v[1000],w[1000];
	
	int count=0;
	
	while(cin>>W,W){
		cin>>N;
		count++;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&v[i],&w[i]);
		}
		
		for(int j=0;j<W;j++){
			if(j<w[0]-1){
				dp[j]=0;
			}else{
				dp[j]=v[0];
			}
		}
		
		for(int i=1;i<N;i++){
			for(int j=0;j<W;j++){
				if(j<w[i]-1){
					dp[j]=dp[j];
				}else{
					dp[j]=max(dp[j],dp[j-w[i]+1]+v[i]);
				}
				if(i==N-1)cout<<dp[j]<<" ";
			}
		}
		cout<<endl;
		cout<<"Case "<<count<<":"<<endl;
		cout<<dp[W-1]<<endl;
		int k;
		for(k=W;dp[k-2]==dp[k-1];k--);
		cout<<k<<endl;
	}
	
	
	
	
	return 0;
	
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>

using namespace std;

const int INF = 1 << 29;
typedef pair<int, int> P;

int n, w;
int dp[1001][1001];//お宝の番号、重さ
int main(void)
{
	int caseCount = 0;
	while(cin >> w && w)
	{
		cin >> n;
		vector<int> ps(n), ws(n);
		memset(dp, 0, sizeof(dp));
		char c;
		for (int i = 0; i < n; i++)
		{
			cin >> ps[i] >> c >> ws[i];
		}

		if(ws[0] <= w) dp[0][ws[0]] = ps[0];
		for (int i = 1; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				if(j + ws[i] <= w)
					dp[i][j + ws[i]] = max(dp[i - 1][j + ws[i]], dp[i - 1][j] + ps[i]);
				else dp[i][j] = dp[i - 1][j];
			}
		}

		cout << "Case " << ++caseCount << ":" << endl;
		int ansp = 0, answ = INF;
		for (int i = 0; i <= w; i++)
		{
			if(ansp < dp[n - 1][i])
			{
				ansp = dp[n - 1][i];
				answ = i;
			}
		}
		cout << ansp << endl << answ << endl;
	}
	return 0;
}