Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

const MaxW = 1000
const MaxN = 1000

type Tuple struct {
	Value  int
	Weight int
}

func nextLine(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func solve(w int, n int, vws []Tuple) Tuple {
	var dp [MaxN + 1][MaxW + 1]int

	for i := 0; i < n; i++ {
		for j:= w; j > 0; j-- {
			if j < vws[i].Weight {
				dp[i + 1][j] = dp[i][j]
			} else {
				dp[i + 1][j] = int(math.Max(float64(dp[i][j]), float64(dp[i][j - vws[i].Weight] + vws[i].Value)))
			}
		}
	}

	maxValue := dp[n][w]
	minWeight := w

	for j := w - 1; j > 0; j-- {
		if maxValue == dp[n][j] {
			minWeight = j
		} else {
			break
		}
	}

	return Tuple{Value: maxValue, Weight: minWeight}
}

func main() {
	var sc = bufio.NewScanner(os.Stdin)
	index := 1
	for {
		var w, n int
		w, _ = strconv.Atoi(nextLine(sc))
		if w == 0 {
			return
		}
		n, _ = strconv.Atoi(nextLine(sc))

		vws := make([]Tuple, n, n)
		for i := 0; i < n; i++ {
			array := strings.Split(nextLine(sc), ",")
			v, _ := strconv.Atoi(array[0])
			w, _ := strconv.Atoi(array[1])
			vws[i] = Tuple{Value: v, Weight: w}
		}

		t := solve(w, n, vws)
		fmt.Printf("Case %d:\n", index)
		fmt.Println(t.Value)
		fmt.Println(t.Weight)
		index++
	}
}

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

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { char c;for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> c >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int w, n;

// index, weight
int dp[1010][1010];
PArray treasure;

int main(void)
{
	ios::sync_with_stdio(false);
	int count = 1;
	while(cin >> w && w)
	{
		n = getInt();
		for (int i = 0; i <= n; i++)
		{
			memset(dp[i], 0, sizeof(dp[i]));
		}
		treasure.resize(n);
		readAll(treasure);
		P ans = P(-1, INF);
		for (int i = 0; i < n; i++)
		{
			int price = treasure[i].first, weight = treasure[i].second;
			for (int j = 0; j <= w; j++)
			{
				int nw = j + weight;
				if(nw <= j)
					dp[i + 1][nw] = max(dp[i][nw], max(dp[i + 1][nw - 1], max(dp[i + 1][nw], dp[i][j] + price)));
				if(j != 0) dp[i + 1][j] = max(dp[i + 1][j], max(dp[i + 1][j - 1], dp[i][j]));
				if(ans.first == dp[i + 1][nw] && ans.second > nw)
					ans = P(dp[i + 1][nw], nw);
				else if(ans.first < dp[i + 1][nw]) ans = P(dp[i + 1][nw], nw);
			}
		}

		cout << "Case " << count << ':' << endl;
		cout << ans.first << endl << ans.second << endl;
		count++;
	}
	return 0;
}

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

int W,N;
int dp[1001][1001]; // 宝の数×重さ
struct Obj {
	int price;
	int weight;
} obj[1001];

int main(void){
	while (cin >> W, W) {
		// input
		cin >> N;
		char buf;
		for (int i = 0; i < N; i++) {
			cin >> obj[i].price >> buf >> obj[i].weight;
		}
		
		// init
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = 0;
			}
		}
		dp[0][obj[0].weight] = obj[0].price;

		// calc
		int wei;
		for (int i = 0; i < N - 1; i++) {
			for (int j = 0; j <= W; j++) {
				if (j != 0 && dp[i][j] == 0) {
					continue;
				}
				// 下
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
				// 右下
				if (wei = j + obj[i + 1].weight, wei <= W) {
					dp[i + 1][wei] = max(dp[i + 1][wei], dp[i][j] + obj[i + 1].price);
				}
			}
		}
		
		struct Obj res = {0, 0};
		for (int i = 0; i <= W; i++) {
			for(int j = 0; j < N; j++) {
				if (res.price < dp[j][i]) {
					res.price = dp[j][i];
					res.weight = i;
				}
			}
		}

		// output
		cout << res.price << endl << res.weight << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define max_n 1000
#define max_w 1000
using namespace std;

int N,W;
long long dp[max_n+1][max_w+1];
long long dw[max_n+1][max_w+1];
int w[max_n],v[max_n];

long long rec(int i,int j){
	int res;
	if(dp[i][j]!=-1)return dp[i][j];
	if(i==N){
		res=0;
		dw[i][j]=0;
	}else if(j<w[i]){
		res=rec(i+1,j);
		dw[i][j]=dw[i+1][j];
	}else{
		long long r1=rec(i+1,j);
		long long r2=rec(i+1,j-w[i])+v[i];
		if(r1>r2){
			res=r1;
			dw[i][j]=dw[i+1][j];
		}else if(r2>r1){
			res=r2;
			dw[i][j]=dw[i+1][j-w[i]]+w[i];
		}else{
			if(dw[i+1][j]>(dw[i+1][j-w[i]]+w[i])){
				res=r2;
				dw[i][j]=dw[i+1][j-w[i]]+w[i];
			}else{
				res=r1;
				dw[i][j]=dw[i+1][j];
			}
		}
	}
	return dp[i][j]=res;
}

int main(){
	int p=1;
	while(cin>>W,W){
		cin>>N;
		rep(i,N){
			scanf("%d,%d",&v[i],&w[i]);
		}
		memset(dp,-1,sizeof(dp));
		memset(dw,-1,sizeof(dw));
		cout<<"Case "<<p<<":"<<endl;
		cout<<rec(0,W)<<endl;
		cout<<dw[0][W]<<endl;
		p++;
		/*cout<<"value="<<rec(0,W)<<" cost="<<dw[0][W]<<endl;//????????§??????????????????
		cout<<"cost="<<dw[0][W]<<endl;*/
	}
	return 0;
}