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

#define FOR(i, n, m) for(int i = (n);i < (m);i++)
#define REP(i, n) for(int i = 0;i < (n);i++)
#define PII pair<int, int>

typedef long long int lli;

using namespace std;

int n, W;
vector<int> v, w;
int dp[1000][1000+1];

int main(void){
    int cs = 1;
    while(cin >> W && W){
        cin >> n;
        REP(m, n){
            int a, b;
            scanf("%d, %d", &a, &b);
            v.push_back(a);
            w.push_back(b);
        }

        FOR(i, 1, n){
            FOR(j, 0, W+1){
                if(j - w[i] < 0){
                    dp[i][j] = dp[i-1][j];
                }else{
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]);
                }
            }
        }

        cout << "Case " << cs << ":" << endl;
        cout << dp[n-1][W] << endl;
        int min_w = 100000000;
        REP(j, W+1){
            if(dp[n-1][j] == dp[n-1][W])
                min_w = min(min_w, j);
        }
        cout << min_w << endl;
        cs++;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

bool cmp(const pair<int,int> &a,const pair<int,int> &b){
	if( a.first != b.first ) return a.first < b.first;
	else return a.second > b.second;
}
int main(){
	int n,W;
	int t = 1;
	while( cin >> W >> n && W ){
		vector< pair<int,int> > cur;
		cur.push_back({0,0});
		for(int i = 0 ; i < n ; i++){
			int w,c;
			scanf("%d,%d",&c,&w);
			for(int j = 0 , t = cur.size() ; j < t ; j++ )
				cur.push_back(pair<int,int>(cur[j].first+w,cur[j].second+c));
			sort(cur.begin(),cur.end(),cmp);
			int maxi = -1;
			vector< pair<int,int> > nex;
			for(int j = 0 ; j < cur.size() ; j++){
				if( maxi >= cur[j].second ) continue;

				if( cur[j].first <= W ) nex.push_back(cur[j]);
				maxi = cur[j].second;
			}cur = nex;
		}
		int ans = 0, ans2 ;
		for(int i = 0 ; i < cur.size() ; i++){
			if( cur[i].first <= W ){
				ans = cur[i].second;
				ans2 = cur[i].first;
			}
		}

		cout << "Case " << t++ << ":" << endl;
		cout << ans << endl;
		cout << ans2 << endl;
	}
}

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

int w, z[1000][2] = {}, map[1000][1000] = {}, n, max_w = 20000000, max_v = 0;

int main(){
	int k = 1;
	while(cin >> w >> n){
		if (w == 0) break;
		max_w = 20000000;
		max_v = 0;
		for(int i = 0;i<n;i++){
			char cmp;
			cin >> z[i][0] >> cmp >> z[i][1];
		}
		for(int i = 1;i<=n;i++){
			for(int j = 0;j<=w;j++){
				if (z[i - 1][1] > j) continue;
				map[i][j] = max(map[i - 1][j], map[i - 1][j - z[i-1][1]] + z[i - 1][0]);
				if(max_v < map[i][j]){
					max_v = map[i][j];
					max_w = j;
				}
				else if(max_v == map[i][j]){
					if (max_w > j) max_w = j;
				}
			}
		}
		cout << "Case " << k << ":" << endl
			<< max_v << endl
			<< max_w << endl;
		k++;
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
class takara {
public:
	int v, w;
	takara() {}
	takara(int x, int y) :v(x), w(y) {}
	bool operator<(const takara &other) {
		return this->w < other.w;
	}
};
signed main() {
	int W; int x(1);
	while (cin >> W, W) {
		int N; cin >> N;
		vector<takara>list;
		list.push_back(takara(0, 0));
		rep(i, 0, N) {
			int w, v; char c; cin >> v >> c >> w;
			list.push_back(takara(v, w));
		}
		vector<vector<int > >v(N + 1, vector<int>(W + 1, 0));
		rep(i, 1, N + 1) {
			rep(j, 1, W + 1) {
				v[i][j] = v[i - 1][j];
				if (j - list[i].w >= 0) {
					v[i][j] = max(v[i - 1][j], v[i - 1][j - list[i].w] + list[i].v);
				}
			}
		}
		int ret(0);
		rep(i, 0, W + 1) {
			if (v[N][ret] < v[N][i])ret = i;
		}
		cout << "Case " << x++ << ":" << endl;
		cout << v[N][W] << endl << ret << endl;
	}
	return 0;
}

C++: #include<cstdio>

int main(){
	int w,n,count=1;
	scanf("%d",&w);
	while(w){
		scanf("%d", &n);
		int a[n][2];
		for(int i = 0; i < n; i++){
			scanf("%d,%d", &a[i][0], &a[i][1]);
		}
		unsigned long long c = 1 << n,b;
		int max = 0,weight, weightm = 0,sum;
		while(c){
			sum = 0;
			weight = 0;
			b = 1;
			for(int i = 0; i < n; i++){
				if(c & b){
					sum += a[i][0];
					weight += a[i][1];
				}
				if(weight > w){
					break;
				}
				if(sum > max){
					max = sum;
					weightm = weight;
				}
				if(sum == max && weight < weightm){
					weightm = weight;
				}
				b = b << 1;
			}
			c--;
		}
		printf("Case %d:\n%d\n%d\n", count++, max, weightm);
		scanf("%d", &w);
	}

}

C++: #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define INF (1<<21)
int dp[1001][1001];
pair<int,int> solve(vector<int> value , vector<int> weight , int W , int N){
	for(int i=0;i<1001;i++)
		for(int j=0;j<1001;j++)
			dp[i][j] = 0;
	for(int num=1;num<=N;num++){
		for(int w=0;w <= W;w++){
			if(w >= weight[num])dp[num][w] = max( dp[num-1][w] , dp[num-1][w-weight[num]] + value[num]);
			else dp[num][w] = dp[num-1][w];
		}
	}	
	int ans1 = *max_element(dp[N],dp[N]+W+1);
	int ans2 = find(dp[N],dp[N]+W+1,ans1)-dp[N];
	return make_pair(ans1,ans2);
}

int main(){
	int n,w,CaseNum=0;
	while(cin >> w && w){
		cout << "Case " << ++CaseNum << ":" << endl;
		cin >> n;
		vector<int> value(1+n) , weight(1+n);
		for(int i=1;i<=n;i++){
			scanf("%d,%d\n",&value[i],&weight[i]);
		}
		pair<int,int> ans = solve(value,weight,w,n);
		cout << ans.first << endl << ans.second << endl;
	}
}