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>


#define NMAX 1000
#define WMAX 1000
using namespace std;

int main(void){

	int W,N;
	char gav;
	int k[NMAX+1],o[NMAX+1];
	int dp[2][WMAX+1];
	int max;

	for(int i=1;;i++){
		cin>>W;
		if(W==0) break;
		cin>>N;
		for(int j=0;j<N;j++){
			cin>>k[j]>>gav>>o[j];
		}
		for(int j=0;j<2;j++){
			for(int l=0;l<=W;l++){
				dp[j][l]=-1;
			}
		}
		dp[0][0]=0;
		for(int j=0;j<N;j++){
			for(int l=0;l<=W;l++){
				if(dp[0][l]>=0){
					if(l+o[j]<=W){
						if(dp[0][l]+k[j]>dp[0][l+o[j]]){
							dp[1][l+o[j]]=dp[0][l]+k[j];
						}
					}
				}
			}
			for(int l=0;l<=W;l++){
				if(dp[1][l]!=-1) dp[0][l]=dp[1][l];
			}
		}
		max=0;
		for(int j=0;j<=W;j++){
			if(dp[0][j]>dp[0][max]) max=j;
		}
		cout<<"Case "<<i<<":"<<endl;
		cout<<dp[0][max]<<endl;
		cout<<max<<endl;
	}

	return 0;
}

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

int a[1001] = { 0 };
int b[1001] = { 0 };
long w, n, mv, mw;
void solve(long W, long f, long sum) {
	f = min(f, n) + 1;
	for (int i = f; i < n; i++) {
		if (W - b[i] <= 0) {
			mw = max(w - W, mw);
			mv = max(sum, mv);
			return;
		}
		solve(W - b[i], f, sum + a[i]);
	}
}

int main() {
	int u = 0;
	while (scanf("%d", &w)) {
		if (w == 0) break;
		scanf("%d", &n);

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

		solve(w, -1, 0);
		printf("Case %d:\n%d\n%d\n", u, mv, mw);
	}
	return 0;
}

C++: //Solution for aoj:0042 A Thief
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
using namespace std;
#define int  long long unsigned int
int w, n, c;
char a;
int trs[1001][2];
int dp[1001][1001];
signed main(){
	while (cin >> w){
		if (!w)
			return 0;
		c++;
		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> trs[i][0] >> a >> trs[i][1];
		}
		for (int i = 1; i <= n; i++){
			for (int j = 1; j <= w; j++){
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
				if (j >= trs[i][1])
					dp[i][j] = max(dp[i][j], dp[i - 1][j - trs[i][1]] + trs[i][0]);
			}
		}
		for (int i = 1; i <= w; i++){
			if (dp[n][i] == dp[n][w]){
				cout << "Case " << c << ':' << endl << dp[n][i] << endl << i << endl;
				break;
			}
		}
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include<cstdlib>


#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++: // 重さの総和W以下, 同率なら軽い方を出力

#include <iostream>
#include <cstring>

using namespace std;

const int N_MAX=1000;
const int W_MAX=10000;
int value[N_MAX], weight[N_MAX];

int memoW[W_MAX];
int NUM;
int VTOTAL=-1, WTOTAL=9999999;
int W;

int max(int a, int b);
int maximumValue(int v, int w, int n); // v:現在の価値, w:現重量 n番目の要素に対して

int main() {
  cin>>W;
  cin>>NUM;
  for(int i=0; i<NUM; i++) {
    cin>>value[i]; // 価値
    cin.ignore(); // 1文字読み飛ばし(",")
    cin>>weight[i]; // 重さ
  }
  memset(memoW, -1, sizeof(memoW));
  cout<<maximumValue(0, 0, 0)<<endl;
  cout<<WTOTAL<<endl;
}

int max(int a, int b) {
  if(a>b) {
    return a;
  }
  else {
    return b;
  }
}

int maximumValue(int v, int w, int n) {
  if(n==NUM) { // 最深部
    if(v>VTOTAL) {
      VTOTAL=v;
      WTOTAL=w;
    }
    if(v==VTOTAL && w<WTOTAL) {
      WTOTAL=w;
    }
    return v;
  }
  else if(w+weight[n]<=W) {
    return max(maximumValue(v, w, n+1), maximumValue(v+value[n], w+weight[n], n+1));
  }
  else {
    return maximumValue(v, w, n+1);
  }
}