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 <vector>
#include <stdio.h>

using namespace std;

int MAX = 1000*1000;

void solve(int n, int W, vector<int> v, vector<int> w) {
	int **dp = new int*[n];
	for (int i=0; i<n; i++) {
		dp[i] = new int[MAX+2];
		for (int j=0; j<MAX+2; j++)
			dp[i][j] = 0;
	}

	dp[0][w[0]] = v[0];
	for (int i=1; i<n; i++) {
		for (int j = 0; j<MAX+2; j++) {
			dp[i][j] = max(dp[i][j], dp[i-1][j]);
			if (j+w[i] <= W) dp[i][j+w[i]] = max(dp[i][j+w[i]], dp[i-1][j]+v[i]);
		}
	}

	int val = -1, weight = 0;
	for (int j=0; j<=W; j++) {
		if (val < dp[n-1][j]) {
			val = dp[n-1][j];
			weight = j;
		}
	}

	cout << val << endl << weight << endl;

	for (int i=0; i<n; i++)
		delete [] dp[i];
	delete [] dp;
}

int main() {
	

	int n, W;
	int ver = 1;

	cin >> W;
	while (W!=0) {
		cin >> n;

		vector<int> v(n), w(n);
		for (int i=0; i<n; i++)
			scanf("%d,%d", &v[i], &w[i]);

		cout << "Case " << ver << ":" << endl;
		solve(n, W, v, w);

		cin >> W;
		ver++;
	}

	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <random>
#include<cstdlib>
#define _USE_MATH_DEFINES
#include <math.h>

#define INF (1<<21)
#define swap(a,b) {a=a+b; b=a-b; a=a-b;}
#define abs(a) (a > 0 ? a:-a)
#define min(a,b) {a > b ? b:a}
#define max(a,b) {a > b ? a:b}
using namespace std;

//O(W*N)??§??????
#define MAX_W 1000
#define MAX_N 100
#define MAX_VALUE 10000
#define MAX_WEIGHT 1000

int dp[MAX_W+1];
vector<pair<int, int> > Item;
void q42() {
	int W, N;
	int cnt = 1;
	for (; cin >> W;) {
		if (!W) break;

		cin >> N;
		for (int i = 0; i < N; i++) {
			int value, weight;
			char tmp;
			cin >> value >> tmp >> weight;
			Item.push_back(make_pair(value, weight));
		}
		for (int i = 0; i <= W; i++) {
			dp[i] = -1;
		}
		dp[0] = 0;

		for (int i = 0; i < N; i++) {
			int value = Item[i].first;
			int weight = Item[i].second;
			for (int j = W; j >= 0; j--) {
				if (j - weight < 0) continue;
				if (dp[j - weight] == -1) continue;
				dp[j] = max(dp[j], dp[j - weight] + value);
			}
		}

		int ans_value = 0, ans_weight = 0;
		for (int i = 0; i <= W; i++) {
			if (ans_value < dp[i]) {
				ans_value = dp[i];
				ans_weight = i;
			}
		}

		cout << "Case " << cnt << ":" << endl;
		cout << ans_value << endl;
		cout << ans_weight << endl;
		cnt++;
	}
}

int main() {
	q42();
	return 0;
}

C++: 
// dpの練習
// dpでなく、愚直に組んだプログラミング
// ナップサック問題とほぼ同じ

#include <iostream>
#include <algorithm>

using namespace std;

int wei; // 持てる最大の重さ
int n; // お宝の数
int v[1001]; // お宝の価値
int w[1001]; // お宝の重さ
 // グローバル配列は0で初期化されている
int dp[1001][1001]; //メモテーブル

int max_value(int i, int j) {
  int value;

  if( dp[i][j] >= 0 ){
    return dp[i][j];
    //すでに１回は調べている
  }
 
  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    //もうお宝がないつまり、価値は0
    value = 0;
  } else if (j < w[i]) {
    //風呂敷が持てる重さを超えてしまう
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    value = max_value(i + 1, j);
  } else {
    //左がお宝を入れない、右がお宝を入れた場合
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    value = max( max_value(i + 1, j), max_value(i + 1, j - w[i]) + v[i] );
  } 
  return dp[i][j] = value;
  //メモに記憶させる
}

void max_weight( int ans ){

  int w_min;
   
  for (int i = wei -1 ; i >= 0 ; i--){
    if( ans != max_value( 0 , i ) ){
         w_min = i + 1;
         break;
    }
  }
  cout << w_min << endl;  
}


int main(void){

  int cnt = 1;
  while( cin >> wei ){
    memset( dp , -1 , sizeof(dp) );
    //まだ調べてないところを-1で初期化
    if( wei == 0 )break;
    cin >> n;
    char ten;
    for( int i = 0 ; i < n ; i++ ){
      cin >> v[i] >> ten >> w[i];
    }
    cout << "Case " << cnt << ":" << endl;
    int ans = max_value( 0 , wei );
    cout << ans << endl;
    max_weight(ans);
    cnt++;
  }
  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;
}