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 <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n;
int va[1005],we[1005];
int dp[1005]={};
#define INF 1000000000
int main(){
	int cou=1;
	while(1){
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		if(!n) break;
		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			scanf("%d,%d",&va[i],&we[i]);
		}
		for(int i=0;i<m;i++){
			for(int j=n;j>=we[i];j--){
				dp[j]=max(dp[j],dp[j-we[i]]+va[i]);
			}
		}
		int rp=0,e;
		for(int i=n;i>=0;i--){
			if(rp<=dp[i]){
				e=i;
				rp=dp[i];
			}
		}
		printf("Case %d:\n",cou);
		cou++;
		printf("%d\n%d\n",rp,e);
	}
}

C++: #include <iostream>
#include <array>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define out(s) cout << (s) << endl;

int dp[1001][1001];

int main()
{
	int W, N, cnt = 1;
	cin >> W;
	while (W!=0)
	{
		cin >> N;
		memset(dp, -1, sizeof(dp));
		dp[0][0] = 0;
		for (size_t i = 0; i < N; i++)
		{
			int vi, wi;
			scanf("%d,%d", &vi, &wi);
			for (size_t n = 0; n <= W; n++)
			{
				if (dp[i][n] != -1) {
					dp[i + 1][n] = max(dp[i][n], dp[i + 1][n]);
					dp[i + 1][n + wi] = max(dp[i][n] + vi, dp[i][n]);
				}
			}
		}
		int dp_max = dp[N][W];
		int index = W;
		for (size_t i = W; i > 0; i--)
		{
			if (dp_max == dp[N][i])index = i;
		}
		cout << "Case " << cnt << ":" << endl << dp_max << endl << index << endl;
		cin >> W;
		cnt++;
	}
	return 0;
}

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

using namespace std;

int W, N, v[1000], w[1000], dp[1001][1001];

int main() {
  for (int I = 1;; I++) {
    scanf("%d\n", &W);
    if (!W) return 0;
    scanf("%d\n", &N);
    for (int i = 0; i < N; i++)
      scanf("%d,%d\n", &v[i], &w[i]);
    for (int j = 0; j <= W; j++)
      dp[N][j] = 0;
    for (int i = N-1; i >= 0; i--)
      for (int j = 0; j <= W; j++)
        if (j >= w[i])
          dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]]+v[i]);
        else
          dp[i][j] = dp[i+1][j];
    int maxv = 0, maxw = 0;
    for (int j = 0; j <= W; j++)
      if (dp[0][j] > maxv) {
        maxv = dp[0][j];
        maxw = j;
      }
    printf("Case %d:\n%d\n%d\n", I, maxv, maxw);
  }
}

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

struct treasure{
	int val;
	bool combi[1000];
};

treasure dp[1001][1001];
int main(){
	int w, n;
	for (int dataset = 1; cin >> w, w; dataset++){
		cin >> n;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < w; j++){
				dp[i][j].val = -1;
				for (int k = 0; k < n; k++){
					dp[i][j].combi[k] = false;
				}
			}
		}
		dp[0][0].val = 0;
		int t_w[1000], t_val[1000], ans_val = 0, ans_w = 0;
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &t_val[i], &t_w[i]);
		}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < w; j++){
				if (dp[i][j].val == -1) continue;
				if (dp[i][j].val > ans_val){
					ans_val = dp[i][j].val;
					ans_w = j;
				}
				for (int k = 0; k < n; k++){
					if (dp[i][j].combi[k] || j + t_w[k] >= w
						|| dp[i + 1][j + t_w[k]].val > dp[i][j].val + t_val[k])
						continue;
					dp[i + 1][j + t_w[k]] = dp[i][j];
					dp[i + 1][j + t_w[k]].val = dp[i][j].val + t_val[k];
					dp[i + 1][j + t_w[k]].combi[k] = true;
				}
			}
		}
		printf("Case %d:\n%d\n%d\n", dataset, ans_val, ans_w);
	}
	return 0;
}

C++: //A Thief
#include <cstdio>
#include <iostream>
#include <string.h>
#include <cstdlib>

using namespace std;

int dp[1000][1000];
int n;
int w[1000], v[1000];
int W;
int weight;

int rec(int i, int j)
{
	if(dp[i][j] >= 0){
		return dp[i][j];
	}
	
	int res;
	if(i == n){
		res = 0;
	}
	else if(j < w[i]){
		res = rec(i + 1, j);
	}
	else{
		res = max(rec(i + 1, j), rec(i + 1, j - w[i]) + v[i]);
	}
	weight = j;
	return dp[i][j] = res;
}

void solve()
{
	memset(dp, -1, sizeof(dp));
	cout << rec(0, W) << endl;
}

int main()
{
	int count = 1;
	while(scanf("%d", &W) != EOF){
		if(W == 0){ break; }
		char c;
		char temp[10000];
		cin >> n;
		for(int i = 0; i < n; i++){
			scanf("%d,%d", &v[i], &w[i]);
		}
		cout << "Case " << count << ":" << endl;
		solve();
		cout << weight - 1 << endl;
		count++;
	}
	
	return 0;
}

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

#define min(a,b) {a > b ? b:a}
#define max(a,b) {a > b ? a:b}
using namespace std;

#define MAX_W 1000
#define MAX_N 1000

int dp[MAX_W*10+1];
void q42() {
	int W, N;
	int cnt = 1;
	for (; cin >> W;) {
		if (!W) break;

		cin >> N;
		vector<pair<int, int> > Item;
		for (int i = 0; i < N; i++) {
			int value, weight;
			char tmp;
			cin >> value >> tmp >> weight;
			Item.push_back(make_pair(value, weight));
		}

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

		for (int i = 0; i < N; i++) {
			int value = Item[i].first;
			int weight = Item[i].second;
			for (int j = W; j >= 0; j--) {
				if (j - weight < 0) continue;
				if (dp[j - weight] == -1) continue;
				dp[j] = max(dp[j], dp[j - weight] + value);
			}
		}

		int ans_value = 0, ans_weight = 0;
		for (int i = 0; i <= W; i++) {
			cout << dp[i] << endl;
			if (ans_value < dp[i]) {
				ans_value = dp[i];
				ans_weight = i;
			}
		}

		cout << "Case " << cnt++ << ":" << endl;
		cout << ans_value << endl;
		cout << ans_weight << endl;
	}
}

int main() {
	q42();
	return 0;
}

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

pair<int, int> hoge(vector< pair<int, int> > &c, int i, int ww, int w, int v)
{
    if (i  == int(c.size()) || ww < w)
        return make_pair(w, v);

    pair<int, int> c1, c2;
    c1 = hoge(c, i+1, ww, w + c[i].first, v + c[i].second);
    c2 = hoge(c, i+1, ww, w, v);

    if (ww < c1.first) {
        return c2;
    }
    else if (c1.second == c2.second) {
        return (c1.first < c2.first)? c1 : c2;
    }
    else {
        return (c1.second < c2.second)? c2 : c1;
    }
}

int main(int argc, char const* argv[])
{
    int ww, nn;
    for(int i = 0;; ++i) {
        vector< pair<int, int> > container;
        cin >> ww;
        if (ww == 0)
            break;
        cin >> nn;
        for (int i = 0; i < nn; ++i) {
            int v, w;
            char c;
            cin >> v >> c >> w;
            container.push_back(make_pair(w, v));
        }
        sort(container.begin(), container.end(), greater< pair<int, int> >());
        pair<int, int> r = hoge(container, 0, ww, 0, 0);
        cout << "Case " << i+1 << ":" << endl;
        cout << r.second << endl;
        cout << r.first  << endl;
    }
    
    return 0;
}