Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

const MaxW = 1000
const MaxN = 1000

type Tuple struct {
	Value  int
	Weight int
}

func nextLine(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func solve(w int, n int, vws []Tuple) Tuple {
	var dp [MaxN + 1][MaxW + 1]int

	for i := 0; i < n; i++ {
		for j:= w; j > 0; j-- {
			if j < vws[i].Weight {
				dp[i + 1][j] = dp[i][j]
			} else {
				dp[i + 1][j] = int(math.Max(float64(dp[i][j]), float64(dp[i][j - vws[i].Weight] + vws[i].Value)))
			}
		}
	}

	maxValue := dp[n][w]
	minWeight := w

	for j := w - 1; j > 0; j-- {
		if maxValue == dp[n][j] {
			minWeight = j
		} else {
			break
		}
	}

	return Tuple{Value: maxValue, Weight: minWeight}
}

func main() {
	var sc = bufio.NewScanner(os.Stdin)
	index := 1
	for {
		var w, n int
		w, _ = strconv.Atoi(nextLine(sc))
		if w == 0 {
			return
		}
		n, _ = strconv.Atoi(nextLine(sc))

		vws := make([]Tuple, n, n)
		for i := 0; i < n; i++ {
			array := strings.Split(nextLine(sc), ",")
			v, _ := strconv.Atoi(array[0])
			w, _ := strconv.Atoi(array[1])
			vws[i] = Tuple{Value: v, Weight: w}
		}

		t := solve(w, n, vws)
		fmt.Printf("Case %d:\n", index)
		fmt.Println(t.Value)
		fmt.Println(t.Weight)
		index++
	}
}

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

long long dp[1000];
int main(){
	long long w, n, ans_w = 0, ans_val = 0;
	for (int dataset = 1; cin >> w, w; dataset++){
		int t_w, t_val;
		cin >> n;
		for (int i = 0; i < w; i++) dp[i] = -1;
		dp[0] = 0;
		for (int i = 0; i < n; i++){
			scanf("%lld,%lld", &t_val, &t_w);
			for (long long j = w - 1; j >= 0; j--){
				//printf("%d ", dp[j]);
				if (dp[j] == -1 || j + t_w >= w || dp[j + t_w] > dp[j] + t_val)
					continue;
				dp[j + t_w] = dp[j] + t_val;
			}
			//puts("");
		}
		for (int i = 0; i < w; i++){
			//printf("%d ", dp[i]);
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		//puts("");
		printf("Case %d:\n%lld\n%lld\n", dataset, ans_val, ans_w);
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
#define REP(i,n) for (int i=0; i<n; i++)

int main(void){
	int c=0, W;
	char s;
	while (true){
		cin>>W;
		if (W==0) break;
		int dt[1001];
		REP(i,1001) dt[i]=0;
		c ++;

		int p,w,N;
		cin>>N;
		REP(i,N){
			cin>>p>>s>>w;
			for (int w0=W; w0>=0; w0--){
				int p0=dt[w0];
				if (w0==0 || p0!=0){
					int w1 = w0+w;
					int p1 = p0+p;
					if (w1<=W && dt[w1]<p1)	dt[w1]=p1;
				}
			}
		}
		int m=0,n;
		REP(i, W+1)
			if (dt[i]>m){
				m = dt[i];
				n = i;
			}
		cout<<"Case "<<c<<":"<<endl;
		cout<<m<<endl;
		cout<<n<<endl;
	}
}

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

int dp[1000][10000];
int W;
int n;
int v[1000], w[1000];
int now = 1;

int main()
{
	while( 1 ){
		memset( dp, 0, sizeof( dp ) );
		scanf( "%d", &W );
		if( !W ) break;
		scanf( "%d", &n );
		for( int i = 0; i < n; i++ ){
			scanf( "%d,%d",  &v[i], &w[i] );
		}
		
		for( int i = 0; i < n; i++ ){
		 	for( int j = 0; j <= W; j++ ){
				if( j < w[i] ){
				 	dp[i+1][j] = dp[i][j];
				}
				else{
				 	dp[i+1][j] = std::max( dp[i][j], dp[i][j-w[i]] + v[i] );
				}
			}
		}
		
		int res = W;
		for( int i = W; i >= 0; i-- ){
			if( dp[n][i] == dp[n][W] ) res = std::min( res, i );
			else break;
		}
		
		printf( "Case %d:\n", now );
		printf( "%d\n%d\n", dp[n][W], res );
		
		now++;	
	}
	

 	return 0;
}

C++: #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( scanf("%d",&W), W ){
		int n, v[1000], w[1000];
		memset( dp, -1, sizeof(dp) );
		scanf("%d", &n);
		rep(i,n){
			scanf("%d,%d", &v[i], &w[i]);
		}
		dp[0][0] = 0;
		rep(i,n){
			for(int j=W; j>=w[i]; j--){
				if( dp[i][j-w[i]] != -1 ){
					dp[i+1][j] = max( dp[i][j-w[i]]+v[i], dp[i][j] );
				}
			}
			for(int j=w[i]-1; j>=0; j--){
				dp[i+1][j] = dp[i][j];
			}
		}
		int mw = 0;
		rep(i,W){
			if( dp[n][i]>dp[n][mw] )	mw = i;
		}
		printf("Case %d:\n",cnt++);
		printf("%d\n", dp[n][mw]);
		printf("%d\n", mw);
	}
}

C++: #include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;

ll value[1000], weight[1000];
ll dp[1001][1001]; // dp[?¬?????????????][??????] = ?????§??????

int main() {
	int W;
	int c = 1;
	while ( cin >> W, W ) {
		int N; cin >> N;
		for (int i = 0; i < N; ++i) scanf("%d,%d", &value[i], &weight[i]);

		fill(&dp[0][0], &dp[0][0]+1001*1001, 0);

		for (int i = 0; i < N; ++i) {
			for (int w = 0; w <= W; ++w) {
				dp[i+1][w] = max(dp[i+1][w], dp[i][w]);
				if (w + weight[i] <= W) {
					dp[i+1][w+weight[i]] = max(dp[i+1][w+weight[i]], dp[i][w]+value[i]);
				}
			}
		}
		ll av = 0, aw = 0;
		for (int w = 0; w < W; ++w) {
			if (dp[N][w] > av) {
				av = dp[N][w];
				aw = w;
			}
		}
		printf("Case %d:\n", c++);
		printf("%d\n", av);
		printf("%d\n", aw);
	}
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>

#define REP(X, Y) for( int (X) = 0; (X) < (Y); ++(X) )
#define REPT(X, Y, Z) for( int (X) = (Y); (X) < (Z); ++(X) )
#define REPR(X, Y) for( int (X) = (Y); (X) >= 0; --(X) )

int w, n, u[1000], v[1000];
int dp[1001][1000];
int dataset;

int main(){
	while (true){
		scanf("%d", &w);
		if (!w)break;
		scanf("%d", &n);
		++dataset;
		REP(i, n)
			memset(dp[i], -1, (w + 1)*sizeof(int) / sizeof(char)),
			scanf("%d,%d", &v[i], &u[i]);
		dp[0][0] = 0;
		REP(i, n){ //i番目の要素について検証
			REP(j, w+1){
				if (dp[i][j] == -1)continue;
				dp[i + 1][j] = dp[i][j];
				if (j + u[i] > w)continue;
				dp[i + 1][j + u[i]] =
					std::max(
					dp[i + 1][j] + v[i],
					dp[i][j + u[i]]
					);
			}
		}
		int maxu = [&]()->int{
			REPR(i, w)
				if (dp[n][i] != -1)return i;
		}();
		printf("Case %d:\n%d\n%d\n", dataset, dp[n][maxu], maxu-1);
	}
	return 0;
}

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

int dp[1001][1001];

int main(){
	int case_number = 1;
	while(1){
		int W, N;
		scanf("%d%d", &W, &N);
		if(W == 0) break;

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

		int answ = 0;
		for(int j = 0; j <= W; ++j){
			if(dp[N][j] > dp[N][answ]){
				answ = j;
			}
		}

		printf("Case %d:\n%d\n%d\n", case_number, dp[N][answ], answ);
		++case_number;
	}
}