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 <cstdio>
#include <algorithm>
using namespace std;

int a[1001] = { 0 };
int b[1001] = { 0 };
int w, n, mv, mw;
void solve(int W, int f, int sum) {
	f = min(f, n) + 1;
	for (int i = f; i < n; i++) {
		if (W - b[i] <= 0) {
			mw = max(w - W, mw);
			mv = max(sum, mv);
			return;
		}
		solve(W - b[i], f, sum + a[i]);
	}
}

int main() {
	int u = 0;
	while (scanf("%d", &w)) {
		if (w == 0) break;
		scanf("%d", &n);

		mv = mw = 0; u++;
		for (int i = 0; i < n; i++) {
			scanf("%d,%d", &a[i], &b[i]);
		}

		solve(w, -1, 0);
		printf("Case %d:\n%d\n%d\n", u, mv, mw);
	}
	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>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <istream>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <climits>
using namespace std;
 
typedef ostringstream OSS;
typedef istringstream ISS;
 
typedef vector<int> VI;
typedef vector< VI > VVI;
 
typedef long long LL;
 
typedef pair<int, int> PII;
typedef vector<PII> VPII;
 
#define X first
#define Y second
 
int main(void) {
	int dataset_index = 1;

	int W, N;
	while (cin >> W >> N, W) {
		VI ps(N), ws(N);

		char dummy;
		for (int i = 0; i < N; i++) {
			cin >> ps[i] >> dummy >> ws[i];
		}

		VVI dp(N + 1, VI(W + 1, -1));
		dp[0][0] = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < W + 1; j++) {
				if (dp[i][j] == -1) continue;
				
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

				int w = j + ws[i];
				int p = dp[i][j] + ps[i];
				if (w <= W) {
					dp[i + 1][w] = max(dp[i + 1][w], p);
				}
			}
		}

		int ans_p = 0, ans_w = 0;

		for (int i = 0; i < W + 1; i++) {
			if (dp[N][i] > ans_p) {
				ans_p = dp[N][i];
				ans_w = i;
			}
		}

		cout << "Case " << dataset_index << ":" << endl;
		cout << ans_p << endl << ans_w << endl;

		++dataset_index;
	}
}

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

using namespace std;

int main(){
    int N,W;
    int v[1000],w[1000];

    while(cin>>W){
        if(W==0) break;

        cin>>N;
        int dp[N+1][W+1];

        for(int i=0;i<=W;i++){
            dp[0][i]=0;
        }

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

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

        cout<<dp[N][W]<<endl;

        int max_w=W;
        while(dp[N][W]==dp[N][max_w]) max_w--;

        cout<<max_w+1<<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 <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 dfs(int idx, int weight)
{
	if(weight > w || idx >= n)
		return 0;
	if(dp[idx][weight] != -1) return dp[idx][weight];

	int ret = 0;
	ret = dfs(idx + 1, weight);
	ret = max(ret, dfs(idx + 1, weight + treasure[idx].second) + treasure[idx].first);
	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 = P(-1, INF);
		dfs(0, 0);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				if(dp[i][j] == -1) continue;
				if(ans.first == dp[i][j] && ans.second > j)
					ans = P(dp[i][j], j);
				if(ans.first > dp[i][j])
					ans = P(dp[i][j], j);
			}
		}

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