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