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 "math.h"
#include "stdio.h"
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>

using namespace std;


/** Problem0042 : A Thief
 Time Limit Exceeded **/

#define MAX_N 1100
#define MAX_W 1100

int W, N, tre[1100][2]={0}, solW, solN;
int dp[MAX_N][MAX_W];

pair<int, int> solve(int i, int j)
{
	if (dp[i][j] >= 0)
		return pair<int, int>(dp[i][j], j);
	
	pair<int, int> res;
	if (i == N) {
		res = pair<int, int>(0, W-j);
	} else if (j < tre[i][1]) {
		res = solve(i+1, j);
	} else {
		//res.first = max(solve(i+1, j).first, solve(i+1, j-tre[i][1]).first+tre[i][0]);
		
		if (solve(i+1, j).first > solve(i+1, j-tre[i][1]).first+tre[i][0]) {
			res = solve(i+1, j);
		} else if (solve(i+1, j).first < solve(i+1, j-tre[i][1]).first+tre[i][0]) {
			res = pair<int, int>(solve(i+1, j-tre[i][1]).first+tre[i][0], solve(i+1, j-tre[i][1]).second);
		} else {
			res = pair<int, int>(solve(i+1, j).first, min(solve(i+1, j).second, solve(i+1, j-tre[i][1]).second));
		}
	}
	return res;
}

int main()
{
	int set=0; char cam;
	while (1) {
		set++;
		
		cin >> W;
		solW = 0; solN = 0;
		if (W==0) break;
		cin >> N;
		
		memset(dp, -1, sizeof(dp));
		
		for (int i=0; i<N; i++) {
			cin >> tre[i][0] >> cam >> tre[i][1];
		}
		
		pair<int, int> sol = solve(0, W);
		
		cout << "Case " << set << ":" << endl;
		cout << sol.first << endl;
		cout << sol.second << endl;
	}
	return 0;
}

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

int W;
int w[1001],v[1001];
int N;
int dp[1001][1001];

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]);
    /* 
   int tmp1,tmp2;
    tmp1 = rec(i+1,j);
    tmp2 = rec(i+1,j-w[i]) + v[i];
    if(tmp1 < tmp2){
      res = tmp2;
    }else{
      res = tmp1;
    }
    */
  }
  return dp[i][j] = res;
}


int main(){

  int num = 1;

  while(1){
    cin >> W;
    if(W == 0) break;
    cin >> N;
    //初期化
    /*
    for(int i = 0; i < 1001; i++){
      w[i] = v[i] = -1;
      for(int j = 0; j < 1001; j++)
	dp[i][j] = -1; 
    }
    */
    memset(dp,-1,sizeof(dp));
    memset(w,-1,sizeof(w));
    memset(v,-1,sizeof(v));


    //入力
    char tmp;
    for(int i = 0; i < N; i++) cin >> v[i] >> tmp >> w[i];

    int ans = rec(0,W);

    // dp配列を全て埋める
    for(int i = 0; i < N; i++){
      for(int j = 0; j <= W; j++){
	if(dp[i][j] == -1)
	  rec(i,j);
      }
    }   
  
    cout << "Case "<< num  << ":"<< endl;
    num++;
    cout << ans << endl; 
    int j;
    for( j = 0; dp[0][j] != ans ; j++){}
    cout << j << endl;
    
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
main(){
  int b,c,N,K[1010],P[1010],i,m,j,W,dp[1010],h=1,y,g;
  char d;
  while(1){
    b=0;
    cin >> W;
    for(i=0;i<=W;i++){
      dp[i]=0;
    }
    if(W==0){
      break;
    }
    cin >> N;
    for(i=0;i<N;i++){
      cin >> P[i] >> d >> K[i];
      c=K[i];
      dp[K[i]]=P[i];
      for(j=W;j>=0;j--){
	if(dp[j]!=0){
	  if(dp[c+j]!=0){
	    if(dp[c+j]<dp[c]+dp[j]){
	      dp[c+j]=dp[c]+dp[j];
	    }
	    else{
	      continue;
	    }
	  }
	  if(dp[c+j]==0){
	    dp[c+j]=dp[c]+dp[j];
	  }
	}
      }
    }
    for(i=1;i<=W;i++){
      if(dp[i]>b){
	y=i;
	b=dp[i];
      }
      else{
	continue;
      }
    }
    cout << "Case:" << " " << h << endl;
    cout << b << endl << y << endl;
    h++;
  }
}

C++: #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>

#define pb(a) push_back(a)

using namespace std;

int main(){
  int max_w, n;
  int count = 1;

  while(true){
    scanf("%d", &max_w);
    if(max_w == 0){
      break;
    }

    scanf("%d", &n);
    vector<int> v, w;
    for(int i = 0; i < n; ++i){
      int t1, t2;
      scanf("%d,%d", &t1, &t2);
      v.pb(t1); w.pb(t2);
    }

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

      }
    }

    int ans = 0, index = -1;
    for(int i = 0; i < 1001; ++i){
      if(ans < dp[n][i]){
        ans = dp[n][i];
        index = i;
      }
    }
    printf("Case %d:\n%d\n%d\n", count, ans, index);
    count++;
  }
  return 0;
}

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

int n, W;
int v[1200], w[1200], dp[1200];

int main() {
  int cnt = 1;
  while(cin >> W, W) {
    cin >> n;
    for(int i = 0; i < n; i++) scanf("%d,%d", &v[i], &w[i]);
    
    printf("Case %d:\n", cnt++);
    memset(dp, 0, sizeof(dp));
    for(int i = 0; i < n; i++) {
      for(int j = W; j >= 0; j--) {
	if(j-w[i] >= 0) dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
      }
    }
    cout << dp[W] << endl;

    for(int i = W; i >= 0; i--) {
      if(dp[W] != dp[i]) {
	cout << i+1 << endl;
	break;
      }
    }
  }
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int dp[1001][1001];
int main()
{
	int count=1,i,j,W,n;
	while(1)
	{
		cin >> W;
		if(W==0)return 0;
		scanf("%d",&n);
		memset(dp,0,sizeof(dp));
		int v[n],w[n];
		for(i=0;i<n;i++)
		{
			scanf("%d,%d",&v[i],&w[i]);
		}
		for(i=0;i<n;i++)
		{
			for(j=0;j<=W;j++)
			{
				if(w[i]>j)
				{
					dp[i+1][j]=dp[i][j];
				}
				else
				{
					dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
				}
			}
		}
		for(i=W;i>=0;i--)
		{
			if(dp[n][W]!=dp[n][i])break;
		}
		cout << "Case " << count << ':' << endl;
		cout << dp[n-1][W] << endl;
		cout << i+1 << endl;
		count++;
	}
	return 0;
}