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>
#include<cstdio>

using namespace std;
#define MAX 1000

struct Item{
  int value,weight;
};
int max;
int N,W;
Item items[MAX];
int H[MAX+1][MAX+1];//Óëµ«
int cc=1;

void compute(){
  for(int w=0;w<=W;w++) H[0][w]=0;
  for(int i=1;i<=N;i++) H[i][0]=0;
  
  for(int i=1;i<=N;i++){
    for(int w=1;w<=W;w++){
      if(items[i].weight <= w){
	if(items[i].value + H[i-1][w-items[i].weight] >  H[i-1][w]){
	  H[i][w] = items[i].value + H[i-1][w-items[i].weight];
	}
	else H[i][w]=H[i-1][w];
      }
      else H[i][w] = H[i-1][w];
    }
  }

}

void input(){
  cin >> N;
  for(int i=1;i<=N;i++){
    scanf("%d,%d", &items[i].value,&items[i].weight);
  }
}

void calc(){
  int sw=10000;//àÁÆà¿¢³¢wðÛ¶
  for(int i=1;i<=N;i++){
    for(int w=1;w<=W;w++){
      if(H[i][w] == H[N][W] && w<=sw){
	sw = w;
      }
    }
  }
  printf("Case %d:\n%d\n%d\n",cc,H[N][W],sw);  
}


int main(){

  int maxValue;
  while(cin >> W){
    if(W==0)break;
    input();
    compute();
    calc();
    cc++;


  }
}

C++: #include<cstdio>
#include<iostream>
#include<array>
using namespace std;
array<pair<int,int>,1000> pairs;
array<array<pair<int,int>,10001>,1001> m;
int main(){
  for(int caseNo=0;;caseNo++){
    int w; scanf("%d",&w);
    if(w==0) break;
    int n; scanf("%d",&n);
    for(int i=0;i<n;i++){
      scanf("%d,%d",&pairs[i].first,&pairs[i].second);
    }
    for(int i=1;i<=n;i++){
      for(int j=0;j<=w;j++){
	if(pairs[i].second<=j){
	  if(m[i-1][j].first>m[i-1][j-pairs[i].second].first+pairs[i].first){
	    m[i][j]=m[i-1][j];
	  }else{
	    m[i][j].first=m[i-1][j-pairs[i].second].first+pairs[i].first;
	    m[i][j].second=m[i-1][j-pairs[i].second].second+pairs[i].second;
	  }
	}else{
	  m[i][j].first=m[i-1][j].first;
	}
      }
    }
    printf("Case %d:\n%d\n%d\n",caseNo+1,m[n][w].first,m[n][w].second);
  }
}

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

int W, N;
int v[1000], w[1000], dp[1000+1][1000+1];

int solve(int i, int weight, int value)
{
  if (dp[i][weight] != -1)
    return dp[i][weight];

  dp[i][weight] = value;
  if (i == N)
    return value;

  int ret = -1;
  if (weight+w[i] <= W)
    ret = max(ret, solve(i+1, weight+w[i], value+v[i]));
  return max(ret, solve(i+1, weight, value));  
}

int main()
{
  int cnt = 1;
  while (~scanf("%d", &W)) {
    if (W == 0)
      break;

    for (int i = 0; i < 1001; ++i) {
      for (int j = 0; j < 1001; ++j)
	dp[i][j] = -1;
    }

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

    int value = solve(0, 0, 0), weight = 1 << 30;
    for (int i = 0; i < N+1; ++i) {
      for (int j = 0; j < W+1; ++j) {
	if (dp[i][j] == value)
	  weight = min(weight, j);
      }
    }
    printf("Case %d:\n", cnt++);
    printf("%d\n%d\n", value, weight);
  }
  return 0;
}

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

#define MAX 1001

int dp[MAX][MAX];
int v[MAX], w[MAX];

int main() {
	int W, n, u = 0;
	char t;
	while (cin >> W) {
		if (W == 0) break;
		cin >> n;
		u++;

		for (int i = 0; i < n; i++) {
			cin >> v[i] >> t >> w[i];
		}

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

		int mv, mw;
		mv = mw = 0;
		for (int i = 0; i <= W; i++) {
			if (dp[n][i] > mv) {
				mv = dp[n][i];
				mw = i;
			}
		}

		cout << "Case " << u << ":" << endl;
		cout << mv << endl;
		cout << mw << endl;
	}
	return 0;
}

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

int data[1000][2];
int dp[1000][1000];
int n,w;

int solve(){
	for(int i=1;i<=n;i++){
		for(int j=1;j<=w;j++){
			if(j >= data[i][1]){
				dp[i][j] = max(dp[i-1][j],dp[i-1][j-data[i][1]]+data[i][0]);
			}else{
				dp[i][j] =dp[i-1][1];
			}
		}
	}
	return dp[n][w];
}

int solve2(){
	for(int i=0;i<=w;i++){
		if(dp[n][w] == dp[n][i]){
			return i;
		}
	}
	return w;
}

int main(){
	int casecount = 0;
	while(true){
		casecount++;
		cin>>w;
		if(w == 0){
			break;
		}
		memset(dp,0,sizeof(dp));

		cin >> n;
		for(int i=0;i<n;i++){
			scanf("%d,%d",&data[i][0],&data[i][1]);
		}
		
		cout << "Case " << casecount << ":" << endl;
		cout << solve() << endl;
		cout << solve2() << endl;
	}
}

C++: #include<cstdio>
int main(void){
	int ww;
	int n;
	int a[1001][1001];
	int w[1001];
	int p[1001];
	int max;
	int mm;
	for (int tt=1;;tt++){
		scanf("%d", &ww);
		if (ww == 0)break;
		scanf("%d", &n);
		for (int i = 0; i<1001; i++)a[0][i] = 0;
		for (int i = 1; i <= n; i++){
			scanf("%d,%d", &p[i], &w[i]);
			for (int j = w[i]; j <= ww; j++){
				if (a[i - 1][j] > a[i - 1][j - w[i]] + p[i]){
					a[i][j] = a[i - 1][j];
				}
				else{
					a[i][j] = a[i - 1][j - w[i]] + p[i];
				}
			}
		}
		max = 0;
		mm = 0;
		for (int i = 0; i <= ww; i++){
			if (max < a[n][i]){
				max = a[n][i];
				mm = i;
			}
		}
		printf("Case %d:\n%d\n%d\n", tt, max, mm);
	}
}

C++: #define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <algorithm>
#include <complex>
#include <cassert>
#include <vector>
#include <iomanip>

using namespace std;

typedef complex<double> P;

#define all(c) (c).begin(), (c).end()
#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,b) loop(i,0,b)

const double eps = 1e-10;

double cross(const P& a, const P& b) {
    return imag(conj(a)*b);
}
double dot(const P& a, const P& b) {
    return real(conj(a)*b);
}

struct L : public vector<P> {
    L(const P &a, const P &b) {
        push_back(a); push_back(b);
    }
};

P crosspoint(const L &l, const L &m) {
    double A = cross(l[1] - l[0], m[1] - m[0]);
    double B = cross(l[1] - l[0], l[1] - m[0]);
    if (abs(A) < eps && abs(B) < eps) return m[0]; // same line
    if (abs(A) < eps) assert(false); // !!!PRECONDITION NOT SATISFIED!!!
    return m[0] + B / A * (m[1] - m[0]);
}

typedef vector<int> vi;

int main(){
    int W;
    int c = 1;
    while (cin >> W && W){
        printf("Case %d:\n",c);
        c++;
        int n; cin >> n;
        vi v(n), w(n);
        rep(i, n){
            scanf("%d,%d", &v[i], &w[i]);
        }
        vi dp(W + 1);
        rep(i, n){
            for (int j = W; j >= 0; j--){
                if (j - w[i] >= 0) dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
            }
        }
        int ans = 1e8;
        rep(i, W + 1){
            if (dp[i] == dp.back()){
                ans = min(ans, i);
            }
        }
        cout << dp[W] << endl << ans << endl;
    }
}