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>
#include<algorithm>
using namespace std;
#define latte WORLD

int N,W,tv[1000],tw[1000];
int memo[1000][1001],ansv,answ;

int latte(int n,int v,int w){
    if(w>W) return 0;
    if(n==N){
        if(ansv<v || (ansv==v && answ>w)){
            ansv = v; answ = w;
        }
        return v;
    }
    if(memo[n][w]!=-1) return memo[n][w];
    int ans=0;
    memo[n][w] = max(latte(n+1,v+tv[n],w+tw[n]),latte(n+1,v,w));
    return memo[n][w];
}

int main(){
    for(int ds=1;cin>>W,W;ds++){
        cin>>N;
        for(int i=0;i<N;i++) scanf("%d,%d",&tv[i],&tw[i]);
        fill_n((int*)memo,1000*1001,-1); ansv = answ = 0;
        latte(0,0,0);
        printf("Case %d:\n%d\n%d\n",ds,ansv,answ);
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

#define MAX_N 1000
#define MAX_W 10000

int dp[MAX_W][MAX_N], W, N, value[MAX_N], w[MAX_N];

int main(){
	int count = 0;

	while(true){
		++count;
		
		if(w == 0)
			break;

		scanf("%d%d", &W, &N);

		for(int i = 0; i < N; ++i){
			for(int j = 0; j <= W; ++j)
				dp[j][i] = 0;
		}//initialize

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

		for(int i = N - 1; i >= 0; ++i){
			for(int j = 0; j < W; ++j){
				if(j < w[i])
					dp[i][j] = dp[i + 1][j];
				else
					std::max(dp[i + 1][j], dp[i + 1][j - w[i]] - value[i]);
			}
		}
		printf("count %d:%d?\n", count, dp[0][W]);
	}
	return 0;
}

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

using namespace std;

const int MAX_w = 1000;
const int MAX_v = 10000;

int dp[MAX_w + 1][MAX_v + 1];


int n, wet ;
int w[MAX_w], v[MAX_v];

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
int rec(int i, int j) {
  int res;
  if(dp[i][j] != -1){
  	
	return dp[i][j];
  }

  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(
        rec(i + 1, j),
        rec(i + 1, j - w[i]) + v[i]
    );
  }

  return res;
}

void solve(int ca) {
  // 0番目以降で容量W以下の場合の結果を表示する
  int wmin;
  
  memset(dp, -1, sizeof(dp));
  
  cout << "Case" << " " << ca << ":" <<endl; 
  
  int ans =  rec(0, wet) ;
  cout << ans << endl;
  
  for (int w = wet -1 ; w>=0 ; w--){
  	if( ans != rec(0,w)){
		 wmin = w+1;
		 break;
  	}
  }
  
  cout << wmin << endl;  
  
}


int main(){
	
	char x;
	int ca = 0;
	
	while(1){
			
		ca++;
		cin >> wet;
		if(wet == 0 ) break;
		cin >> n;
		
		for(int i = 0 ;  i<n ; i++){
			
			cin >> v[i] >> x >> w[i];
		}
		
		
		 solve(ca);
		
	}
	
	return 0;
	
}

C++: 
//============================================================================
// Name        : AOJ0042.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

#define MAX_N 1000
#define MAX_M 10000

int n, W;
int w[MAX_N] = { 0 }, v[MAX_N] = { 0 };

short dp[MAX_N + 1][MAX_M + 1] = { { 0 } };

void solve() {

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i]) {
        dp[i + 1][j] = dp[i][j];
      } else {
        dp[i + 1][j] = (short)max((int)dp[i][j], dp[i][j - w[i]] + v[i]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W; j++) {
      //printf("%5d", dp[i][j]);
    }
    //cout << endl;
  }
  //cout << endl;
  cout << dp[n][W] << endl;
  int i;
  for (i = W; i >= 0; i--) {
    if (dp[n][W] != dp[n][i]) break;
  }
  cout << i + 1 << endl;

}

int main() {

  int kase = 1;
  char str[11];
  cin >> W;

  while (W != 0) {
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> str;

      int j = 0;

      v[i] = 0;
      w[i] = 0;

      while (str[j] != ',') {
        v[i] = v[i] * 10;
        v[i] = v[i] + (str[j] - '0');
        j++;
      }

      j++;

      while (str[j] != '\0') {
        w[i] = w[i] * 10;
        w[i] = w[i] + (str[j] - '0');
        j++;
      }
    }
    /*
     for (int i = 0; i < n; i++) {
     cout << v[i] << endl;
     cout << w[i] << endl;
     }
     */
    cout << "Case " << kase << ":" << endl;
    solve();

    kase++;
    cin >> W;
  }

  return 0;
}

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

using namespace std;

#define WMAX 1000
#define NMAX 1000
int main(void)
{
    int W, n;
    int cyc = 0;
    while (cin >> W && W && cin >> n) {
        cyc++;
        int dp[WMAX+1][NMAX+1] = {};
        for (int i = 0; i < n; i++) {
            int v, w;
            scanf("%d,%d", &v, &w);
            for (int j = 0; j < W+1; j++) {
                dp[j][i+1] = max(dp[j][i], (j-w >= 0 ? dp[j-w][i] + v : 0));
            }
        }
        for (int i = 0; i < n + 1; i++) {
            for (int j = 0; j < W+1; j++) {
                cout << dp[j][i] << " ";
            }
            cout << endl;
        }
        cout << "Case " << cyc << ":" << endl;
        for (int j = 0; j < W+1; j++) {
            if (dp[j][n] == dp[W][n]) {
                cout << dp[j][n] << endl << j << endl;
                break;
            }
        }
    }
}

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

int dp[1000];
int main(){
	int w, n, ans_w = 0, ans_val = 0;
	for (int dataset = 1; cin >> w, w; dataset++){
		int t_w, t_val;
		cin >> n;
		for (int i = 0; i < w; i++) dp[i] = -1;
		dp[0] = 0;
		for (int i = 0; i < 5000000000; i++){}
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &t_val, &t_w);
			for (int j = w - 1; j >= 0; j--){
				//printf("%d ", dp[j]);
				if (dp[j] == -1 || j + t_w >= w || dp[j + t_w] > dp[j] + t_val)
					continue;
				dp[j + t_w] = dp[j] + t_val;
			}
			//puts("");
		}
		for (int i = 0; i < w; i++){
			//printf("%d ", dp[i]);
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		//puts("");
		printf("Case %d:\n%d\n%d\n", dataset, ans_val, ans_w);
	}
	return 0;
}