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

#define SIZE 1000

int w, n;
int dp[SIZE + 1][SIZE + 1];
int va[SIZE], wa[SIZE];

int main() {
	int u = 0;
	while (cin >> w) {
		if (w == 0) break;
		cin >> n;
		u++;

		char t;
		for (int i = 0; i < n; i++) {
			cin >> va[i] >> t >> wa[i];
		}

		dp[1001][1001] = { 0 };
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= w; j++) {
				if (wa[i] <= j) {
					dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wa[i]] + va[i]);
				} else {
					dp[i][j] = dp[i - 1][j];
				}
			}
		}

		for (int i = w; i >= 0; i--) {
			if (dp[n][w] > dp[n][i]) {
				cout << "Case " << u << ":" << endl;
				cout << dp[n][i + 1] << endl;;
				cout << i + 1 << endl;
				break;
			}
		}
	}
	return 0;
}

C++: #include<stdio.h>
int dp[1001][1001];
int main(void)
{
	int i,j,k,l;
	int w,n;
	int max=0;
	int a[1001],b[1001];
	
	scanf("%d",&w);
	l=1;
	while(w!=0){
	scanf("%d",&n);
		for(i=0;i<=n;i++){
			for(j=0;j<=w;j++){
				dp[i][j]=-1;
			}
		}
		for(i=0;i<n;i++){
			scanf("%d,%d",&a[i],&b[i]);
		}
		for(i=0;i<n;i++){
			dp[i][0]=0;	
			for(j=0;j<=w;j++){
				if(dp[i][j]!=-1){
					if(j+b[i]<=w && dp[i][j]+a[i]>=dp[i+1][j+b[i]]){
						dp[i+1][j+b[i]]=dp[i][j]+a[i];
					}
					if(dp[i+1][j]<dp[i][j]){
						dp[i+1][j]=dp[i][j];
					}
				}
			}
		}
	//	for(i=0;i<n;i++){
		for(j=0;j<=w;j++){
	//		printf("%3d ",dp[i][j]);
			if(dp[n][j]>max){
				max=dp[n][j];
				k=j;
			}
		}
		//	printf("\n");
	//	}
		printf("Case %d:\n",l);
		printf("%d\n%d\n",max,k);
		scanf("%d",&w);
		l++;
	}
	return 0;
}

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


using namespace std;

int main(int argc, char *argv[]) {
  int n, b, loop = 1;
  while (cin >> b, b) {
    cin >> n;
    vector<int> wv(n, 0), vv(n, 0);
    for (int i = 0; i < n; i++) {
      int w, v;
      char c;
      cin >> v >> c >> w;
      wv[i] = w;
      vv[i] = v;
    }

    vector<int> res(b + 1, -1), log(b + 1, -1);
    res[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = b - wv[i]; j >= 0; j--) {
        if (res[j] >= 0 && res[j] + vv[i] > res[j + wv[i]]) {
          res[j + wv[i]] = res[j] + vv[i];
          log[j + wv[i]] = i;
        }
      }
    }

    auto itr = max_element(res.begin(), res.end());

    cout << "Case " << loop << ':' << endl;
    cout << *itr << endl;
    cout << itr - res.begin() << endl;
    loop++;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

string to_s(int n){
	string res;
	while(n){
		res += n%10+'0';
		n /= 10;
	}
	reverse(res.begin(),res.end());
	return res;
}
int main(){
	int N,W,c=1;
	int dp[2000];
	while(cin>>W&&W){
		fill(dp,dp+1001,0);
		cin>>N;
		while(N--){
			int v,w;
			cin>>v;
			cin.ignore();
			cin>>w;
			for(int i=W;0<=i;i--){
				dp[i+w] = max(dp[i+w],dp[i]+v);
			}
		}
		int *p = max_element(dp,dp+W+1);
		cout<<"Case "<<to_s(c++)<<':'<<endl;
		cout<<*p<<endl;
		cout<<p-dp<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>

using namespace std;

vector< vector<int> > dp;
vector<int> w,v;
int n,weight;

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<w[i]) res=rec(i+1,j);
	else res=max(rec(i+1,j),rec(i+1,j-w[i])+v[i]);

	return dp[i][j]=res;
}

int main(){

	int count=1;
	cin>>weight;
	while(weight!=0){
		cin>>n;
		w.resize(n); v.resize(n);
		for(int i=0;i<n;i++) scanf("%d,%d",&v[i],&w[i]);

		dp = vector< vector<int> >(n+1);
		for(int i=0;i<n+1;i++) dp[i]=vector<int>(weight+1,-1);

		for(int i=0;i<weight+1;i++) dp[n][i]=0;

		for(int i=n-1;i>=0;i--){
			for(int j=0;j<weight+1;j++){
				if(j<w[i]) dp[i][j]=dp[i+1][j];
				else dp[i][j]=max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]);
			}
		}

		cout<<"Case "<<count<<":"<<endl<<dp[0][weight]<<endl;
		for(int i=weight-1;i>=0;i--){
			if(dp[0][i]!=dp[0][weight]){
				cout<<i+1<<endl;
				break;
			}
		}
		count++;
		cin>>weight;
	}


	return 0;

}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

const int maxw = 1001;
const int maxn = 1001;
const int maxp = 10001;

int dp[maxn][maxw]={};
int w[maxn] = {};
int p[maxn] = {};
int W,N;

int main()
{
	int casenum = 0;
	
	while(cin >> W)
	{
		if(W==0)break;
		casenum++;
		cout << "Case " << casenum << ":" << endl;
		cin >> N;
		char t;
		rep(i,N) cin>>p[i] >> t >> w[i];

		memset(dp,0,sizeof(dp));
		rep(i,N)rep(j,W+1)
		{
			if(j-w[i]>=0) dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]] + p[i]);
			else dp[i+1][j] = dp[i][j];
		}
		int ansp = 0,answ = 0;
		for(int j=W;j!=0;j--)if(ansp<= dp[N][j])
		{
			ansp = dp[N][j];
			answ = j;
		}
		
		cout << ansp << endl;
		cout << answ << endl;
	}
	return 0;
}

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

using namespace std;

int value[1000], weight[1000];
int dp[1001][1001]; // dp[?¬?????????????][??????] = ?????§??????

int main() {
	int W;
	int c = 1;
	while ( cin >> W, W ) {
		int N; cin >> N;
		for (int i = 0; i < N; ++i) scanf("%d,%d", &value[i], &weight[i]);

		fill(&dp[0][0], &dp[0][0]+1001*1001, 0);

		for (int i = 0; i < N; ++i) {
			for (int w = 0; w <= W; ++w) {
				dp[i+1][w] = max(dp[i+1][w], dp[i][w]);
				if (w + weight[i] <= W) {
					dp[i+1][w+weight[i]] = max(dp[i+1][w+weight[i]], dp[i][w]+value[i]);
				}
			}
		}
		int av = 0, aw = 0;
		for (int w = 0; w < W; ++w) {
			if (dp[N][w] > av) {
				av = dp[N][w];
				aw = w;
			}
		}
		printf("Case %d:\n", c++);
		printf("%d\n", av);
		printf("%d\n", aw);
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define SUPmaxm 1001
#define SUPmaxw 1001

int maxm;
int maxw;
int vs[SUPmaxm];
int wos[SUPmaxm];
int dp[SUPmaxm][SUPmaxw]; //m ika no item tukatte, w ika no omomi de, sum v no max
int rev,rew;

void solve(){
	rev=0;
	FOR(m,0,maxm+1) FOR(w,0,maxw+1) dp[m][w]=0;
	FOR(m,1,maxm+1){
		FOR(w,0,maxw+1){
			if(w<wos[m]) dp[m][w] = dp[m-1][w];
			else dp[m][w] = max(dp[m-1][w],dp[m-1][w-wos[m]]+vs[m]);
			rev=max(rev,dp[m][w]);
		}
	}
	rew=0;
	FOR(w,0,maxw+1) FOR(m,0,maxm+1) if(dp[m][w]==rev) {rew=w;return;};
}

int main(){
	for(int casei=1;true;++casei){
		scanf("%d",&maxw);			if(!maxw) return 0;
		scanf("%d",&maxm);
		FOR(m,1,maxm+1) scanf("%d,%d",vs+m,wos+m);
		solve();
		printf("Case %d:\n%d\n%d\n",casei,rev,rew);
	}
}