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 <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;
}