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<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;
	}
}