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 <cstdio>

#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;

int main(){
	int c = 1;
	int w;
	while(cin >> w && w){
		int n;
		int tr[1024][2];
		// 価値を最大に
		// 重さ合計 価値
		int dp[1024];
		
		cin >> n;
		rep(i, n){
			scanf("%d, %d", &tr[i][0], &tr[i][1]);
		}

		rep(i, w){
			dp[i] = -1;
		}
		dp[0] = 0;
		
		rep(i, n){
			for(int j = w; j >= 0; j--){
				if(dp[j] != -1){
					int we = j + tr[i][1];
					if(we <= w){
						dp[we] = max(dp[we], dp[j] + tr[i][0]);
					}
				}
			}
		}
	
		int mv,mw;
		mv = 0; mw = 0;
		rep(i, w){
				if(dp[i] > mv){
					mv = dp[i];
					mw = i;
				}
		}

		cout << "Case " << c << ':' << endl;
		cout << mv << endl;
		cout << mw << endl;

		c++;
	}
	return 0;
}

C++: #include<stdio.h>
int i,j,l;
int w;
int n;
int kati,omosa;
long long int maxkati,maxomosa,casecnt=0;
long long int dp[1005][1005];//dp[お宝の総数][お宝の重さ]
int main(void){
	scanf("%d",&w);
	while(w!=0){
		for(i=0;i<=1000;i++){
			for(j=0;j<=1000;j++){
				dp[i][j]=0;
			}
		}
		scanf("%d",&n);
		scanf("%d,%d",&kati,&omosa);
		dp[0][omosa]=kati;
		for(j=1;j<=n;j++){
			dp[j][omosa]=dp[0][omosa];
		}
		for(i=1;i<n;i++){
			scanf("%d,%d",&kati,&omosa);
			for(j=i;j<=n;j++){
				if(dp[j][omosa]<kati){
					dp[j][omosa]=kati;
				}
			}
			for(j=1;j<=w;j++){
				if(dp[i-1][j]>0){
					if(dp[i][j+omosa]<dp[i-1][j]+kati){	
						dp[i][j+omosa]=dp[i-1][j]+kati;
						for(l=i;l<=n;l++){
							dp[l][j+omosa]=dp[i][j+omosa];
						}
					}
				}
			}
			for(j=i;j<=w;j++){
				if(dp[i][j+omosa]>dp[j+1][j+omosa]){
					dp[j+1][j+omosa]=dp[i][j+omosa];
				}
			}
		}
		/*for(i=0;i<=n;i++){
			for(j=1;j<=w;j++){
				printf("%lld ",dp[i][j]);
			}
			printf("\n");
		}*/
		maxomosa=0;
		maxkati=0;
		for(j=0;j<=w;j++){
			if(maxkati==dp[n][j]);
			else if(maxkati<dp[n][j] ){
				maxkati=dp[n][j];
				maxomosa=j;
			}
		}
		casecnt++;
		printf("case %lld:\n",casecnt);
		printf("%lld\n",maxkati);
		printf("%lld\n",maxomosa);
		scanf("%d",&w);
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

void post_proc(int** a, int N)
{
	if (a != NULL) {
		for (int i=0; i<N; i++) {
			if (a[i] != NULL) delete[] a[i];
		}
		delete[] a;
	}
}

int main()
{
	int W, N;
	int v[1000], w[1000];
	char dummy;
	
	for (int test_num = 1; ; test_num++) {
		cin >> W >> N;
		if (W==0) break;
		for (int i=0; i<N; i++) { cin >> v[i] >> dummy >> w[i]; }
		
		int **dp;
		try {
			dp = new int*[N];
		} catch (bad_alloc) {
			//cout << "bad_alloc : dp" << endl;
			post_proc(dp, N); return 1;
		}
		
		for (int i=0; i<N; i++) {
			try {
				dp[i] = new int[W+1];
			} catch (bad_alloc) {
				//cout << "bad_alloc : dp[" << i << "]" << endl;
				post_proc(dp, N); return 1;
			}
		}
		
		for (int j=0; j<=W; j++) dp[0][j] = -1;
		dp[0][0] = 0;
		dp[0][w[0]] = v[0];
		
		int max_val = v[0];
		int weight_of_max_val = w[0];
		
		for (int i=1; i<N; i++) {
			for (int j=W; j>=0; j--) {
				dp[i][j] = dp[i-1][j];
				if (max_val <= dp[i][j]) {
					max_val = dp[i][j]; weight_of_max_val = j;
				}
				if ( j-w[i] >= 0 && dp[i-1][j-w[i]] >= 0 ) dp[i][j] = dp[i-1][j-w[i]] + v[i];
				if (max_val <= dp[i][j]) {
					max_val = dp[i][j]; weight_of_max_val = j;
				}
			}
		}
		
		cout << "Case " << test_num << ":" << endl;
		cout << max_val << endl << weight_of_max_val << endl;
		
		post_proc(dp, N);
	}
	
	return 0;
}

C++: #include<iostream>

using namespace std;

const int MAX_W = 1001;
const int MAX_T = 1001;

int dp[MAX_T][MAX_W];

void init(){
  for(int i = 0; i < MAX_T; i++){
    fill(dp[i],dp[i]+MAX_W,0);
  }
}

int main(){
  int rev = 0, max;
  while(cin >> max && max){
    rev++;
    init();
    int num;
    cin >> num;
    for(int i = 0; i < num; i++){
      char trash;
      int price, weight;
      cin >> price >> trash >> weight;
      for(int j = 1; j <= max; j++){
	if(weight <= j){
	  dp[i][j] = price;
	  if(i)dp[i][j]+= dp[i-1][j-weight];
	}
      }
    }
    int ablep = 0, ablew = 10000;
    for(int i = 1; i < num; i++){
      for(int j = 1; j < max; j++){
	if(ablep < dp[i][j]){
	  ablep = dp[i][j];
	  ablew = j;
	}
      }
    }
    cout << "Case " << rev << ":" << endl;
    cout << ablep << endl << ablew << endl;

  }
  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<=N;i++){
			dp[i]= 0;
		}
		for(int i=0;i<N;i++){
			for(int j=W;j>=we[i];j--){
				dp[j] = max(dp[i],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;
	}
}