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++: //============================================================================
// 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>

using namespace std;

class item
{
public:
	int value;
	int weight;
	bool flag;
	item()
	{
		flag=false;
	}
};

bool input(int *c, int *n, item it[1000])
{
	cin>>*c;
	if(*c==0)
	{
		return false;
	}
	cin>>*n;
	for(int i=0;i<*n;i++)
	{
		char t;
		cin>>it[i].value>>t>>it[i].weight;
	}
	return true;
}

item max(item a, item b)
{
	if(a.value>b.value)
	{
		return a;
	}
	else
	{
		return b;
	}
}

item add(item a, item b)
{
	a.value+=b.value;
	a.weight+=b.weight;
	return a;
}

item table[1000][1000];

item solve(int c, int n, item it[1000])
{
	if(table[c][n].flag!=true){
		if(c==0||n==0)
		{
			table[c][n].value=0;
			table[c][n].weight=0;
		}
		else if(c<it[n-1].weight)
		{
			table[c][n]=solve(c,n-1,it);
		}
		else
		{
			table[c][n]=max(solve(c,n-1,it),add(solve(c-it[n-1].weight,n-1,it),it[n-1]));
		}
		table[c][n].flag=true;
	}
	return table[c][n];
}

int main(void)
{
	int c,n;
	item it[1000];
	for(int i=1;input(&c,&n,it);i++)
	{
		item ans=solve(c,n,it);
		cout<<"Case "<<i<<':'<<endl<<ans.value<<endl<<ans.weight<<endl;
	}
}

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

int T, N, V[1010], W[1010];

int main() {

	for ( int t = 1; scanf( "%d", &T ) && T; t++ ) {
	
		scanf( "%d", &N );
	
		for ( int i = 0; i < N; i++ ) { scanf( "%d,%d", &V[i], &W[i] ); }
	
		int A[1010][1010] = {{0}};
	
		for ( int i = 1; i <= N; i++ ) {
		
			for ( int j = 0; j <= T; j++ ) {
	
				if ( j < W[i-1] ) {
	
					A[i][j] = A[i-1][j];
	
				} else {
	
					A[i][j] = max( A[i-1][j], A[i-1][j-W[i-1]] + V[i-1] );
					
				}
	
			}
	
		}
		
		/*for ( int i = 1; i <= N; i++ ) {
		
			printf( "i = %d\n", i );
		
			for ( int j = 0; j <= T; j++ ) {
			
				printf( "\tj = %d\t%d\n", j, A[i][j] );
			
			}
		
		}*/
		
		int best = -1, best_w = -1;
		
		for ( int j = 0; j <= T; j++ ) {
		
			if ( A[N][j] > best ) {
			
				best = A[N][j];
				best_w = j;
				
			}
		
		}
		
		printf( "Case %d:\n%d\n%d\n", t, best, best_w );
	
	}

}

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

using namespace std;

const int MAX_N = 1020;
const int MAX_W = 1020;

typedef struct {
  int v;
  int w;
} DP;

// ??\???
int W, N;
int v[MAX_N], w[MAX_N];

// ?????¢???????????????
DP dp[MAX_N][MAX_W]; 

// i????????\?????§j????????????????????§??°??????????????????????????????????¨?????????§?????¨???????????????????????????
DP rec(int i, int j) {
  if (dp[i][j].v != -1)
    return dp[i][j];

  int res;
  int weight;
  if (i == N) {
    res = 0;
    weight = 0;
  } else if (w[i] > j) {
    DP result = rec(i+1, j);
    res = result.v;
    weight = result.w;
  } else {
    DP result1 = rec(i+1, j);
    DP result2 = rec(i+1, j-w[i]);
    result2.v += v[i];
    result2.w += w[i];

    if (result1.v > result2.v) {
      res = result1.v;
      weight = result1.w;
    } else if (result1.v < result2.v) {
      res = result2.v;
      weight = result2.w;
    } else {
      res = result1.v;
      weight = min(result1.w, result2.w);
    }
   
  }

  dp[i][j].v = res;
  dp[i][j].w = weight;
  return dp[i][j];
}

DP solve() {
  for (int i=0; i<MAX_N; i++) {
    for (int j=0; j<MAX_W; j++) {
      dp[i][j].v = -1;
    }
  }

  return rec(0, W);
}

int main() {
  int counter = 0;
  
  while(scanf("%d", &W), W) {
    counter++;
    
    scanf("%d", &N);
    for (int i=0; i<N; i++) {
      scanf("%d, %d", &v[i], &w[i]);
    }

    DP result = solve();
    printf("Case %d:\n", counter);
    printf("%d\n%d\n", result.v, result.w);
  }
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
#include<cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VII;
typedef pair<int,int> PII;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	int w,n,index = 1;
	int ans_v = 0,ans_w = 0;
	VI value,weight;

	while(1){
		cin >> w;
		if(w == 0) break;
		cin >> n;

		VII dp(n + 1,VI (w + 1,-1));
		dp[0][0] = 0;

		rep(i,n){
			int a,b;
			char ch;
			cin >> a >> ch >> b;
			value.pb(a);
			weight.pb(b);
		}

		rep(i,n){
			rep(j,w + 1){
				if(dp[i][j] == -1) continue;
				dp[i + 1][j] = max(dp[i + 1][j],dp[i][j]); //valle[i]テ」ツつ津ィツカツウテ」ツ?陛」ツ?ェテ」ツ??・ツ?エテ・ツ青?
				if(j + weight[i] <= w){ //value[i]テ」ツつ津ィツカツウテ」ツ?凖・ツ?エテ・ツ青?
					dp[i + 1][j + weight[i]] = max(dp[i + 1][j + weight[i]],value[i] + dp[i][j]);
					
				}
			}
		}

		rep(i,dp[n].size()){
			if(dp[n][i] > ans_v){
				ans_v = dp[n][i];
				ans_w = i;
			}
		}		

		cout << "Case " << index++ << ':' << endl;
		cout << ans_v << endl << ans_w << endl;
	}

	return 0;
}