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
P dp[1010][1010];
PArray treasure;

P dfs(int idx, int weight)
{
	if(idx >= n) return P(0, weight);
	if(dp[idx][weight].first != -1) return dp[idx][weight];

	P ret1 = dfs(idx + 1, weight);
	P ret2 = P(-1, -1);
	if(weight + treasure[idx].second <= w)
	{
		ret2 = dfs(idx + 1, weight + treasure[idx].second);
		ret2.first += treasure[idx].first;
	}
	P ret;
	if(ret1.first == ret2.first)
		ret = P(ret1.first, min(ret1.second, ret2.second));
	else ret = max(ret1, ret2);
	return dp[idx][weight] = ret;
}

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], -1, sizeof(dp[i]));
		}
		treasure.resize(n);
		readAll(treasure);

		P ans = dfs(0, 0);

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

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAX_N = 1000;
const int MAX_W = 1000;
int W, N;
int v[MAX_N];
int w[MAX_N];
int dp[MAX_W + 1];

void solve() {
	fill(dp, dp + W + 1, 0);
	for (int i = 0; i < N; i++) {
		for (int j = W; j >= 0; j--) {
			if (j - w[i] >= 0) {
				dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
			}
		}
	}
	printf("%d\n", dp[W]);
	for (int i = W; i >= 0; i--) {
		if (dp[W] != dp[i]) {
			printf("%d\n", i + 1);
			break;
		}
	}
}

int main () {
	int i = 0;
	while (scanf("%d", &W), W) {
		i++; printf("Case %d:\n", i);
		scanf("%d", &N);
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &v[i], &w[i]);
		}
		solve();
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <cstring>
#include <limits>
#include <cmath>
#include <cfloat>

#define FOR(i,b,n) for(int i=b;i<n;i++)
#define RFOR(i,b,n) for(int i=n-1;i>=b;i--)
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define NCLR(mat) memset(mat, -1, sizeof(mat))

using namespace std;

int w,n;
int val[2000], wg[2000];
int dp[2000]; //[wg] <= val


pair<int, int> solve()
{
	pair<int, int> p = make_pair<int,int>(0, 0);
	CLR(dp);
	
	FOR(j, 0, n)
	{
		RFOR(i, 0, w+1)
		{
			if(( i == 0 || dp[i] != 0)&&( i+wg[j] <= w))
			{
				dp[i+wg[j]] = max(dp[i+wg[j]], dp[i]+val[j]);
			}
		}
	}
	
	FOR(i, 0, w+1)
	{
		if( p.first < dp[i])
		{
			p.first = dp[i];
			p.second = i;
		}
	}
	
	return p;
	
}

int main()
{
	int c = 1;
	pair<int, int> q;
	char p;
	
	while(cin >> w, w)
	{
		cin >> n;
		
		FOR(i, 0, n)
		{
			cin >> val[i] >> p >> wg[i];
		}	
		
		solve();
		
		cout << "Case " << c << ":" << endl;
		c++;
		
		q = solve();
		
		cout << q.first << endl << q.second << endl;
		
	}
	
	return 0;
}

C++: #include <iostream>

using namespace std;

void post_proc(int** a, int N)
{
	if (a != NULL) {
		for (int i=0; i<N; i++) {
			if (a[i] != NULL) delete[] a[i];
		}
		delete[] a;
	}
}

int main()
{
	int W, N;
	int v[1000], w[1000];
	char dummy;
	
	for (int test_num = 1; ; test_num++) {
		cin >> W >> N;
		if (W==0) break;
		for (int i=0; i<N; i++) { cin >> v[i] >> dummy >> w[i]; }
		
		int **dp;
		try {
			dp = new int*[N];
		} catch (bad_alloc) {
			//cout << "bad_alloc : dp" << endl;
			post_proc(dp, N); return 1;
		}
		
		for (int i=0; i<N; i++) {
			try {
				dp[i] = new int[W+1];
			} catch (bad_alloc) {
				//cout << "bad_alloc : dp[" << i << "]" << endl;
				post_proc(dp, N); return 1;
			}
		}
		
		for (int j=0; j<=W; j++) dp[0][j] = -1;
		dp[0][0] = 0;
		dp[0][w[0]] = v[0];
		
		int max_val = v[0];
		int weight_of_max_val = w[0];
		
		for (int i=1; i<N; i++) {
			for (int j=W; j>=0; j--) {
				dp[i][j] = dp[i-1][j];
				if (max_val <= dp[i][j]) {
					max_val = dp[i][j]; weight_of_max_val = j;
				}
				if ( j-w[i] >= 0 && dp[i-1][j-w[i]] >= 0 ) {
					if ( dp[i][j] < dp[i-1][j-w[i]] + v[i] ) {
						dp[i][j] = dp[i-1][j-w[i]] + v[i];
						if (max_val <= dp[i][j]) {
							max_val = dp[i][j]; weight_of_max_val = j;
						}
					}
				}
			}
		}
		
		cout << "Case " << test_num << ":" << endl;
		cout << max_val << endl << weight_of_max_val << endl;
		
		post_proc(dp, N);
	}
	
	return 0;
}