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>
using namespace std;
int field[1001][1001];
int main(void){
    int w;
    int ans_e,ans_w;
    int c = 0;
    while(scanf("%d",&w),w){
        ans_e = 0;
        ans_w = 0;
        c++;
        int i_exp[1001];
        int i_cos[1001];
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++){
            scanf("%d,%d",&i_exp[i],&i_cos[i]);
        }
        for(int i = 1;i <= n;i++){
            for(int j = 0;j <= w;j++){
                field[i][j] = field[i-1][j];
                if(j - i_cos[i] >= 0 && field[i][j] <= field[i-1][j-i_cos[i]]+i_exp[i]) field[i][j] = field[i-1][j-i_cos[i]]+i_exp[i];
            }
        }
        ans_e = field[n][w];
        for(int i = w;i >= 0;i--){
            if(ans_e != field[n][i]){
                ans_w = i+1;
                break;
            }
        }
        printf("Case %d:\n%d\n%d\n",c,ans_e,ans_w);
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int W,N,w[1001],v[1001];
int memo[1001][1001],ans2;
int func(int i,int j){
    if(i==N)return 0;
    if(memo[i][j]!=-1)return memo[i][j];
    if(j+w[i]>W)return func(i+1,j);
    int res;
    if(j<W){
        ans2=max(ans2,j);
    }
    res=max(func(i+1,j),func(i+1,j+w[i])+v[i]);
    return memo[i][j]=res;
}
int main(){
    int ans,cou=0,hinan;
    while(cin>>W,W){
        cin>>N;
        fill(memo[0],memo[0]+1001*1001,-1);
        for(int i=0;i<N;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        ans=func(0,0);
        hinan=ans2;
        for(int i=0;i<N;i++){
            if(w[i]==ans2){
                for(int j=0;j<N;j++){
                    if(v[j]==ans-v[i])hinan=w[j];
                }
                break;
            }
        }
        cout<<ans<<endl<<ans2+hinan<<endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
int main(){
    int w, n;
    int ct = 1;
    while(cin >> w >> n, w){
    int va[n], we[n];
    for(int i = 0; i < n; i++)  scanf("%d, %d",&va[i], &we[i]);
    int dp[1010][1010];
    for(int i = 0; i < 1010; i++)
        for(int j = 0; j < 1010; j++)
            dp[i][j] = 0;
    for(int i = 0; i < n; i++)
        for(int j = 0; j <= w; j++){
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            if(j + we[i] <= w)
                dp[i + 1][j + we[i]] = max(dp[i + 1][j + we[i]], dp[i][j] + va[i]);
        }
    int macs = 0, sumwe = 1010;
    for(int i = 0; i <= n; i++)
        for(int j = 0; j <= w; j++)
            if(dp[i][j] == dp[n][w])
                sumwe = min(j, sumwe);
    cout << "Case " << ct << ":" << endl << dp[n][w] << endl << sumwe << endl;
    ct++;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>

using namespace std;

int N,W;

struct Treasurea
{

  int value;
  int weight;
  int flag;

  Treasurea()
  {
    value = 0;
    weight = 0;
    flag = false;
  }

  Treasurea& operator+=(const Treasurea &obj)
  {
    value += obj.value;
    weight += obj.weight;
  };

};

Treasurea knapsack(vector<Treasurea> &treasurea, int correntWeight,int correntValue, int kind)
{

  if(correntWeight < 0)
    {
      Treasurea temp;
      temp.weight = W - (correntWeight + treasurea[kind - 1].weight);
      temp.value = correntValue - treasurea[kind - 1].value;
      return temp;
    }
  else if(kind == treasurea.size())
    {
      Treasurea temp;
      temp.weight = W - correntWeight;
      temp.value = correntValue;
      return temp;
    }

  Treasurea total;

  for(int i = 0;i < treasurea.size(); ++i)
    {

      if(treasurea[i].flag) continue;

      treasurea[i].flag = true;

      if((total.value < knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).value ) || (total.value == knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).value && total.weight > knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).weight))
         total = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

      treasurea[i].flag = false;

    }

  return total;

}

int main()
{

  int case_number = 0;
  vector<Treasurea> treasurea(N);
  Treasurea result;

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

      cin >> N;

      for(int i = 0; i < N; ++i)
        {

          Treasurea temp;
          scanf("%d,%d",&temp.value,&temp.weight);
          treasurea.push_back(temp);

        }

      result = knapsack(treasurea,W,0,0);

      cout << "Case " << ++case_number << endl << result.value << endl << result.weight << endl;

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

    }

  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--);
		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;
}