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 <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1ll << 60;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

#ifdef WIN32
#define dump(x) cout << #x << " = " << (x) << "\n"
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n"
#else
#define dump(x)
#define debug(x)
#endif

int w, n, now = 0;
vector<PII> v;
PII dp[ 1001 ][ 1001 ];

PII dfs( int now, int g ) {
	if ( now == n ) {
		return MP( 0, w - g );
	}
	if ( dp[ now ][ g ].first >= 0 ) {
		return dp[ now ][ g ];
	}
	PII res = MP( -1, -1 );
	if ( g >= v[ now ].second ) {
		PII tmp = dfs( now + 1, g - v[ now ].second );
		tmp.first += v[ now ].first;
		res = tmp;
	}
	PII tmp = dfs( now + 1, g );
	if ( tmp.first > res.first || ( tmp.first == res.first && tmp.second < res.second ) ) {
		res = tmp;
	}
	return dp[ now ][ g ] = res;
}

void solve() {
	printf( "Case %d:\n", now );
	PII ans = dfs( 0, w );
	printf( "%d\n%d\n", ans.first, ans.second );
}

int main() {
#ifdef WIN32
	printf( "Debug Mode\n" );
#endif
	while ( ~scanf( "%d", &w ) && w ) {
		now++;
		for ( int i = 0; i < 1001; i++ ) {
			for ( int j = 0; j < 1001; j++ ) {
				dp[ i ][ j ] = PII( -1, -1 );
			}
		}
		scanf( "%d", &n );
		v.resize( n );
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d,%d", &v[ i ].first, &v[ i ].second );
		}
		solve();
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include<map>
#define pb(a) push_back(a)
using namespace std;
typedef pair<int,int> P;
int main(){
  int c=0;
  int w,n,i,j,k;;
  while(cin >> w,w){
    c++;
    cout << "Case "<<c<<":"<<endl;
    cin >> n;
    string s;
    vector<P> v;
    v.clear();
    P p,ans;
    for(k=0;k<n;k++){
      cin >> s;
      for(i=0;i<s.size();i++) if(s[i]==',') s[i]=' ';
      stringstream ss(s);
      ss>>p.first >>p.second;
      v.pb(p);
    }
    int dp[1005][1005]={{}};
    int inf = 1 << 20;
    fill(dp[0],dp[1004],-inf);
    dp[0][0]=0;
    for(i=0;i<n;i++){
      //cout << v[i].first << ":" << v[i].second << endl;
      for(j=0;j<=w;j++){
	if(dp[i][j]==-inf) continue;
	dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
	if(j+v[i].second<=w)
	  dp[i+1][j+v[i].second]=max(dp[i+1][j],dp[i][j]+v[i].first);
	//cout << dp[i+1][j]<<endl;
      }
    }
    ans.first=0;ans.second=0;
    /*
    for(i=0;i<=n;i++)
      for(j=0;j<=w;j++)
	if(dp[i][j]!=-inf)cout << i << ":" << j << ":" << dp[i][j] << endl;
    */
    for(i=0;i<=w;i++){
      //cout << i << ":" << dp[1][i] << endl;
      if(dp[n][i]>ans.first){
	ans.first=dp[n][i];
	ans.second=i;
      }
    }
    cout << ans.first << endl << ans.second << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    cin>>n;
    vector<int>in(m,0);
    rep(i,n){
      int a,b;
      char d;
      cin>>a>>d>>b;
      for(int i=m;i>0;i--){
	if(i-b<0)continue;
	if(i!=b&&!in[i-b])continue;
	in[i]=max(in[i-b]+a,in[i]);
      }
      //rep(i,m+1)cout<<in[i]<<" "<<i<<endl;
      //cout<<" "<<endl;
    }
    int ans1=0,ans2=0;
    for(int i=m;i>0;i--){
      if(ans1<=in[i]){ans1=in[i];ans2=i;}
    }
    cout<<"Case "<<co<<":"<<endl;
    cout<<ans1<<endl<<ans2<<endl;
  }
}

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

int main(){
  int W, N, cs=0;
  int cost[1001], weight[1001];
  int dp[1001];
  
  while(cin >> W, W){
    cin >> N;
    for(int i = 0 ; i < N ; i++){
      scanf("%d,%d", &cost[i], &weight[i]);
    }
    fill(dp, dp+1001, -1);
    dp[0] = 0;

    for(int i = 0 ; i < N ; i++){
      for(int j = W-weight[i] ; j >= 0 ; j--){
	if(dp[j] >= 0) dp[j+weight[i]] = max(dp[j+weight[i]], dp[j]+cost[i]);
      }
    }

    int Cost = 0, Weight = 0;
    
    for(int i = 0 ; i <= W ; i++){
      if(dp[i] > Cost){
	Cost = dp[i];
	Weight = i;
      }
    }
    printf("Case %d:\n%d\n%d\n", ++cs, Cost, Weight);
  }
  return 0;
}