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<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  int n,W;
  int cnt=1;
  while(cin>>W,W){
    cin>>n;
    vector<int>v(n);
    vector<int>w(n);
    for(int i=0;i<n;i++)
      scanf("%d,%d",&v[i],&w[i]);
    int dp[n+1][W+1]={};
    for(int i=n-1;i>=0;i--){
      for(int j=0;j<=W;j++){
	if(j-w[i]<0)dp[i][j]=dp[i+1][j];
	else if(j-w[i]>=0)dp[i][j]=max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]);
      }
    }
    int ma=dp[0][W];
    int weight=W;
    for(int i=W;i>=0;i--){
      if(ma==dp[0][i]){
      weight=i;
      }else
	break;
    }
    cout<<"Case"<<" "<<cnt<<':'<<endl;
    cout<<dp[0][W]<<endl;
    cout<<weight<<endl;
    cnt++;
  }
  return 0;
}

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

int dp[1001][1001];
int v[1001], w[1001];

int main(){
	int W;

	int k = 1;
	while (std::cin >> W, W){
		int n;

		std::cin >> n;
		char a;
		for (int i = 0; i < n; i++)std::cin >> v[i] >> a >> w[i];

		memset(dp, -1, sizeof(dp));

		dp[1][0] = 0;
		dp[1][w[0]] = v[0];
		for (int i = 1; i < n; i++){
			for (int j = 0; j <= W; j++){

				if (dp[i][j] != -1){
					dp[i + 1][j] = std::max(dp[i + 1][j], dp[i][j]);
					if (j + w[i] <= W)dp[i + 1][j + w[i]] = std::max(dp[i + 1][j + w[i]], dp[i][j] + v[i]);
				}
			}
		}

		int max = 0, id = 0;
		for (int i = 1; i <= W; i++){
			if (max < dp[n][i])max = dp[n][i], id = i;
		}
		std::cout << "Case " << k << ":" << std::endl << max << std::endl << id << std::endl;

		k++;
	}

	return 0;
}

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

#define MAX_N 1500
#define MAX_W 10000

int dp[MAX_N][MAX_W];
int W, N, v[MAX_N], w[MAX_N];

int main() {
	while (true) {
		memset(dp, 0, sizeof(dp));
		cin >> W;
		if (W == 0) { break; }
		cin >> N;
		for (int i = 0; i < N; i++)
		{
			scanf("%d,%d", &v[i], &w[i]);
		}
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j <= W; j++)
			{
				dp[i + 1][j] = max(dp[i][j], dp[i + 1][j]);
				if (j + w[i] < W)
				{
					dp[i + 1][j + w[i]] = max(dp[i][j] + v[i], dp[i + 1][j + w[i]]);
				}
			}
		}
		int maxn = 0;
		for (int i = 0; i < MAX_N; i++)
		{
			for (int j = 0; j < MAX_W; j++)
			{
				maxn = max(maxn, dp[i][j]);
			}
		}
		cout << maxn << endl;
	}
}

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

struct Treasure{
	int Weight;
	int Worth;
};
vector<int> TempMaxWorth;

int GetMaxWorth(const int MaxWeight,vector<Treasure> nowTreasureCollection, vector<Treasure>::iterator nowIterator,const vector<Treasure>& AllTreasureCollection){
	 vector<Treasure>::iterator rootIterator(nowIterator);
	for (; nowIterator!=AllTreasureCollection.end(); ++nowIterator){
		nowTreasureCollection.push_back(*(nowIterator));
		const int NowWorth = accumulate(nowTreasureCollection.cbegin(),
			nowTreasureCollection.cend(),
			0,
			[](const int q, const Treasure& p){return q + p.Worth; }
		);
		const int NowWeight = accumulate(nowTreasureCollection.cbegin(),
			nowTreasureCollection.cend(),
			0,
			[](const int q, const Treasure& p){return q + p.Weight; });
		if (NowWeight <= MaxWeight&&NowWorth > TempMaxWorth[NowWeight]){
			TempMaxWorth[NowWeight] = NowWorth;
			replace_if(TempMaxWorth.begin() + NowWeight, TempMaxWorth.end(), [=](int n){return NowWorth > n; }, NowWorth);
			if((++nowIterator)!=AllTreasureCollection.end())GetMaxWorth(MaxWeight, nowTreasureCollection, nowIterator,AllTreasureCollection);
			nowIterator--;
		}
		nowTreasureCollection.pop_back();
	}
	((nowIterator)) = (rootIterator);
	return 0;
}

int main(){
	int MaxWeight;
	int TreasureNum;
	for (int i = 1; cin >> MaxWeight;++i){
		if (MaxWeight == 0)return 0;
		cin  >> TreasureNum;
		TempMaxWorth.resize(MaxWeight + 1);
		fill(TempMaxWorth.begin(), TempMaxWorth.end(), 0);
		vector<Treasure> AllTreasureCollection;
		for (int j = 0; j < TreasureNum; ++j){
			Treasure T;
			cin >> T.Worth;
			cin.get();
			cin >> T.Weight;
			AllTreasureCollection.push_back(T);
		}	
		vector<Treasure> NowTreasureCollection = {};
		GetMaxWorth(MaxWeight, NowTreasureCollection, AllTreasureCollection.begin(), AllTreasureCollection);
		cout << "Case " << i << " :" << endl << TempMaxWorth[MaxWeight] << endl << find(TempMaxWorth.cbegin(), TempMaxWorth.cend(), TempMaxWorth[MaxWeight]) - TempMaxWorth.cbegin() << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
#include <bitset>
#define INF	100000000
#define pb	push_back
#define fi first
#define sec second
#define SS stringstream
using namespace std;
typedef pair<int, int> P;
typedef long long int	ll;

int w, n;
int t = 0;
int dp[1001][1001];
vector<P> vec;

int rec(int i, int j){
	if(dp[i][j] >= 0) return dp[i][j];
	int res;
	if(i == n) res = 0;
	else if(j < vec[i].sec) res = rec(i+1, j);
	else res = max(rec(i+1, j), rec(i+1, j-vec[i].sec)+vec[i].fi);
	return dp[i][j] = res;
}

int main(){
	while(scanf("%d", &w)){
		t++;
		if(w == 0) break;
		memset(dp, -1, sizeof(dp));
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			int temp1, temp2;
			char trash;
			scanf("%d%c%d", &temp1, &trash, &temp2);
			vec.pb(P(temp1, temp2));
		}
		rec(0, w);
		printf("Case %d:\n", t);
		printf("%d\n", rec(0, w));
		for(int i = 1; i <= w; i++){
			if(dp[0][w-i] != dp[0][w]){
				printf("%d\n", w-i);
				break;
			}
		}
		vec.clear();
	}
}