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++: 
import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Math.*;

public class Main {

	int INF = 1 << 28;
	//long INF = 1L << 62;
	double EPS = 1e-10;

	void run() {
		Scanner sc = new Scanner(System.in);
		for (int c=1;;c++) {
			int W = sc.nextInt();
			if (W == 0) break;
			
			int n = sc.nextInt();
			int[] v = new int[n], w = new int[n];
			for (int i=0;i<n;i++) {
				String[] ws = sc.next().split(",");
				v[i] = Integer.valueOf(ws[0]); w[i] = Integer.valueOf(ws[1]);
			}
			
			int[] dp = new int[W+1];
			fill(dp, -1); dp[0] = 0;
			for (int i=0;i<n;i++) for (int j=W;j>=w[i];j--) if (dp[j-w[i]]>=0){
				dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
			}
			int aw = 0, av = 0;
			for (int i=0;i<=W;i++) if (av < dp[i]){
				av = dp[i];
				aw = i;
			}
			System.out.println("case " + c + ":");
			System.out.println(av);
			System.out.println(aw);
		}
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}

	public static void main(String[] args) {
		new Main().run();
	}
}

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;

int main(void){
	int i,j;
	int n,tmp;
	vector<int> v,w;
	int max_w;
	int cur=0,nxt=1;
	int dp[2][10001];
	int index=0;
	int tmp_max = 0;
	
	while(cin >> max_w){
		if(max_w == 0) break;
		memset(dp, 0, sizeof(dp));
		cur = 0;
		nxt = 1;
		cin >> n;
		for(i=0;i<n;i++){
			cin >> tmp;
			v.push_back(tmp);
			cin.ignore();
			cin >> tmp;
			w.push_back(tmp);
		}
		//f[^ÇÝÝ±±ÜÅ
		
		/*
		*  dp[i+1][j] := iÔÚÜÅÅd³jÉµ½Æ«ÌÅåÌ¿l
		*  dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i]) if(j > w[i])
		*  dp[i+1][j] = dp[i][j]
		*/
		for(i=0;i<n;i++){
			for(j=0;j<=max_w;j++){
				if(j >= w[i]){
					dp[nxt][j] = max(dp[cur][j], dp[cur][j-w[i]] + v[i]);
				}else{
					dp[nxt][j] = dp[cur][j];
				}
				//cout << dp[nxt][j] << " ";
			}
			nxt = 1 - nxt; //toggle
			cur = 1 - cur; //toggle
			//cout << endl;
		}
		
		cout << dp[cur][max_w] << endl;
		for(i=0;i<=max_w;i++){
			if(tmp_max < dp[cur][i]){
				tmp_max = dp[cur][i];
				index = i;
			}
		}
		cout << index << endl;
		v.clear();
		w.clear();
	}
	
	
}

C++: #include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef pair< int , int > P;

int W, N, value[1000], weight[1000];
P dp[1001][1001];

P rec(int i,int j){ //i番目以降の品物からj以下となるように選ぶ
  if(dp[i][j] != P(-1,-1)) return dp[i][j];
  P res;
  if(i == N) res = P(0,j);
  else if(j < weight[i]) return res = rec(i+1,j);
  else{
    P tmp = rec(i+1,j-weight[i]);
    res = max(rec(i+1,j),P(tmp.first + value[i],tmp.second));
  }
  return dp[i][j] = res;
}

int main(){
  int cnt = 0; char op;
  while(cin >> W , W){
    fill_n(*dp,1001*1001,P(-1,-1));
    cin >> N;
    for(int i = 0 ; i < N ; i++ ) cin >> value[i] >> op >> weight[i];
    P p = rec(0,W);
    cout << "Case " << ++cnt << ":" << endl;
    cout << p.first << endl;
    cout << W - p.second << endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> P;
typedef pair<P,int> status;

int main() {
	int W,N,cnt = 1;
	while(true){
		cin >> W;
		if(W==0) return 0;
		cin >> N;
		int val[1001],weight[1001];
		memset(val,0,sizeof(val));
		memset(weight,0,sizeof(weight));
		for(int i = 0;i<N;i++){
			cin >> val[i];
			cin.get();
			cin >> weight[i];
		}
		int dp[1003];
		memset(dp,0,sizeof(dp));
		for(int i = 0;i<N;i++){
			for(int j = W;j >= weight[i];j--){
				if(j-weight[i]<0)continue;
				dp[j] = max(dp[j],dp[j-weight[i]]+val[i]);
			}
		}
		int mini;
		for(int i = W;dp[i] == dp[W];i--) mini = i;
		cout << "Case " << cnt << ":" << endl;
		cout << dp[W] << endl;
		cout << mini << endl;
		cnt++;
	}
	return 0;
}

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

int sum[1000+1];
int temp[1000+1];
int size;

void update(int score, int weight) {
	for (int i = weight; i <= size; i++) {
		int a = sum[i-weight] + score;
		temp[i] = a > sum[i] ? a : sum[i];
	}
	for (int i = weight; i <= size; i++)
		sum[i] = temp[i];
}

int main() {
	int k = 0, n;
	while (cin >> size, size) {
		k++;
		cin >> n;
		for (int i = 0; i <= size; i++)
			sum[i] = temp[i] = 0;
		
		for (int i = 0; i < n; i++) {
			int a, b; scanf("%d,%d", &a, &b);
			update(a, b);
		}

		int w = size;
		while (w > 0 && sum[w-1] == sum[size]) w--;
		cout << "Case " << k << ":" << endl;
		cout << sum[size] << endl;
		cout << w << endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string.h>
int w[1000],v[1000],W,N,dp[2][10000],dpw[2][10000];

bool solve(int num)
{
	memset(dp,0,sizeof(dp));
	memset(dpw,0,sizeof(dpw));
	scanf("%d",&W);
	if(W==0)
		return false;
	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)&1][j]=dp[i&1][j];
				dpw[(i+1)&1][j]=dpw[i&1][j];
			}
			else
			{
				int t1=dp[i&1][j], t2=dp[i&1][j-w[i]]+v[i], w1=dpw[i&1][j], w2=dpw[i&1][j-w[i]]+w[i];
				if(t1>t2||(t1==t2&&w1<w2))
				{
					dp[(i+1)&1][j]=t1;
					dpw[(i+1)&1][j]=w1;
				}
				else
				{
					dp[(i+1)&1][j]=t2;
					dpw[(i+1)&1][j]=w2;
				}
			}
		}
		printf("Case %d:\n%d\n%d\n",num,dp[N&1][W],dpw[N&1][W]);
		return true;
}

int main()
{
	for(int i=0;;i++)
		if(!solve(i+1))
			return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

// ====================================================================

int main() {
  int W, N, v, w, count = 1;
  char c;
  while (true) {
    cin >> W;
    if (W == 0) return 0;
    cin >> N;

    ll dp[N + 1][W + 1];
    for (int i = 0; i <= N; i++)
      for (int j = 0; j <= W; j++)
        dp[i][j] = 0;

    for (int i = 0; i < N; i++) {
      cin >> v >> c >> w;
      for (int j = W; j >= 0; j--) {
        // 入れないパターン
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        // 入れるパターン
        if (j - w >= 0) dp[i + 1][j - w] = max(dp[i + 1][j - w], dp[i][j] + v);
      }
    }

    int res = 0, pos = 0;
    for (int j = W; j >= 0; j--) {
      if (res < dp[N][j]) {
        res = dp[N][j];
        pos = j;
      }
    }
    cout << "Case " << count << ":" << endl;
    cout << res << endl;
    cout << W - pos << endl;
    count++;
  }
}