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 <string>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define rep(i,b) for(auto i=0;i<(b);i++)
#define pb push_back
#define sz(c) int((c).size())

bool piicomp(const pii& a, const pii& b)
{
  if (a.first < b.first)
    return true;
  else if (a.first > b.first)
    return false;
  else {
    if (a.second > b.second)
      return true;
    else
      return false;
  }
}

typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
pii rec(vvpii& memo, int capacity, pii cur, int i, vi& value, vi& weight)
{
  pii res;
  if (i >= sz(value))
    return cur;
  int j = capacity - cur.second;
  res = memo[j][i];
  if (res.first != -1)
    return res;
  if (cur.second + weight[i] > capacity)
    return memo[j][i] = rec(memo, capacity, cur, i+1, value, weight);
  return memo[j][i] =
    max(rec(memo, capacity, pii(cur.first+value[i], cur.second+weight[i]), i+1, value, weight),
        rec(memo, capacity, cur, i+1, value, weight),
        piicomp);
}

void thief(int capacity, vi& value, vi& weight)
{
  vvpii memo(capacity+1, vpii(sz(value), pii(-1,-1)));
  pii res = rec(memo, capacity, pii(0,0), 0, value, weight);
  cout << res.first << endl;
  cout << res.second << endl;
}

int main(int argc, char **argv)
{
  int casenr = 0;
  for (;;) {
    int W, N;
    cin >> W;
    if (!W)
      break;
    cin >> N;
    vi value(N), weight(N);
    rep (i, N) {
      char c;
      cin >> value[i] >> c >> weight[i];
    }
    cout << "Case " << ++casenr << ":" << endl;
    thief(W, value, weight);
  }
  return 0;
}

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

using namespace std;

#define price first
#define weight second

int main(){
	int W;
	int cnt = 1;
	while (cin >> W, W) {
		int N; cin >> N;
		vector<pair<int, int> > tres(N);
		vector<int> dp(W+1);
		for (int i = 0; i < N; i++) { scanf("%d,%d", &tres[i].price, &tres[i].weight);}
		for (int i = 0; i < W+1; i++) { dp[i] = -1; }
		dp[0] = 0;

		for (int i = 0; i < N; i++) {
			for (int j = W; j >= 0; j--) {
				if (dp[j] == -1) { continue; }
				if (j+tres[i].weight <= W) {
					if (dp[j+tres[i].weight] < dp[j]+tres[i].price) {
						dp[j+tres[i].weight] = dp[j]+tres[i].price;
					}
				}
			}
		}
		int PRICE = 0;
		int WEIGHT = 0;
		for (int i = W; i >= 0; i--) {
			if (PRICE <= dp[i]) {
				PRICE = max(PRICE, dp[i]);
				WEIGHT = i;
			}
		}

		cout << "Case " << cnt << ":" << endl;
		cout << PRICE << endl;
		cout << WEIGHT-1 << endl;
		cnt++;
	}

	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <utility>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int W,N;
const int MAX_N = 1001;
const int MAX_W = 1001;
int v[MAX_N];
int w[MAX_N];
int dp[MAX_N][MAX_W];

void solve() {
	memset(dp, 0, sizeof(dp));
	repa(i,1,N) {
		repa(j,0,W) {
			if(j - w[i] >= 0) {
				dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]);
			}
		}
	}

	int maxval = -1;
	int weight = 0;
	repa(i,0,W) {
		if(maxval < dp[N][i]) {
			maxval = dp[N][i];
			weight = i;
		}
	}
	cout <<	maxval << endl;
	cout << weight << endl;
}

int main() {
	int c = 0;
	while(cin>>W, W) {
		cin >> N;
		rep(i,N) {
			scanf("%d,%d", &v[i], &w[i]);
		}

		printf("Case %d:\n", ++c);
		solve();
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <math.h>
#include <map>
#include <queue>
#include <string>
using namespace std;

int W,N,v[1000],w[1000],dp[1001][1001];
char c;

int main(){
	int cn=0;
	while(cin>>W){
		if(W==0)return 0;
		cn++;
		cin>>N;
		for(int i=0;i<N;i++)cin>>v[i]>>c>>w[i];
		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]);
			}
		}
		int V=dp[N][W],w2=W;
		while(dp[N][w2]==V)w2--;
		cout<<"Case "<<cn<<":"<<endl<<dp[N][W]<<endl<<w2+1<<endl;
	}
}

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

using namespace std;

typedef pair<int, int> P;

P plus_(P a, P b) {
    return P(a.first+b.first, a.second+b.second);
}

int main() {
    int w, n;
    int value[1000], weight[1000];
    P dp[1000][1000];
    for (int case_num=1; true; case_num++) {
        cin >> w;
        if (w == 0) {
            return 0;
        }
        cin >> n;
        for (int i=0; i<n; i++) {
            cin >> value[i];
            cin.ignore(1);
            cin >> weight[i];
        }
        for (int i=0; i<=w; i++) {
            dp[0][i] = P(0, 0);
        }
        for (int i=1; i<=n; i++) {
            for (int j=0; j<=w; j++) {
                if (j < weight[i-1]) {
                    dp[i][j] = dp[i-1][j];
                }
                else {
                    P a = plus_(dp[i-1][j-weight[i-1]], P(value[i-1], -weight[i-1]));
                    P b = dp[i-1][j];
                    dp[i][j] = max(a, b);
                }
            }
        }
        cout << "Case " << case_num << ":" << endl;
        cout << dp[n][w].first << endl;
        cout << -dp[n][w].second << endl;
    }
}

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

int a[1001] = { 0 };
int b[1001] = { 0 };
int w, n, mv, mw;
void solve(int W, int f, int sum) {
	f = min(f, n) + 1;
	for (int i = f; i < n; i++) {
		if (W - b[i] < 0) {
			if (sum == mv) {
				mw = min(w - W, mw);
				return;
			}
			mw = max(w - W, mw);
			mv = max(sum, mv);
			return;
		}
		solve(W - b[i], f, sum + a[i]);
	}
}

int main() {
	int u = 0;
	while (scanf("%d", &w)) {
		if (w == 0) break;
		scanf("%d", &n);

		mv = 0;
		mw = w;
		u++;
		for (int i = 0; i < n; i++) {
			scanf("%d,%d", &a[i], &b[i]);
		}

		solve(w, -1, 0);
		printf("Case %d:\n%d\n%d\n", u, mv, mw);
	}
	return 0;
}