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>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int N, W, v[1002], w[1002]; // v[i],w[i] := iツ氾板姪堋づ個品ツ閉ィツづ個可ソツ値ツ、ツ重ツつウ
int dp[1002][1002]; // dp[i][j] := iツ氾板姪堋づ個品ツ閉ィツづ慊づ考ツ猟カツつオツつスツ篠楪づ個、ツ重ツつウjツづ可つィツつッツづゥツ催妥・ツづ個可ソツ値
void solve() {
  memset(dp, 0, sizeof dp);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j + w[i] <= W)
        dp[i+1][j+w[i]] = max(dp[i][j+w[i]], dp[i][j]+v[i]);
      else
        dp[i+1][j+w[i]] = dp[i][j+w[i]];
    }
  }
  // int *itr = max_element(dp[N], dp[N]+W+1);
  // printf("%d\n%d\n", *itr, itr - dp[N]);
  int ans = -1, itr = -1;
  for (int i = 0; i <= W; ++i) if (ans < dp[N][i]) ans = dp[N][i], itr = i;
  printf("%d\n%d\n", ans, itr);
}

int main() {
  int z = 1;
  while (scanf("%d",&W),W) {
    scanf("%d",&N);
    rep(i,N) scanf("%d,%d",v+i,w+i);
    printf("Case %d:\n", z);
    solve();
    ++z;
  }
  return 0; 
}

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

int item_w[1001];
int item_p[1001];
int n;
int w;
int dp[1001][1001];//kati,omosa

pair<int,int> check(int i,int ww,int pp)
{
  //  cout<<i<<","<<ww<<","<<pp<<endl;
  pair<int,int> tmp;
  pair<int,int> tmp2;
  if(ww>w)
    return make_pair(0,0);
  if(i>n)
    return make_pair(ww,pp);
  if(dp[i][ww]>pp)
    return make_pair(ww,dp[i][ww]);
  dp[i][ww]=pp;
  tmp=check(i+1,ww,pp);
  tmp2=check(i+1,ww+item_w[i],pp+item_p[i]);
  if(tmp.second>tmp2.second ||(tmp.second==tmp2.second &&tmp.first<tmp2.first))
    {
      return tmp;
    }
  else
    {
      return tmp2;
    }
}

int main()
{
  int casenum = 1;
  for(;cin>>w,w;)
    {
      cin>>n;
      memset(dp,-1,sizeof(dp));
      for(int i=0;i<n;i++)
	{
	  char tmp;
	  cin>>item_p[i]>>tmp>>item_w[i];
	}
      int wa=0;
      int ans=0;
      pair<int,int> ans2=check(0,0,0);
      /*
      for(int i=0;i<=w;i++)
	{
	  //	  cout<<"test: i,dp[i]="<<i<<","<<dp[i]<<endl;
	  if(ans<dp[i])
	    {
	      ans=dp[i];
	      wa=i;
	    }
	}
      */
            cout<<"Case "<<casenum++<<":"<<endl<<ans2.second<<endl<<ans2.first<<endl;
	    //cout<<"Case "<<casenum++<<":"<<endl<<ans<<endl<<wa<<endl;
    }
}

C++: #include<stdio.h>

int main()
{
	for(int c=1;;c++)
	{
		int w,n,kati[100],weit[100],weitk=0,katik=0;
		scanf("%d",&w);
		if(w==0)
		{
			return 0;
		}
		scanf("%d",&n);
		
		int katisum[100][100]={0},weitsum[100][100]={0};
		for(int x=0;x<n;x++)
		{
			scanf("%d,%d",&kati[x],&weit[x]);
		}
		for(int y=0;y<n;y++)
		{
			for(int z=y;z<n;z++)
			{
				for(int r=y;r<=z;r++)
				{
					katisum[y][z]+=kati[r];
					weitsum[y][z]+=weit[r];
				}
			}
		}

		for(int y=0;y<n;y++)
		{
			for(int z=0;z<n;z++)
			{
				if(w>=weitsum[y][z]&&katik<katisum[y][z])
				{
					weitk=weitsum[y][z];
					katik=katisum[y][z];
				}
				
			}
		}
		for(int y=0;y<n;y++)
		{
			for(int z=0;z<n;z++)
			{
				if(katik==katisum[y][z]&&weitk>weitsum[y][z])
				{
					weitk=weitsum[y][z];
				}
			}
		}
		printf("Case %d:\n%d\n%d\n",c,katik,weitk);
	}
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define MP make_pair
#define PB push_back
#define REP(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long LL;
typedef vector<int> VI;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int dp[1001][1001];

int main(){
	int W,N,lp=1;
	while(true){
		cin >> W;
		if(W == 0) break;

		cin >> N;
		vector<pair<int,int> > data;
		REP(i,N){
			int a,b;
			char c;
			cin >> a >> c >> b;
			data.PB(MP(a,b));
		}

		

		REP(i,N+1){
			REP(j,W+1){
				dp[i][j] = -1;
			}
		}
		dp[0][0] = 0;

		REP(i, N){
			REP(j, W+1){
				dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
				if(dp[i][j] != -1 && data[i].second+j <= W){
					dp[i+1][data[i].second+j] = max(dp[i+1][data[i].second+j], dp[i][j]+data[i].first);
				}
			}
		}

		int ans1=0, ans2=0;
		REP(i,W+1){
			if(ans1 < dp[N][i]){
				ans1 = dp[N][i];
				ans2 = i;
			}
		}
		cout << "Case " << lp << ":" << endl;
		cout << ans1 << endl;
		cout << ans2 << endl;
		lp++;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
#include <cmath>
//#include <string>
//#include <sstream>
#include <iomanip>
//#include <complex>
#include <stdio.h>
//小数制度 	cout << fixed << setprecision(5) << tmp << endl;
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)

int dp[1010][1010];
int v[1010];
int w[1010];
int main(){
  int wei,n;
  int cnt = 1;
  while(cin >> wei,wei){
    cin >> n;
    REP(i,1010) REP(j,1010) dp[i][j] = 0;
    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<=wei;j++){
        if(j-w[i]<0) dp[i+1][j] = dp[i][j];
        else  dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
      }
    }
    /*for(int i=0;i<n+1;i++){
      for(int j=0;j<wei+1;j++){
        cout << dp[i][j] << " ";//l<<endl;
      }
      cout << endl;
    }*/
    int maxv=0;
    int ans=-1;
    for(int i=0;i<wei+1;i++){
      if(maxv < dp[n][i]){
        maxv = dp[n][i];
        ans = i;
      }
    }
    cout << "Case " << cnt << ":" <<endl;
    cout << maxv << endl << ans << endl;
    cnt++;
  }
}

C++: #include<stdio.h>
#include<string.h>
int w[1000],v[1000],W,N,dp[10000][1000],dpw[10000][1000];

bool solve(int num)
{
	memset(dp,0,sizeof(dp));
	memset(dpw,0,sizeof(dpw));
	scanf("%d",&W);
	if(W==0)
		return false;
	scanf("%d",&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++)
		{
			if(j<w[i])
			{
				dp[i+1][j]=dp[i][j];
				dpw[i+1][j]=dpw[i][j];
			}
			else
			{
				int t1=dp[i][j], t2=dp[i][j-w[i]]+v[i], w1=dpw[i][j], w2=dpw[i][j-w[i]]+w[i];
				if(t1>t2||(t1==t2&&w1<w2))
				{
					dp[i+1][j]=t1;
					dpw[i+1][j]=w1;
				}
				else
				{
					dp[i+1][j]=t2;
					dpw[i+1][j]=w2;
				}
			}
		}
		printf("Case %d:\n%d\n%d\n",num,dp[N][W],dpw[N][W]);
		return true;
}

int main()
{
	for(int i=0;;i++)
		if(!solve(i+1))
			return 0;
}