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 <bits/stdc++.h>

#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VB vector<bool>
#define VL vector<long long>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

const std::string YES = "YES";
const std::string Yes = "Yes";
const std::string NO = "NO";
const std::string No = "No";

bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }

// 4近傍（右, 下, 左, 上）
const std::vector<int> dy = { 0, -1, 0, 1 };
const std::vector<int> dx = { 1, 0, -1, 0 };

using namespace std;


void solve(int W, int N, VI &v, VI &w) {
    VVI dp(N + 1, VI(W + 1, -1));
    dp[0][0] = 0;
    FOR(i, 0, N) {
        int value = v[i];
        int weight = w[i];

        FOR(j, 0, dp[0].size()) {

            if (dp[i][j] == -1) { continue; }

            // not use
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);

            // use
            if (j + weight < dp[0].size()) {
                dp[i + 1][j + weight] = max(dp[i + 1][j + weight], dp[i][j] + value);
            }
        }
    }

    int ans_value = -1;
    int ans_weight = -1;
    FOR(j, 0, W + 1) {
        if (dp[N][j] > ans_value) {
            ans_value = dp[N][j];
            ans_weight = j;
        }
    }
    cout << ans_value << endl;
    cout << ans_weight << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int no = 1;
    while (true) {
        int W, N;
        cin >> W;
        if (W == 0) { break; }
        cin >> N;
        vector<int> v(N), w(N);
        FOR(i, 0, N) {
            char dummy;
            cin >> v[i];
            cin >> dummy;
            cin >> w[i];
        }
        cout << "Case " << no++ << ":" << endl;
        solve(W, N, v, w);
    }
    return 0;
}

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

#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;

int main(){
	int c = 1;
	int w;
	while(cin >> w && w){
		int n;
		int tr[1024][2];
		// 価値を最大に
		// 重さ合計 価値
		int dp[1024];
		
		cin >> n;
		rep(i, n){
			scanf("%d, %d", &tr[i][0], &tr[i][1]);
		}

		rep(i, w){
			dp[i] = -1;
		}
		dp[0] = 0;
		
		rep(i, n){
			for(int j = w; j >= 0; j--){
				if(dp[j] != -1){
					int we = j + tr[i][1];
					if(we <= w){
						dp[we] = max(dp[we], dp[j] + tr[i][0]);
					}
				}
			}
		}
	
		int mv,mw;
		mv = 0; mw = 0;
		rep(i, w){
				if(dp[i] > mv){
					mv = dp[i];
					mw = i;
				}
		}

		cout << "Case " << c << ':' << endl;
		cout << mv << endl;
		cout << mw << endl;

		c++;
	}
	return 0;
}

C++: #include<stdio.h>
int i,j,l;
int w;
int n;
int kati,omosa;
long long int maxkati,maxomosa,casecnt=0;
long long int dp[1005][1005];//dp[お宝の総数][お宝の重さ]
int main(void){
	scanf("%d",&w);
	while(w!=0){
		for(i=0;i<=1000;i++){
			for(j=0;j<=1000;j++){
				dp[i][j]=0;
			}
		}
		scanf("%d",&n);
		scanf("%d,%d",&kati,&omosa);
		dp[0][omosa]=kati;
		for(j=1;j<=n;j++){
			dp[j][omosa]=dp[0][omosa];
		}
		for(i=1;i<n;i++){
			scanf("%d,%d",&kati,&omosa);
			for(j=i;j<=n;j++){
				if(dp[j][omosa]<kati){
					dp[j][omosa]=kati;
				}
			}
			for(j=1;j<=w;j++){
				if(dp[i-1][j]>0){
					if(dp[i][j+omosa]<dp[i-1][j]+kati){	
						dp[i][j+omosa]=dp[i-1][j]+kati;
						for(l=i;l<=n;l++){
							dp[l][j+omosa]=dp[i][j+omosa];
						}
					}
				}
			}
			for(j=i;j<=w;j++){
				if(dp[i][j+omosa]>dp[j+1][j+omosa]){
					dp[j+1][j+omosa]=dp[i][j+omosa];
				}
			}
		}
		/*for(i=0;i<=n;i++){
			for(j=1;j<=w;j++){
				printf("%lld ",dp[i][j]);
			}
			printf("\n");
		}*/
		maxomosa=0;
		maxkati=0;
		for(j=0;j<=w;j++){
			if(maxkati==dp[n][j]);
			else if(maxkati<dp[n][j] ){
				maxkati=dp[n][j];
				maxomosa=j;
			}
		}
		casecnt++;
		printf("case %lld:\n",casecnt);
		printf("%lld\n",maxkati);
		printf("%lld\n",maxomosa);
		scanf("%d",&w);
	}
	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 ) 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;
}

C++: #include<iostream>

using namespace std;

const int MAX_W = 1001;
const int MAX_T = 1001;

int dp[MAX_T][MAX_W];

void init(){
  for(int i = 0; i < MAX_T; i++){
    fill(dp[i],dp[i]+MAX_W,0);
  }
}

int main(){
  int rev = 0, max;
  while(cin >> max && max){
    rev++;
    init();
    int num;
    cin >> num;
    for(int i = 0; i < num; i++){
      char trash;
      int price, weight;
      cin >> price >> trash >> weight;
      for(int j = 1; j <= max; j++){
	if(weight <= j){
	  dp[i][j] = price;
	  if(i)dp[i][j]+= dp[i-1][j-weight];
	}
      }
    }
    int ablep = 0, ablew = 10000;
    for(int i = 1; i < num; i++){
      for(int j = 1; j < max; j++){
	if(ablep < dp[i][j]){
	  ablep = dp[i][j];
	  ablew = j;
	}
      }
    }
    cout << "Case " << rev << ":" << endl;
    cout << ablep << endl << ablew << endl;

  }
  return 0;
}