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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <sstream>
#include <deque>

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

using namespace std;

/**
wがあふれないようにする
価値が最大

dp[重さ] = 最大価値
*/

int dp[1000] = {-1};

int main() {
    int w, cases = 1;
    
    while(cin >> w, w) {
        int n, a[1000][2];
        cin >> n;

        for ( int i = 0; i < n; i ++ ) {
            scanf("%d,%d", &a[i][1], &a[i][0]);
        }

        for ( int i = 0; i < n; i++ ) {
            for ( int j = 0; j < n; j++ ) {
                int ww = a[i][0] + a[j][0],
                    p = a[i][1] + a[j][1];
                if ( ww > w ) continue;
                if ( p > dp[ww] ) dp[ww] = p;
            }
        }
        int max_i = 0;
        for ( int i = 0; i < w; i++ ) {
            if ( dp[i] > dp[max_i] ) max_i = i;
        }
        printf("Case %d:\n%d\n%d\n", cases++, max_i, dp[max_i]);
    }
    
    return 0;
}

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

typedef struct{
  int value; 
  int weight;
}SET;

int main(){
  int cnt=1;

  while(1){
    
    int w,n;
    SET data[1111];
    int memo[1111][1111]={{0}};
    
    scanf("%d",&w);
    if(w==0) break;
    scanf("%d",&n);
    
    for(int i=0;i<n;i++){
      scanf("%d,%d",&data[i].value,&data[i].weight);
    }
    
    memo[0][data[0].weight]=data[0].value;
    int max_value=data[0].value;
    int max_weight=data[0].weight;
    
    for(int i=1;i<n;i++){
      if(data[i].value>memo[i-1][data[i].weight]){
	memo[i][data[i].weight]=data[i].value;
      }
      for(int j=1;j<=w;j++){
	if(memo[i-1][j]>0 && memo[i-1][j]>memo[i][j]){
	  memo[i][j]=memo[i-1][j];
	}
	
	if(j+data[i].weight<=w && memo[i-1][j+data[i].weight]<data[i].value+memo[i-1][j]){
	  memo[i][j+data[i].weight] = data[i].value+memo[i-1][j];
	}
	
	if(memo[i][j]==max_value){
	  if(max_weight>j){
	    max_weight=j;
	  }
	}
	if(memo[i][j]>max_value){
	  max_value=memo[i][j];
	  max_weight=j;
	}
      }
    }
    printf("Case %d:\n",cnt);
    printf("%d\n",max_value);
    printf("%d\n",max_weight);
    cnt++;
    
  }	

  return 0;
}

C++: #include <iostream>

using namespace std;

int n, W;
int v[1010], w[1010];
int dp[1010][1010];

void solve() {
	for (int i=0; i<n; i++) {
		for (int j=0; j<=W; j++) {
			if (w[i] <= j) dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i]);
			else dp[i+1][j] = dp[i][j];
		}
	}
	
	int sumV = 0, sumW = 0;
	for (int i=0; i<=W; i++) {
		if (dp[n][i] > sumV) {
			sumV = dp[n][i];
			sumW = i;
		}
	}
	cout << sumV << endl;
	cout << sumW << endl;
}

int main() {
	int Case = 0;
	
	while (1) {
		cin >> W; if (W==0) break;
		
		Case++;
		cout << "Case " << Case << ":" << endl;
		
		cin >> n;
		
		char dumy;
		for (int i=0; i<n; i++) cin >> v[i] >> dumy >> w[i];
		
		solve();
	}
	
	return 0;
}

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

int T, N, V[1010], W[1010];

int main() {

	for ( int t = 1; scanf( "%d", &T ) && T; t++ ) {
	
		scanf( "%d", &N );
	
		for ( int i = 0; i < N; i++ ) { scanf( "%d,%d", &V[i], &W[i] ); }
	
		int A[1010][1010] = {{0}};
	
		for ( int i = 1; i < N; i++ ) {
		
			for ( int j = 0; j <= T; j++ ) {
	
				if ( j < W[i-1] ) {
	
					A[i][j] = A[i-1][j];
	
				} else {
	
					A[i][j] = max( A[i-1][j], A[i-1][j-W[i-1]] + V[i-1] );
					
				}
	
			}
	
		}
		
		int best = -1, best_w = -1;
		
		for ( int j = 0; j <= T; j++ ) {
		
			if ( A[N-1][j] > best ) {
			
				best = A[N-1][j];
				best_w = j;
				
			}
		
		}
		
		printf( "Case %d:\n%d\n%d\n", t, best, best_w );
	
	}

}

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

int n, W;
int w[1001], v[1001];
int dp[1001][1001];
int num=1;

void reset_variable(){
	W = 0;
	n = 0;
	for (int i = 0; i < 1001; i++){
		w[i] = 0;
		v[i] = 0;
	}
	memset(dp, 0, sizeof(dp));
}


void solve(void){
	cin >> n;
	for (int i = 0; i<n; i++){
		scanf("%d,%d", &v[i], &w[i]);
	}
	cout << "Case " << num << ":" << endl;
	for (int i = 0; i <= W; i++){
		dp[n][i] = 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], v[i] + dp[i + 1][j - w[i]]);
			}
		}
	}
	for (int i = 0; i < 1001; i++){
		if (dp[0][i] == dp[0][W]){
			cout << dp[0][W] << endl << i << endl;
			return;
		}
	}
}

int main(){
	while (1){
		reset_variable();
		cin >> W;
		if (!W) return 0;
		solve();
		num++;
	}
	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 <= 5; 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;
}