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>
using namespace std;

typedef int integer;
const int NMAX = 1008, WMAX = 10008;

integer weight[NMAX], value[NMAX], dp[NMAX][WMAX];

void init() {
  for (int i = 0; i < NMAX; i++) {
    weight[i] = value[i] = 0;
  }
  for (int i = 0; i < NMAX; i++) {
    for (int j = 0; j < WMAX; j++) {
      dp[i][j] = 0;
    }
  }
}

int main() {
  int W;
  int Case = 0;
  while (cin >> W and W) {
    Case++;
    init();

    int N;
    cin >> N;
    for (int i = 1; i <= N; i++) {
      scanf("%d,%d", &value[i], &weight[i]);
    }
    
    for (int w = 1; w <= W; w++) {
      for (int i = 1; i <= N; i++) {
        if (weight[i] > w) {
          dp[i][w] = dp[i-1][w];
        } else {
          dp[i][w] = max(dp[i-1][w], dp[i-1][w-weight[i]] + value[i]);
        }
      }
    }

    int minW = W, maxV = dp[N][W];
    for (int w = W; w >= 0; w--) {
      if (dp[N][w] == maxV) {
        minW = w;
      }
    }
    cout << "Case " << Case << ":" << endl;
    cout << maxV << endl << minW << endl;
  }
}

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

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define SUPmaxm 1001
#define SUPmaxw 1001

int maxm;
int maxw;
int vs[SUPmaxm];
int wos[SUPmaxm];
int dp[SUPmaxm][SUPmaxw]; //m ika no item tukatte, w ika no omomi de, sum v no max
int rev,rew;

void solve(){
	rev=0;rew=maxw;
	FOR(m,0,maxm+1) FOR(w,0,maxw+1) dp[m][w]=0;
	FOR(m,1,maxm+1){
		FOR(w,0,maxw+1){
			if(w<wos[m]) dp[m][w] = dp[m-1][w];
			else dp[m][w] = max(dp[m-1][w],dp[m-1][w-wos[m]]+vs[m]);
			if(dp[m][w]>rev||(dp[m][w]==rev&&w<rew)){rev=dp[m][w];rew=w;}
		}
	}
}

int main(){
	for(int casei=1;true;++casei){
		scanf("%d",&maxw);			if(!maxw) return 0;
		scanf("%d",&maxm);
		FOR(m,1,maxm+1) scanf("%d,%d",vs+m,wos+m);
		solve();
		printf("Case %d:\n%d\n%d\n",casei,rev,rew);
	}
}

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

int dp[1001], w[1001], v[1001];

int main(){
    int W;
    int N;
    int c = 1;

    while(cin >> W, W != 0){
        fill(dp, dp+W+1, 0);
        cin >> N;
        for(int i=1; i<=N; i++){
            scanf("%d,%d", &v[i], &w[i]);
        }

        for(int i=1; i<=N; i++){
            for(int j=W; j>=1; j--){
                if(j+w[i] <= W){ //ibvUbNÉiÔÚÌi¨ªüêçêé
                    dp[j+w[i]] = max(dp[j+w[i]], dp[j]+v[i]);
                    /* dp[j+w[i]] Íj+w[i]ÜÅÌd³Åìêé¿lÌ(bè)Åål
                     * dp[j]+v[i] ÍjÜÅÌd³Åìêé¿lÌÅålÆiÔÚÌàÌð«µ½Ì
                     * ¿lðvµ½àÌ
                     * å«¢ûªj+w[i]ÜÅÌå«³Åìêé¿lÌbèÅålÉÈé
                     */
                }
            }
        }

        int max_value = 1;
        for(int i=1; i<=W; i++){
            if(dp[i] > dp[max_value]){
                max_value = i;

            }
        }

        cout << "Case " << c << ":" << endl;
        cout << dp[max_value] << endl;
        cout << max_value-1 << endl;
        c++;
    }
    return 0;
}

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

int main(){
    int w, n;
    int count = 0;
    while(cin >> w, w){
        vector<pair<int, int> > v;
        int dp[1000][1000];
        memset(dp, 0, sizeof(dp));
        cin >> n;
        for(int i=0; i < n; i++){int s, t; scanf("%d,%d", &s, &t); v.push_back(make_pair(s, t));}
        for(int i=n-1; 0 <= i; i--){
            for(int j=0; j <= w; j++){
                if(j < v[i].second) dp[i][j] = dp[i+1][j];
                else dp[i][j] = max(dp[i+1][j], dp[i+1][j-v[i].second]+v[i].first);
            }
        }
        cout << "Case " << ++count << ":" << endl;
        cout << dp[0][w] << endl;
        for(int i=w; ; i--){
            if(dp[0][i] != dp[0][w]){cout << i+1 << endl; break;}
        }
    }
}

C++: #include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>

#define INF (1<<21)
#define swap(a,b) {a=a+b; b=a-b; a=a-b;}
#define abs(a) (a > 0 ? a:-a)
#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
#define MAX_VALUE 10000
#define MAX_WEIGHT 1000

int dp[MAX_W + 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 <algorithm>
#include <cstring>
using namespace std;
#define MAX_N 1000
#define MAX_W 1000

int N, W;
int v[MAX_N], w[MAX_N];
int memo[MAX_N][MAX_W];
int resW = 1e9;

int solve(int index, int weight){
  if(memo[index][weight] > 0) return memo[index][weight];

  int res;
  if(index == N) res = 0;
  else if(weight < w[index]) res = solve(index + 1, weight);
  else{
    res = max(solve(index + 1, weight), solve(index + 1, weight - w[index]) + v[index]);
  }
  return memo[index][weight] = res;
}

void askw(int ans, int weight, int index){
  if(ans == 0){
    if(weight < resW) resW = weight;
  }
  if(index == N) return;
  if(ans - v[index] >= 0){
    askw(ans - v[index], weight + w[index], index + 1);
    askw(ans, weight, index + 1);
  }
}

int main(void){
  int cnt = 0, ans;
  while(cin >> W, W){
    cnt++;
    cin >> N;
    for(int i = 0; i < N; i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    memset(memo, -1, sizeof(memo));
    ans = solve(0, W); askw(ans, 0, 0);
    cout << "Case " << cnt << ":" << endl;
    cout << ans << endl;
    cout << resW << endl;
  }
  return 0;
}