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++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using P   = pair<ll, ll>;
using mat = vector<vector<ll>>;
typedef string::const_iterator State;
class ParseError {};

void IOS() { ios::sync_with_stdio(false), cin.tie(0); }

const ll INF = 1e16;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-12;

template <typename T>
void dump(T x) { cout << x << endl; }
void dumpf(ld x, int t) { cout << setprecision(t) << fixed << x << endl; }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
	fill((T *)array, (T *)(array + N), val);
}
inline ll mod(ll a, ll b) {
	if (a > 0) return a % b;
	if (a % b == 0) return 0;
	ll x = -a / b + 1;
	a += x * b;
	return a % b;
}
ll powm(ll a, ll b, ll c) {
	if (b == 0)
		return 1;
	else if (b % 2 == 0) {
		ll d = powm(a, b / 2, c);
		return (d * d) % c;
	} else
		return (a * powm(a, b - 1, c)) % c;
}
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
//ll dx[4] = {1, 0, -1, 0};
//ll dy[4] = {0, 1, 0, -1};

ll dp[1002][1002] = {};
ll a[1002], b[1002];

int main() {
	ll w, n, cnt = 0;
	while (1) {
		cin >> w;
		if (w == 0) break;
		cnt++;
		cin >> n;
		Fill(dp, 0);
		for (int i = 0; i < n; i++) {
			scanf("%d,%d", &a[i], &b[i]);
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= w; j++) {
				dp[i + 1][j] = dp[i][j];
				if (j >= b[i]) dp[i + 1][j] = max(dp[i][j - b[i]] + a[i], dp[i + 1][j]);
			}
		}
		ll worth = dp[n][w];
		ll sum = 0, tmp = 0;
		for (int i = 1; i <= w; i++) {
			if (dp[n][i] > tmp) {
				sum = i;
				tmp = dp[n][i];
			}
		}
		printf("Case %d:\n", cnt);
		printf("%d\n", worth);
		printf("%d\n", sum);
	}

	return 0;
}

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

int dp[1010][1010];

int main() {
	int w;
	int x = 1;
	while (cin >> w, w) {
		int n;
		cin >> n;
		int v[1005], m[1005];
		for (int i = 0;i < n;i++) {	scanf("%d,%d", &v[i], &m[i]);}

		for (int i = 0;i < n;i++)
			for (int j = 0;j < w;j++)dp[i][j] = -1;
		dp[0][0] = 0;

		for (int i = 0;i < n;i++) {
			for (int j = 0;j <= w;j++) {
				if (dp[i][j] !=-1) {
					if(dp[i+1][j]==-1)dp[i + 1][j] = 0;
					if (j + m[i] <= w&&dp[i+1][j+m[i]]==-1)dp[i + 1][j + m[i]] = 0;

					dp[i + 1][j] = dp[i][j];
					dp[i + 1][j + m[i]] = max(dp[i][j] + v[i], dp[i + 1][j + m[i]]);
				}
			}
		}

		int k=0, aw=0;
		for (int i = 0;i <= w;i++) {
			if (dp[n][i] > k) {
				k = dp[n][i];
				aw = i;
			}
		}
		cout << "Case " << x << ":" << endl;
		cout << k << endl << aw << endl;
		x++;
	}
	return 0;
}

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

using namespace std;

typedef pair<int,int> ip;

int solve(int W, int N, vector<int>& v, vector<int>& w){
    vector< vector<ip> > dp(N+1, vector<ip>(W+1));
    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 {
                int a = dp[i+1][j].first;
                int b = dp[i+1][j-w[i]].first + v[i];
                if(a >= b) dp[i][j] = dp[i+1][j];
                else {
                    dp[i][j] = make_pair(b, dp[i+1][j-w[i]].second + w[i]);
                }
            }
        }
    }
    cout << dp[0][W].first << endl;
    cout << dp[0][W].second << endl;
}

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

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cctype>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdio>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;




int main(){


    int W,N;
    char c;
    int times = 1;

    while(cin >> W){

        
        if(W == 0){
            return 0;
        }
        cin >> N;
        vector<int> v(N),w(N);
    
        REP(i,N){
            cin >> v[i] >> c >> w[i];
            //cout << v[i] << " " << w[i] << endl;
        }

        int dp[1010][1010];

        REP(i,1010){
            REP(j,1010){
                dp[i][j] = -INF;
            }
        }
        REP(i,1010){
            dp[0][i] = 0;
        }
        
        for(int i = 1;i <= N;i++){
            for(int j = 0;j <= W;j++){

                if(j >= w[i-1]){
                    dp[i][j] = max(dp[i-1][j],dp[i-1][j - w[i-1]] + v[i-1]  );
                }else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        int weight = 0;
        int value = 0;
        for(int i = 0;i <= W;i++){

            if(weight < dp[N][i]){
                weight = i-1;
                value = dp[N][i]; 
            }
        }

        printf("Case %d:\n%d\n%d\n",times,value,weight);


        times++;

    }
    


    return 0;

}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fr first
#define sc second
using namespace std;

int w,n;
typedef pair<int,int> P;
P dp[1001][1001];
bool chg[1001][1001];
vector <P> dat;

P rec(int idx,int wi){
    if(idx == n){
        return P(0,wi);
    }
    if(chg[idx][wi]) return dp[idx][wi];
    P p;
    if(wi - dat[idx].sc >= 0){
        p = rec(idx+1,wi-dat[idx].sc);
        p.first += dat[idx].fr;
        p = max(p,rec(idx+1,wi));
    }
    else p = rec(idx+1,wi);
    chg[idx][wi] = true;
    return dp[idx][wi] = p;
}

int main(){
    while(cin >> w,w){
        int a = 0;
        memset(dp,-1,sizeof(dp));
        memset(chg,false,sizeof(chg));
        cin >> n;
        for(int i=0;i<n;i++){
            int t,m; scanf("%d,%d",&t,&m); dat.push_back(P(t,m));
        }
        P p = rec(0,w);
        cout << "Case " << ++a << ":\n" << p.fr << endl << w - p.sc << endl;
    }
}