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 <cstdio>
#include <utility>
#include <algorithm>
using namespace std;

typedef pair<int, int> Pair;

int w;
int n;
int vs[1001];
int ws[1001];
int dp[1001][1001];

int main() {
  for (int Case=1; ; Case++) {
    scanf("%d", &w);
    if (w == 0) return 0;

    scanf("%d", &n);
    for (int i=0; i<n; i++) {
      scanf("%d,%d", &vs[i], &ws[i]);
    }

    for (int j=0; j<=w; j++) dp[0][j] = 0;
  
    for (int i=0; i<n; i++) {
      for (int j=0; j<=w; j++) {
        dp[i+1][j] = dp[i][j];
      }
  
      for (int j=0; j<=w-ws[i]; j++) {
        dp[i+1][j+ws[i]] = max(dp[i+1][j+ws[i]], dp[i][j] + vs[i]);
      }
    }
  
    Pair ans(0, 0);
    for (int j=0; j<=w; j++) {
      ans = max(ans, Pair(dp[n][j], -j));
    }
  
    printf("Case %d:\n%d\n%d\n", Case, ans.first, -ans.second);
  }
}

C++: #include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
using namespace std;
int inf = 1000000000;
// dp[i][j] = i ???????????§??§?????? j ??§?????????
// dp[i][j] = dp[i][j-?] + v[i];

int dp[1001][1001];

int main(){

  int W, N, l;
  l = 1;
  while(cin >> W >> N){
    if( W == 0 ) break;
    vector<int> v(N);
    vector<int> w(N);
    for(int i=0; i<N; i++) scanf("%d,%d", &v[i], &w[i]);

    for(int i=0; i<1001; i++) for(int j=0; j<1001; j++) dp[i][j] = 0;

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

    // for(int i=0; i<W; i++) cout << i << " " << dp[N][i] << endl;
    // cout << dp[N][W] << endl;
    int ansi = 0;
    int ans = 0;
    for(int i=0; i<W; i++){
      if( ans < dp[N][i] ){
        ans = dp[N][i];
        ansi = i;
      }
    }

    cout << "Case " << l << ":" << endl;
    cout << ansi << endl;
    cout << ans << endl;

    l++;
  }




  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  for(int c=1,w; cin>>w,w; c++){
    int n,tr[1001][2] = {0};
    cin >>n;
    for(int i=1; i<=n; i++){
      scanf("%d,%d",&tr[i][0],&tr[i][1]);
    }
    int dp[1001][1001] = {0};
    for(int i=0; i<=n; i++){
      for(int j=0; j<=w; j++){
	if(j>=tr[i][1]){
	  dp[j][i] = max(dp[j][i-1],dp[j-tr[i][1]][i-1]+tr[i][0]);
	}
      }
    }
    int pr = dp[w][n], val;
    for(int j=0; j<=w; j++){
      if(pr == dp[j][n]){val = j;break;}
    }
    cout <<"Case "<<c<<":"<<endl;
    cout <<pr<<endl<<val<<endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#define in(a) scanf("%d",&a)
using namespace std;
int main(){
  int w,n,a,b,ca=0;
  
  while(in(w) && w != 0){
    in(n);
    int dp[n][w+1];
    int wait[n][w+1];
    fill_n(*dp,n*(w+1),0);
    fill_n(*wait,n*(w+1),0);
    for(int i = 0;i < n;i++){
      scanf("%d,%d",&a,&b);
      if(i == 0){
	dp[i][b] = a;
	wait[i][b] = b;
      }
      else {
	for(int j = 0;j <= w;j++){
	  if(j < b){
	    dp[i][j] = dp[i-1][j];
	    wait[i][b] = wait[i-1][b];
	  }
	  else if(dp[i-1][j] > dp[i-1][j-b]+a){
	    dp[i][j] = dp[i-1][j];
	    wait[i][j] = wait[i-1][j];
	  }
	  else if(dp[i-1][j] <= dp[i-1][j-b]+a){
	    dp[i][j] = dp[i-1][j-b]+a;
	    wait[i][j] = wait[i-1][j-b]+b;
	  } 
	}
      }
    }
    ca++;
    printf("Case %d:\n",ca);
    printf("%d\n%d\n",dp[n-1][w],wait[n-1][w]);
  }

  return (0);
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>

using namespace std;

vector< vector<int> > dp;
vector<int> w,v;
int n,weight;

int rec(int i,int j){

	if(dp[i][j]>=0) return dp[i][j];

	int res;
	if(i==n) res=0;
	else if(j<w[i]) res=rec(i+1,j);
	else res=max(rec(i+1,j),rec(i+1,j-w[i])+v[i]);

	return dp[i][j]=res;
}

int main(){

	int count=1;
	cin>>weight;
	while(weight!=0){
		cin>>n;
		w.resize(n); v.resize(n);
		for(int i=0;i<n;i++) scanf("%d,%d",&v[i],&w[i]);

		dp = vector< vector<int> >(n+1);
		for(int i=0;i<n+1;i++) dp[i]=vector<int>(weight+1,-1);

		for(int i=0;i<weight+1;i++) dp[n][i]=0;

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

		cout<<"Case "<<count<<":"<<endl<<dp[0][weight]<<endl;
		for(int i=weight-1;i>=0;i--){
			if(dp[0][i]!=dp[0][weight]){
				cout<<i+1<<endl;
				break;
			}
		}
		count++;
		cin>>weight;
	}


	return 0;

}

C++: #include <iostream>
#include <stdio.h>

void update(int *dst, int src) {
	if (*dst < src) *dst = src;
	return;
}

int main() {
	for (int cnt = 1; 1; cnt++) {
		// 入力
		int W, N, Value[1000], Weight[1000];
		std::cin >> W;
		if (W == 0) return 0;
		std::cin >> N;
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &Value[i], &Weight[i]);
		}
		// 演算
		int dp[101][101];
		for (int i = 0; i <= W; i++) {
			dp[0][i] = 0;
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				update(&dp[i + 1][j], dp[i][j]);
				if (j + Weight[i] <= W) {
					update(&dp[i + 1][j + Weight[i]], dp[i][j] + Value[i]);
				}
			}
		}
		// 出力
		std::cout << "Case " << cnt << ":" << std::endl;
		int MaxValue = 0, MaxIndex = 0;
		for (int i = 0; i <= W; i++) {
			if (MaxValue < dp[N][i]) {
				MaxValue = dp[N][i];
				MaxIndex = i;
			}
		}
		std::cout << MaxValue << std::endl;
		std::cout << MaxIndex << std::endl;
	}
}

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

typedef pair<int, int> PII;

int N;
int W;
vector<int> vv;
vector<int> vw;

PII solve() {
  vector<vector<int> > dp(N+1, vector<int> (W+1, 0));

  for (int i = N - 1; i >= 0; i--) {
    for (int w = 0; w <= W; w++) {
      if (w < vw[i])
	dp[i][w] = dp[i + 1][w];
      else
	dp[i][w] = max(dp[i + 1][w],
		       dp[i + 1][w - vw[i]] + vv[i]);
    }
  }

  auto v_max = dp[0][W];
  auto w_min = W;
  for (int i = 0; i < W; i++) {
    if (dp[0][i] == v_max) {
      w_min = i;
      break;
    }
  }
  
  return make_pair(v_max, w_min);
}

int main() {
  int c = 1;
  while (true) {
    cin >> W;
    if (W == 0) break;
    
    vv.clear();
    vw.clear();
    cin >> N;
    vv.resize(N);
    vw.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("\n%d,%d", &vv[i], &vw[i]);
    }

    cout << "Case " << c++ << ":" << endl;
    auto result = solve();
    cout << result.first << endl << result.second << endl;
  }
}