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

#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)
#define repr(a, b, c) for (int a = b; a > (c); a--)
#define repre(a, b, c) for (int a = b; a >= (c); a--)

using namespace std;

int main() {
    for (int c = 1; ; c++) {
        int W, N;
        cin >> W;
        if (W == 0) break;
        cin >> N;
        vector< vector<long long> > dp(N + 1, vector<long long>(W + 1, 0));
        int v, w;
        char ch;
        rep(i, 0, N) {
            cin >> v >> ch >> w;
            repe(j, 0, W - w) {
                dp[i + 1][j + w] = max(dp[i][j + w], dp[i][j] + v);
            }
        }
        long long ans_w = 0, ans_v = 0;
        repre(i, W, 0) {
            if (ans_v <= dp[N][i]) {
                ans_w = i;
                ans_v = dp[N][i];
            }
        }
        cout << "Case " << c << ":" << endl;
        cout << ans_v << endl;
        cout << ans_w << endl;
    }
    return 0;
}

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

int N , W ;
int v[1000] , w[1000] ;
int dp[1002][10002] ;
int ans1 , ans2 ;

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

int main(){
	
	int t=0 ;
	
	while( cin >> W >> N , W ){
		for( int i=0 ; i<N ; i++ ){
			scanf("%d,%d" ,&v[i] ,&w[i] ) ;
		}t++ ;
		solve() ;
		
		cout << "Case " << t << ':' << endl ;
		cout << ans1 << endl ;
		cout << ans2 << endl ;
	}
	
return 0 ; 
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))

int dp[1001][1001];

int main(){
	int n,W,v[1000],w[1000],c=0,ans,we;
	for(;;){
		scanf("%d",&W);
		if(!W)break;
		scanf("%d",&n);
		ans=0;
		we=1023;
		rep(i,n)scanf("%d,%d",v+i,w+i);
		rep(i,W+1)dp[i][0]=0;
		rep(i,n)dp[0][i]=0;
		rep(i,W+1)rep(j,n){
			if(i>=w[j])dp[i][j+1]=max(dp[i][j],dp[i-w[j]][j]+v[j]);
			else dp[i][j+1]=dp[i][j];
			ans=max(ans,dp[i][j+1]);
		}
		for(int i=0;i<=W&&we==1023;i++)rep(j,n+1)if(dp[i][j]==ans){we=i;break;}
		printf("Case %d:\n%d\n%d\n",++c,ans,we);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>

using namespace std;

const int INF = 1 << 29;
typedef pair<int, int> P;

int n, w;
int dp[1001][1001];//お宝の番号、重さ
int main(void)
{
	int caseCount = 0;
	while(cin >> w && w)
	{
		cin >> n;
		vector<int> ps(n), ws(n);
		memset(dp, 0, sizeof(dp));
		char c;
		for (int i = 0; i < n; i++)
		{
			cin >> ps[i] >> c >> ws[i];
		}

		if(ws[0] <= w) dp[0][ws[0]] = ps[0];
		for (int i = 1; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				if(j + ws[i] <= w)
					dp[i][j + ws[i]] = max(dp[i - 1][j + ws[i]], dp[i - 1][j] + ps[i]);
				else dp[i][j] = max(dp[i - 1][j], dp[i][j]);
			}
		}

		cout << "Case " << ++caseCount << ":" << endl;
		int ansp = 0, answ = INF;
		for (int i = 0; i <= w; i++)
		{
			if(ansp < dp[n - 1][i])
			{
				ansp = dp[n - 1][i];
				answ = i;
			}
		}
		cout << ansp << endl << answ << endl;
	}
	return 0;
}

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

using namespace std;

int w,n;
vector< pair<int,int> > v;

int** GetArray(int w,int h){
	int** pt = new int*[w];
	for(int i=0;i<w;i++){
		pt[i] = new int[h];
		for(int j=0;j<h;j++){
			pt[i][j] = 0;
		}
	}
	return pt;
}

void DeleteArray(int** pt,int w){
	for(int i=0;i<w;i++){
		delete[] pt[i];
	}
	delete[] pt;
}

int main(){
	int setno = 1;
	int a,b;
	while(scanf("%d",&w) && w){
		scanf("%d",&n);
		printf("Case %d:\n",setno);
		v.clear();
		for(int i=0;i<n;i++){
			scanf("%d,%d",&a,&b);
			v.push_back(make_pair(a,b));
		}
		int** dp = GetArray(n,w);
		for(int j=0;j<w;j++){
			//printf("j:%d\n",j);
			dp[0][j] = 0;
		}
		for(int i=1;i<n;i++){
			for(int j=0;j<w;j++){
				//printf("%d:%d\n",i,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);
				}
			}
		}
		int ans = dp[n-1][w-1];
		int answ = -1;
		for(int i=0;i<w;i++){
			if(dp[n-1][i] == ans){
				answ = i;
				break;
			}
		}
		printf("%d\n%d\n",ans,answ);
		DeleteArray(dp,n);
		setno++;
	}
}

C++: #include<stdio.h>
#include<iostream>
#include<list>
#include<map>
#include<vector>
#include<algorithm>
#include<string.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << x << endl;
using namespace std;

int main(){
    int maxWeight, number;
    int C = 0;
    while(cin >> maxWeight >> number && maxWeight){
        int dp[1001] = {};
        rep(i,number){
            int a,b;
            char c;
            cin >> a >> c >> b;
            for(int j = maxWeight; j >= b; j--){
                dp[j] = max(dp[j], dp[j - b] + a);
                /*持てる重さの最大値から、入力された重さまでのループ
                 *入力した宝を持つべきか判断。
                 *持つ場合、持てる重さから宝の重さを引いて、
                 *　その添字の部分に宝の価値を入力する。
                 *　それがdp[j]にはいる。
                 *それを何度も繰り返す。
                 */
            }
        }
        int x = 0, y = 0;
        for(int i = 0; i <= maxWeight; i++){
            if(x < dp[i]){
                x = dp[i];
                y = i;
            }
        }
        cout << "Case " << ++C << ":\n" << x << "\n" << y << "\n";
    }
}