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

#pragma warning(disable:4996)

#define MAX_N 1500
#define MAX_W 10000

int dp[MAX_N][MAX_W];
int W, N, v[MAX_N], w[MAX_N], cnt;

int main() {
	while (true) {
		cnt++;
		memset(dp, 0, sizeof(dp));
		cin >> W;
		if (W == 0) { break; }
		cin >> N;
		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++)
			{
				dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
				if (j + w[i] < W)
				{
					dp[i + 1][j + w[i]] = max(dp[i][j] + v[i], dp[i + 1][j + w[i]]);
				}
			}
		}
		cout << "Case " << cnt << ":" << endl;
		int maxn = 0, maxid = 0;
		for (int i = 0; i < MAX_N; i++)
		{
			for (int j = 0; j < MAX_W; j++)
			{
				if (maxn < dp[i][j])
				{
					maxn = dp[i][j];
					maxid = j;
				}
				else if (maxn == dp[i][j])
				{
					maxid = min(maxid, j);
				}
			}
		}
		cout << maxn << endl;
		cout << maxid << endl;
	}
}

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

// üÍ
int W, n;
int v[1001], w[1001];

int dp[1001][1001];
int ans_weight;

void solve(){
	// ¿lÌÅålðßé
	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] );
			}
		}
	}
	// ¿lÌÅåldp[n][W]ÆÈéÅ¬Ìd³Ìaðßé
	for(int j=0 ; j <= W ; j++ ){
		if( dp[n][j] == dp[n][W] ){
			ans_weight = j;
			break;
		}
	}
}

int main(){
	for(int t=1 ; scanf("%d", &W) , W ; t++ ){
		// zñÌú»
		for(int i=0 ; i < 1001 ; i++ ){
			for(int j=0 ; j < 1001 ; j++ ){
				dp[i][j] = 0;
			}
		}

		scanf("%d", &n);
		for(int i=0 ; i < n ; i++ ){
			scanf("%d,%d", &v[i], &w[i]);
		}
		solve();
		printf("Case %d:\n", t);
		printf("%d\n%d\n", dp[n][W], ans_weight );
	}
}

C++: #include <iostream>
#include <utility>

auto main () -> int
{
    int index = 1;
    int w;
    while (std::cin >> w && w)
    {
        int n;
        std::cin >> n;
        int dp [n + 1] [w + 1];
        int values [n];
        int weights [n];
        for (auto i = 0; i < n; ++ i)
        {
            std::scanf ("%d,%d" , values + i , weights + i);
        }
        
        for (auto j = 0; j <= w; ++ j)
        {
            dp [0] [j] = 0;
        }
        for (auto i = 1; i <= n; ++ i)
        {
            for (auto j = w; j >= 0; -- j)
            {
                dp [i] [j] = std::max (dp [i - 1] [j] , j + weights [i - 1] <= w ? dp [i - 1] [j + weights [i - 1]] + values [i - 1] : 0);
            }
        }
        auto res = std::make_pair (0 , 0);
        for (int j = 0; j <= w; ++ j)
        {
            if (res.first < dp [n] [j] || (res.first == dp [n] [j] && res.second > w - j))
            {
                res.first = dp [n] [j];
                res.second = w - j;
            }
        }
        
        std::cout << "Case " << index ++ << ":" << std::endl;
        std::cout << res.first << std::endl << res.second << std::endl;
    }
}

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

C++: #include <stdio.h>
#include <algorithm>
using namespace std;

int table[1005][1005];
int weight[1000];
int value[1000];
int w;
int n;
int Case = 0;

int main()
{
	while(true)
	{
		Case++;
		
		scanf("%d", &w);
		if(w == 0) break;
		scanf("%d", &n);
		
		for(int i = 0; i < n; i++)
		{
			scanf("%d,%d", &value[i], &weight[i]);
		}
		
		for(int i = 0; i <= n; i++)
		{
			for(int j = 0; j <= w; j++)
			{
				table[i][j] = -1;
			}
		}
		
		table[0][0] = 0;
		
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j <= w; j++)
			{
				if(table[i][j] == -1) continue;
				table[i + 1][j] = max(table[i + 1][j], table[i][j]);
				if(j + weight[i] <= w)
				{
					table[i + 1][j + weight[i]] = max(table[i + 1][j + weight[i]], table[i][j] + value[i]);
				}
			}
		}
		
		int ansValue = 0;
		int ansWeight;
		for(int i = 0; i <= w; i++)
		{
			if(ansValue < table[n][i])
			{
				ansValue = table[n][i];
				ansWeight = i;
			}
		}
		
		printf("Case %d:\n", Case);
		printf("%d\n%d\n", ansValue, ansWeight);
	}
	
	return 0;
}