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 <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++ << ":" <<"\n";
		cout << ans_value << "\n";
		cout << ans_weight << "\n";
	}
}

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

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

using namespace std;

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

int main() {
	int no = 1;
	while (1) {
		scanf("%d", &W);
		if (W == 0)
			break;
		scanf("%d", &N);
		v = new int[N];
		w = new int[N];
		for (int i = 0; i < N; i++)
			scanf("%d,%d", &v[i], &w[i]);

		memset(dp, 0, sizeof(dp));
		for (int i = N - 1; i >= 0; i--) {
			for (int j = 0; j <= W; j++) {
				if (j < w[i])
					dp[i][j] = dp[i+1][j];
				else
					dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]] + v[i]);
			}
		}
		printf("Case %d:\n", no++);
		printf("%d\n", dp[0][W]);
		for (int j = 0; j <= W; j++) {
			if (dp[0][j] == dp[0][W]) {
				printf("%d\n", j);
				break;
			}
		}

		delete[] v;
		delete[] w;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<deque>
#include<algorithm>
#include<functional>
using namespace std;
int n,w;
pair<int,int> tre[1005];

int dp[1005][1005];//現在調べてる宝ID,重さの総和
int solve(){

  for(int i=0;i<n;i++){
    for(int j=0;j<=w;j++){
      if(j<tre[i].second) dp[i+1][j]=dp[i][j];
      else dp[i+1][j]= max(dp[i][j],dp[i][j-tre[i].second] + tre[i].first);
    }
  }

  int temp=dp[n][w];
  int ret=w;
  for(int i=w;i>0;i--){
    if(temp <= dp[n][i]) ret=i;
    else break;
  }
  return ret;

}

main(){
  int cas=1;
  while(1){
    cin >> w;
    if(!w) break;
    cin >> n;
    for(int i=0;i<n;i++) scanf("%d,%d",&tre[i].first,&tre[i].second);
    int res=solve();
    printf("Case %d:\n",cas);
    printf("%d\n%d\n",dp[n][w],res);
    cas++;
  }

}

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

#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)
#define repr(a, b, c) for (int a = b; a > (c); a--)
#define repre(a, b, c) for (int a = b; a >= (c); a--)

using namespace std;

int main() {
    for (int c = 1; ; c++) {
        int W, N;
        cin >> W;
        if (W == 0) break;
        cin >> N;
        vector< vector<int> > dp(N + 1, vector<int>(W + 1, 0));
        int v, w;
        char ch;
        rep(i, 0, N) {
            cin >> v >> ch >> w;
            repe(j, 0, W - w) {
                dp[i + 1][j + w] = max(dp[i][j + w], dp[i][j] + v);
            }
        }
        int ans_w = 0, ans_v = 0;
        repre(i, W, 0) {
            if (ans_v <= dp[N][i]) {
                ans_w = i;
                ans_v = dp[N][i];
            }
        }
        cout << "Case " << c << ":" << endl;
        cout << ans_v << endl;
        cout << ans_w << endl;
    }
    return 0;
}

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