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 <algorithm>
using namespace std;

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

void solve(){
    for(int i = 0; i < N; i++){
        for(int j = 0; j <= W; j++){
            dp[i+1][j] = 0;
        }
    }
    for(int i = 0; i < N; i++){
        dp[i+1][w[i]] = v[i];
        for(int j = 0; j <= W; j++){
            if( j < w[i] ) dp[i+1][j] = dp[i][j];
            else dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i]);
        }
    }
    int c,v = 0;
    for(int i = 0; i <= W; i++){
        if( dp[N][i] > v ){
            v = dp[N][i];
            c = i;
        }
    }
    printf("Case %d:\n%d\n%d\n", cnt, v, c);
}

int main(){
    cnt = 0;
    while( true ){
        cnt++;
        scanf("%d", &W);
        if( !W ) break;
        scanf("%d", &N);
        for(int i = 0; i < N; i++){
            scanf("%d,%d", v+i, w+i);
        }
        solve();
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

typedef pair<int, int> obj;

int main(){
  int N, W, weight, value;
  char ch;
  int case_num = 1;

  while(1){
    cin >> W;
    if(W == 0) break;
    cin >> N;

    //printf("W = %d N = %d\n", W, N);

    vector<obj> obj_list;
    int dp[2][1010];
    memset(dp, -1, sizeof(dp));
    
    for(int i = 0; i < N; i++){
      cin >> value >> ch >> weight;
      obj_list.push_back(obj(value, weight));
      //printf("value = %d weight = %d\n", value, weight);
    }

    dp[0][0] = 0;
    dp[1][0] = 0;

    for(int i = 0; i < N; i++){
      obj o = obj_list[i];

      for(int j = 0; j <= W; j++){
        if(dp[(i+1)%2][j] != -1){
          dp[i%2][j] = max(dp[i%2][j], dp[(i+1)%2][j]);
          if(j + o.second <= W){
            dp[i%2][j+o.second] = max(dp[i%2][j+o.second], dp[(i+1)%2][j] + o.first);
          }
        }
      }
    }
    int answer = 0;
    int answer_weight = 0;

    for(int i = 0; i <= W; i++){
      if(answer < dp[(N+1)%2][i]){
        answer = dp[(N+1)%2][i];
        answer_weight = i;
      }
      if(answer < dp[N%2][i]){
        answer = dp[N%2][i];
        answer_weight = i;
      }
    }

    cout << "Case " << case_num << ":" << endl;
    cout << answer << endl;
    cout << answer_weight << endl;

    case_num++;
  }

  return 0;
}

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

struct treasure_t{
    int value;
    int weight;
    treasure_t( const int v, const int w ){
        value = v;
        weight = w;
    }
};

vector<treasure_t> treasures;
int W, maxW, maxV;

void solve( int n, int w, int v ){
    if( w > W ) return;
    if( n==treasures.size() ){
        if( (v > maxV) || (v==maxV && w<maxW) ){
            maxV = v;
            maxW = w;
        }
        return;
    }
    solve(n+1,w+treasures[n].weight,v+treasures[n].value);
    solve(n+1,w,v);
}

int main(){
    int cnt = 1;
    while( cin >> W, W ){
        int n;
        cin >> n;
        treasures.clear();
        maxW = 0;
        maxV = 0;
        for( int i=0; i<n; i++ ){
            int v, w;
            scanf("%d,%d", &v,&w);
            treasure_t t(v,w);
            treasures.push_back(t);
        }
        solve(0,0,0);
        cout << "Case " << cnt << ":" << endl;
        cout << maxV << endl;
        cout << maxW << endl;
        cnt++;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)

int caseId = 1;

void solve42(int W, int N) {
    int dp[1001] = {}; // dp[i]: maxvalue under weight i
    REP(i,N) {
        int a, b;
        char c;
        cin >> a >> c >> b;
        for(int j = W; j >= b; j--) {
            dp[j] = max(dp[j], dp[j-b] + a);
        }
    }
    int value = 0, weight = 0;
    REP(i,W) {
        if (value < dp[i]) {
            value = dp[i];
            weight = i;
        }
    }
    cout << "Case " << caseId << ":" << endl << value << endl << weight << endl;
}

int main() {
    int W, N;
    while (cin >> W >> N, W) {
        solve42(W, N);
        caseId++;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int w,n;
pair<int,int> takara[1002];
int dp[1002][10002];
int saidai=0,banti=0;
char ch;
int now=1;
main(){
  while(1){
    cin>>w;
    if(w==0){
      break;
    }
    cin>>n;
    for(int i=1;i<=n;i++){
      cin>>takara[i].first>>ch>>takara[i].second;
    }
    saidai=0;
    banti=0;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=w;j++){
	if(i==0){
	  dp[i][j]=0;
	}
	else if(j-takara[i].second<0){
	  dp[i][j]=dp[i-1][j];
	}
	else{
	  dp[i][j]=max(dp[i-1][j],dp[i-1][j-takara[i].second]+takara[i].first);
	}
	if(i==n&&saidai<dp[i][j]){
	  saidai=dp[i][j];
	  banti=j;
	}
      }
    }
    cout<<"Case "<<now<<":"<<endl<<saidai<<endl<<banti<<endl;
    now++;
  }
}