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

int main(void) 
{
  int dp[1001][1001];  // [takara][weight]
  int wei[1001];       // [takara]
  int val[1001];       // [takara]
  int i, w;
  int n, tw;
  int datcnt;
  char c;

  cin >> tw;
  for(datcnt = 1; tw != 0; datcnt++){
    cin >> n;
    for(i = 1; i <= n; i++){
      cin >> val[i];
      cin >> c;
      cin >> wei[i]; 
    }

    // initArrayDP
    for(i = 0; i <= n; i++) {
      for(w = 0; w <= tw; w++) {
	dp[i][w] = 0;
      }
    }
    //cout << "--map";
    for(i = 1; i <= n; i++) {
      for(w = 1; w <= tw; w++) {
	int tem = w - wei[i];
	if(tem < 0) {
	  dp[i][w] = dp[i-1][w];
	} else {
	  dp[i][w] = max(dp[i-1][w], dp[i-1][tem] + val[i]);
	}
      }
    }

    //for(i = 1; i <= n; i++) {
    //  for(w = 1; w <= tw; w++) {
    //	printf("%5d", dp[i][w]);
    //  }
    //  printf("\n");
    //}

    
    // [takara]の最終列から価値最大かつ、そのなかで重さ最初を探す。
    int maxval  = INT_MIN;
    int lastwei = INT_MAX;

    for(w = 0; w <= tw; w++)
      maxval = max(dp[n][w], maxval);

    for(w = tw; w >= 0; w--)
      if(dp[n][w] == maxval) lastwei = min(lastwei, w);

    cout << "Case " << datcnt << ":" << endl;
    cout << maxval << endl << lastwei << endl;
    
    cin >> tw;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;

int main(){
	int W,N,cou=0;
	int tr[1001],we[1001];
	while(cin >> W , W){
		cou++;
		cin >> N;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		int dp[1010];
		for(int i=0;i<=W;i++){
			dp[i]= 0;
		}
		for(int i=0;i<N;i++){
			for(int j=W;j>=we[i];j--){
				dp[j] = max(dp[j],dp[j-we[i]]+tr[i]);
			}
		}
		int ret = 0;
		for(int i=0;i<=W;i++){
			if(dp[i] > dp[ret]) ret = i;
		}
		cout << "Case " << cou << ":" << endl;
		cout << dp[ret] << endl;
		cout << ret << endl;
	}
}

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

int n, w, maxValue, maxWeight;
const int MAX_N = 1000;
int treasures[MAX_N][2];

void dfs(int index, int value, int weight) {
    if (index == n) {
        if (maxValue < value) {
            maxValue = value;
            maxWeight = weight;
        }
        return;
    }
    if (weight + treasures[index][1] < w) {
        dfs(index + 1, value + treasures[index][0], weight + treasures[index][1]);
    }
    dfs(index + 1, value, weight);
}

int main() {
    int t = 0;
    while (true) {
        cin >> w >> n;
        if (w == 0) {
            break;
        }
        memset(treasures, 0, sizeof(treasures));
        for (int i = 0; i < n; i++) {
            scanf("%d,%d", treasures[i], treasures[i] + 1);
        }
        maxValue = 0;
        dfs(0, 0, 0);
        cout << "Case " << ++t << ":" << endl;
        cout << maxValue << endl << maxWeight << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int v[1000];
int w[1000];

int dp[1010][1010];

int main() {
	int W, N;
	int cnt = 1;
	while(scanf("%d", &W), W) {
		scanf("%d", &N);
		for(int i = 0; i < N; i++) {
			scanf("%d,%d", v + i, w + i);
		}

		fill((int*)begin(dp), (int*)end(dp), 0);
		for(int i = 0; i < N; i++) {
			for(int j = 0; j <= W; j++) {
				if(j + w[i] > W) continue;
				dp[i + 1][j + w[i]] = max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
			}
		}

		printf("Case %d:\n", cnt);
		int max_v = *max_element(dp[N], dp[N] + W + 1);
		for(int j = 0; j <= W; j++) {
			if(dp[N][j] == max_v) {
				printf("%d\n%d\n", max_v, j);
				break;
			}
		}
		cnt++;
	}
}

C++: /*#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main(){
	
	return 0;
}*/
/*
1
y eazqyp pnop pngtg ye obmpngt xmybp mr lygw
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
int dp[1001][1001];
 
int main(){
    int W, cnt=1;
    while( cin >> W, W ){
        int n, v[1000], w[1000];
        memset( dp, -1, sizeof(dp) );
        cin >> n;
        rep(i,n){
            scanf("%d,%d", &v[i], &w[i]);
        }
        //dp[0][0] = 0;
    	
        
    	rep(i,n){
            rep(j,W+1){
                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]+1 );
            }
        }
        int mw = 0;
        rep(i,W+1){
            if( dp[n][i]>dp[n][mw] ) mw = i;
        }
    	
    	/*for(int i = 0;i < n;i++){
    		for(int j = 0;j < W+1;j++){
    			cout << dp[i][j] << " ";
    		}
    		cout << endl;
    	}*/
    	
        cout << "Case " << cnt << ":" << endl;
        cnt++;
        cout << dp[n][mw] << endl;
        cout << mw << endl;
    }
	return 0;
}
/*
20
5
60,5
100,10
120,15
210,20
10,2
*/

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  for(int c=1,w; cin>>w,w; c++){
    int n,tr[1000][2] = {0};
    cin >>n;
    for(int i=0; i<n; i++){
      scanf("%d,%d",&tr[i][0],&tr[i][1]);
    }
    int dp[1001][1001] = {0};
    for(int i=0; i<n; i++){
      for(int j=0; j<=w; j++){
	if(j>=tr[i][1]){dp[j][i] = max(dp[j][i-1],dp[j-tr[i][1]][i-1]+tr[i][0]);}
      }
    }
    int pr = -1, val;
    for(int j=0; j<=w; j++){
      for(int i=0; i<n; i++){
	if(pr<dp[j][i]){pr = dp[j][i];val = j;}
      }
    }
    cout <<"Case "<<c<<":"<<endl;
    cout <<pr<<endl<<val<<endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

int W, N, value, weight, num = 1;
int v[1000], w[1000];
int dp[1000][1000]; //[n][w]
	
int main(){
	while(true){
		scanf("%d", &W);
		if(W == 0) break;
		scanf("%d", &N);
		for(int i = 0; i < N; i++){
			scanf("%d,%d", &v[i], & w[i]);
		}
		for(int i = 0; i < 1000; i++){
			for(int j = 0; j < 1000; j++){
				dp[i][j] = 0;
			}
		}
		
		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 dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
			}
		}
		/*for(int i = 0; i < N; i++){
				cout << v[i] << " " << w[i] << endl;
		}
		for(int i = 0; i <= N; i++){
			for(int j = 0; j < 109; j++){
				cout << dp[i][j] << " " ;
			}
			cout << endl;
		}*/
		value = 0;
		weight = 0;
		for(int j = 0; j < W; j++){
			if(value < dp[0][j]){
				value = dp[0][j];
				weight = j;
			}
		}
		cout << "Case " << num << ":" << endl;
		cout << value << endl << weight << endl;
		num++;
	}
	return 0;
}