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 <cmath>
#include <vector>

using namespace std;

int main() {
	int W, N;
	char d;
	int counter = 1;
	int takara[1000][2];
	while(1) {
		cin >> W;
		if (W == 0) {
			break;
		}
		cin >> N;
		for (int i = 0; i < N; i++) {
			cin >> takara[i][0] >> d >> takara[i][1];
		}
		vector< vector<int> > dp(W+1, vector<int> (N+1));
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[j][i] = 0;
			}
		}
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				if (i == 0) {
					continue;
				}
				if (j < takara[i-1][1]) {
					dp[j][i] = dp[j][i-1];
				} else {
					dp[j][i] = max(dp[j][i-1], dp[j-takara[i-1][1]][i-1] + takara[i-1][0]);
				}
			}
		}
		int MAX[2];
		MAX[0] = 0;
		MAX[1] = 10000;
		for (int i = 0; i <= W; i++) {
			if (MAX[0] == dp[i][N]) {
				MAX[1] = min(MAX[1],i);
			} else if (MAX[0] < dp[i][N]){
				MAX[0] = dp[i][N];
				MAX[1] = i;
			}
		}
		cout << "Case " << counter << ':' << endl << MAX[0] << endl << MAX[1] << endl;
		counter++;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int N, W, v[1002], w[1002]; // v[i],w[i] := iツ氾板姪堋づ個品ツ閉ィツづ個可ソツ値ツ、ツ重ツつウ
int dp[1002][1002]; // dp[i][j] := iツ氾板姪堋づ個品ツ閉ィツづ慊づ考ツ猟カツつオツつスツ篠楪づ個、ツ重ツつウjツづ可つィツつッツづゥツ催妥・ツづ個可ソツ値
void solve() {
  memset(dp, 0, sizeof dp);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j + w[i] <= W)
        dp[i+1][j+w[i]] = max(dp[i][j+w[i]], max(dp[i][j]+v[i], dp[i+1][j+w[i]]));
      else
        dp[i+1][j+w[i]] = max(dp[i][j+w[i]], dp[i+1][j+w[i]]);
    }
  }
  int *itr = max_element(dp[N], dp[N]+W);
  printf("%d\n%d\n", *itr, itr - dp[N]);
}

int main() {
  int z = 1;
  while (scanf("%d",&W),W) {
    scanf("%d",&N);
    rep(i,N) scanf("%d,%d",v+i,w+i);
    printf("Case %d:\n", z);
    solve();
    ++z;
  }
  return 0; 
}

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

#define  req(i,r,k) for(int i=k;i<r;i++)
#define  rep(i,r) req(i,r,0)

const long long int INF = 100000000000000;

long long int dp[1001][1001];

int main() {

    for(int a=1;1;a++){
        int w,n;
        scanf(" %d",&w);
        if(w==0) break;
        scanf(" %d",&n);
        pair<int,int> t[n];
        char c;
        rep(i,n){
            scanf(" %d%c%d",&t[i].first,&c,&t[i].second);
        }

        rep(i,w){
            dp[0][i] = 0;
            dp[i][0] = 0;
        }

        dp[0][0] = 0;
        rep(i,n) {
            rep(j,w) {
                if(j >= t[i].second) {
                    dp[i+1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first);
                }
                else{
                    dp[i+1][j] = dp[i][j];
                }
            }
        }
        long long int maxvalue=0,thenweight=0;

        rep(i,w+1){
            //cout << dp[i] << " " << i << " " << thenweight << endl;
            if(maxvalue < dp[n][i]){
                maxvalue = dp[n][i];
                thenweight = i;
            }
        }

        printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight);
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
int max();
using namespace std;
int main()
{
    int W,n,w[1001],v[1001];
    int cnt = 1;
    int dp[1024][10] = {{0}};
    for(;;){
        scanf("%d",&W);
        if(W == 0)break;
        scanf("%d",&n);
        for(int i = 1;i <= n; i++)scanf("%d,%d",&v[i],&w[i]);

        for(int j = 1;j <= n; j++){
            for(int i = 0;i <= W; i++){
                if(i < w[j]){
                    dp[i][j] = dp[i][j-1];
                }
                else {
                    dp[i][j] = max(dp[i][j-1],dp[i-w[j]][j-1]+v[j]);
                }
            }
        }
        int x = 0;
        for(int i = 0;i <= W; i++){
            if(dp[i][n] == dp[W][n]){
                x = i;
                break;
            }
        }
        printf("Case %d:\n",cnt);
        printf("%d\n",dp[W][n]);
        printf("%d\n",x);
        cnt++;
    }
    return 0;
}
int max(int a,int b){
    int x;
    x = (a > b) ? a:b;
    return x;
}

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

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

int main(void){
	int W;
	int N;
	int k=1;
	while(1){
		scanf("%d",&W);
		if(W==0)break;
		memset(dp,0,sizeof(dp));
		memset(w,0,sizeof(w));
		memset(d,0,sizeof(d));
		scanf("%d",&N);
		for(int i=0;i<N;i++){
			scanf("%d,%d",&d[i],&w[i]);
		}
		for(int i=0;i<N;i++){
			for(int j=W;j>=0;j--){
				if(j>=w[i]){
					dp[j]=max(dp[j],dp[j-w[i]]+d[i]);
				}
			}
		}
		int aw=0,ad=0;
		for(int j=0;j<=W;j++){
			if(ad<dp[j]){
				aw=j;
				ad=dp[j];
			}
		}
		printf("Case %d:\n",k);
		printf("%d\n",ad);
		printf("%d\n",aw);
		k++;
	}
    return 0;
}

C++: #include <math.h>
#include <utility>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main(){

	int n,w;

	for ( int num = 1;; num++ ) {

		cin >> w;
		if ( w == 0 ) break;
		cin >> n;

		int input1,input2;
		char input3;
		vector<int> value;
		vector<int> weight;
		for ( int i = 0; i < n; i++ ) {
			cin >> input1 >> input3 >> input2;
			value.push_back(input1);
			weight.push_back(input2);
		}

		vector<int> check;
		check.push_back(0);
		for ( int i = 0; i < w-1; i++ ) {
			check.push_back(-1);
		}
		int value_sum = 0;
		int weight_sum = w+1;

		for ( int i = 0; i < n; i++ ) {
			for ( int j = w - weight[i] - 1; j >= 0; j-- ) {
				if ( check[j] != -1 ) {
					check[ j+weight[i] ] = max( check[ j+weight[i] ], check[j] + value[i] );
					if ( check[ j+weight[i] ] > value_sum ) {
						value_sum = check[ j+weight[i] ];
						weight_sum = j+weight[i];
					}else if ( check[ j+weight[i] ] == value_sum ) {
						weight_sum = min( weight_sum, j+weight[i] );
					}
				}
			}
		}
		cout << "Case " << num << ":" << endl << value_sum << endl << weight_sum << endl;
	}
	return 0;
}