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 <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { char c;for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> c >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int w, n;

// index, weight
int dp[1010][1010];
PArray treasure;

int dfs(int idx, int weight)
{
	if(weight > w || idx >= n) return 0;
	if(dp[idx][weight] != -1) return dp[idx][weight];

	int ret = 0;
	ret = dfs(idx + 1, weight);
	ret = max(ret, dfs(idx + 1, weight + treasure[idx].second));
	return dp[idx][weight] = ret;
}

int main(void)
{
	ios::sync_with_stdio(false);
	int count = 1;
	while(cin >> w && w)
	{
		n = getInt();
		for (int i = 0; i <= n; i++)
		{
			memset(dp[i], -1, sizeof(dp[i]));
		}
		treasure.resize(n);
		readAll(treasure);

		P ans = P(-1, INF);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				if(ans.first == dp[i][j] && ans.second > j)
					ans = P(dp[i][j], j);
				if(ans.first > dp[i][j])
					ans = P(dp[i][j], j);
			}
		}

		cout << "Case " << count << ':' << endl;
		cout << ans.first << endl << ans.second << endl;
		count++;
	}
	return 0;
}

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

using namespace std;

struct Treasure
{

  int value;
  int weight;

  Treasure()
  {
    value = 0;
    weight = 0;
  }

  void operator()( Treasure t)
  {

    cout << t.value << "," << t.weight << endl;

  }

  Treasure& operator+=(const Treasure& t )
  {

    this->value += t.value;
    this->weight += t.weight;
    return *this;

  }

  Treasure operator+(const Treasure& t)
  {
    Treasure tmp;
    tmp.value = this->value + t.value;
    tmp.weight = this->weight + t.weight;
    return tmp;
  }

  Treasure operator-(const Treasure& t)
  {
    Treasure tmp;
    tmp.value = this->value - t.value;
    tmp.weight = this->weight - t.weight;
    return tmp;
  }

};

Treasure& max(Treasure& t1,Treasure& t2)
{

  if(t1.value == t2.value)
    {
      return (t1.weight < t2.weight) ? t1 : t2;
    }
  else
    {
      return (t1.value > t2.value) ? t1 : t2;
    }

}

int N,W;

Treasure memo[1000][1000];

vector<Treasure> treasure(1000);

Treasure Knapsack(int currentWeight,int currentCount)
{

  Treasure t1,t2,f;

  if(memo[currentWeight][currentCount].weight)
    {
      return memo[currentWeight][currentCount];
    }
  else if(N == currentCount)
    {
      Treasure tmp;
      return tmp;
    }
  else{

    t1 = Knapsack(currentWeight,currentCount + 1);

    if( W >= currentWeight + treasure[currentCount].weight)
      {
        t2 = Knapsack(currentWeight + treasure[currentCount].weight,currentCount + 1) + treasure[currentCount];
      }
    else
      {
        t2 = Knapsack(currentWeight,currentCount + 1);
      }

    memo[currentWeight][currentCount] = max(t1,t2);

    return max(t1,t2);

  }

}

int main()
{

  int caseNumber = 0;

 while(cin >> W && W != 0)
   {

     cin >> N;

     for(int i = 0; i < N; ++i)
       { scanf("%d,%d",&treasure[i].value,&treasure[i].weight); }

     Treasure result = Knapsack(0,0);

     memset(memo,0,sizeof(memo));

     treasure.erase(treasure.begin(),treasure.end());

     cout << "Case " << ++caseNumber << ":" << endl;

     cout << result.value << endl << result.weight << endl;

   }

 return 0;

}

C++: #include<cstdio>
#include<cstring>
int W;
int N;
int C[1000];
int G[1000];
int dpc[1000][1000]={};
int dpw[1000][1000]={};
int roop=0;

int min(int a,int b){
	if(a<b) return a;
	else return b;
}
int max(int a,int b){
	if(a>b) return a;
	else return b;
}

int dfsc(int k,int c){
	int res;
	if(dpc[k][c]>-1)
		res=dpc[k][c];
	else if(k>N) res=0;
	else if(c+G[k]<W)
		res=max(dfsc(k+1,c+G[k])+C[k],dfsc(k+1,c));
	else
		res=dfsc(k+1,c);
	dpc[k][c]=res;
	return res;
}
int dfsw(int k,int c){
	int res;
	if(dpw[k][c]>-1)
		res=dpw[k][c];
	else if(k>N) res=0;
	else if(c+G[k]<W)
		res=max(dfsw(k+1,c+G[k])+G[k],dfsw(k+1,c));
	else
		res=dfsw(k+1,c);
	dpw[k][c]=res;
	return res;
}


int main(){
	while(1){
		roop++;
	memset(dpc,-1,sizeof(dpc));
	memset(dpw,-1,sizeof(dpw));
	scanf("%d",&W);
	if(W==0) break;
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		scanf("%d,%d",&C[i],&G[i]);
	}
	int ansc=dfsc(0,0);
	int answ=dfsw(0,0);
	printf("Case %d:\n%d\n%d\n",roop,ansc,answ);
	}
return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> P;
typedef pair<P,int> status;

int main() {
	int W,N,cnt = 1;
	while(true){
		cin >> W;
		if(W==0) return 0;
		cin >> N;
		int val[1001],weight[1001];
		memset(val,0,sizeof(val));
		memset(weight,0,sizeof(weight));
		for(int i = 0;i<N;i++){
			cin >> val[i];
			cin.get();
			cin >> weight[i];
		}
		int dp[1003];
		memset(dp,0,sizeof(dp));
		for(int i = 0;i<N;i++){
			for(int j = W;j >= weight[i];j--){
				if(j-weight[i]<0)continue;
				dp[j] = max(dp[j],dp[j-weight[i]]+val[i]);
			}
		}
		int mini;
		for(int i = W;dp[i] == dp[W];i--) mini = i;
		cout << "Case " << cnt << ":" << endl;
		cout << dp[W-1] << endl;
		cout << mini << endl;
		cnt++;
	}
	return 0;
}