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

int main(void){
  int n,mw,cnt = 1;
  while(cin>>mw,mw!=0){
    cin>>n;
    printf("Case %d:\n",cnt++);
  int w[100],v[100];
  int dp[100][10000];
  int dp1[100][10000];
  for(int i = 0 ; i < n ; i ++)
    scanf("%d,%d",&v[i],&w[i]);
  for(int j = w[0] ; j < mw + 1 ; j ++)
    dp[0][j] = v[0],dp1[0][j] = w[0];
  for(int i = 1 ; i < n ; i ++){
    for(int j = 0 ; j < w[i] ; j ++){
      dp[i][j] = dp[i-1][j];
      dp1[i][j] = dp1[i-1][j];
    }
    for(int j = w[i] ; j < mw + 1 ; j ++){
      dp[i][j] = max(dp[i-1][j] , dp[i-1][j-w[i]] + v[i]);    
      if(dp[i-1][j] < dp[i-1][j-w[i]] + v[i]){
	// update
	dp1[i][j] = dp1[i-1][j-w[i]] + w[i];
      }else{
	dp1[i][j] = dp1[i-1][j];
      }
    }
  }
  int res = -1,resw = -1;
  for(int i = 0 ; i < mw+1 ; i ++){
    if(res < dp[n-1][i]){
      res = dp[n-1][i];
      resw = dp1[n-1][i];
    }
  }
  /*
  for(int i = 0 ; i < n  ; i ++){
    for(int j = 0 ; j < mw + 1; j ++)
      cout<<dp1[i][j]<<" ";
    cout<<endl;
  }
  //*/
  cout<<res<<endl<<resw<<endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <math.h>
#include <map>
#include <queue>
#include <string>
using namespace std;

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

int main(){
	int cn=0;
	while(cin>>W){
		if(W==0)return 0;
		cn++;
		cin>>N;
		for(int i=0;i<N;i++)cin>>v[i]>>c>>w[i];
		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]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
			}
		}
		int V=dp[N][W],w2=W;
		while(dp[N][w2]==V&&w2>0)w2--;
		cout<<"Case "<<cn<<":"<<endl<<dp[N][W]<<endl<<w2<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)):((m)-(n))),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define loop for(;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string,int> Dictionary;
const Real PI = acos(-1);

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

int main() {

  int case_index = 1;
  loop {
    int C, n; cin >> C >> n;
    if (!C) break;
    cout << "Case " << case_index << endl;
    ++case_index;

    vi cs(n);
    vi xs(n);
    char comma;
    rep (i, n) cin >> xs[i] >> comma >> cs[i];

    int dp[C+1];
    rep (c, C+1) dp[c] = 0;

    rep (i, n) {
      for (int c = C - cs[i]; c >= 0; --c) {
        int c2 = c + cs[i];
        if (dp[c2] < dp[c] + xs[i]) {
          dp[c2] = dp[c] + xs[i];
        }
      }
    }

    int mx = 0,
        mc = -1;
    rep (c, C + 1) {
      if (mx < dp[c]) {
        mx = dp[c];
        mc = c;
      }
    }
    cout << mx << endl;
    cout << mc << endl;
  }

  return 0;
}

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

struct treasure{
	int value;
	int weight;
};
bool comp(treasure t1, treasure t2){
	if(t1.weight <= t2.weight) return true;
	else return false;
}

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	while(cin >> w){
		int n;
		int cost[maxw + 1];
		int best[maxw + 1];
		for(int i = 0; i <= maxw; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		vector<treasure> treasures;
		cin >> n;
		for(int i = 0; i < n; i++){
			treasure t;
			char c;
			cin >> t.value >> c >> t.weight;
			treasures.push_back(t);
		}
		sort(treasures.begin(), treasures.end(), comp);
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= treasures[j].weight)
					if(cost[k] < cost[k - treasures[j].weight] + treasures[j].value)
					{
						cost[k] = cost[k - treasures[j].weight] + treasures[j].value;
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;(cost[w] == cost[w-1]) && (w > 0); w--);
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - treasures[best[l]].weight >= 0;){
			if(l - treasures[best[l]].weight >= 0){
				total += treasures[best[l]].weight;
				l -= treasures[best[l]].weight;
			}
		}
		cout << total << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;

int main(){
	int W,N,cou=0;
	int tr[1001],we[1001];
	while(cin >> W , W){
		cou++;
		cin >> N;
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		P dp[100][1001];
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				dp[i][j] = P(0,0);
			}
		}
		dp[0][0] = P(0,0);
		for(int i=1;i<=N;i++){
			dp[i][0] = P(0,0);
			for(int j=0;j<i;j++){
				if(dp[i-1][j].second+we[i] <= W){
					P p;
					p = P(dp[i-1][j].first+tr[i],dp[i-1][j].second+we[i]);
					if(p.first == dp[i-1][j+1].first){
						if(p.second > dp[i-1][j+1].second) dp[i][j+1] = p;
						else dp[i][j+1] = dp[i-1][j+1];
					}
					else if(p.first > dp[i-1][j+1].first){
						dp[i][j+1] = p;
					}
				}
			}
		}

		cout << "Case " << cou << ":" << endl;
		cout << dp[N][N].first << endl;
		cout << dp[N][N].second << endl;

	}
}