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<cstring>
#include<algorithm>
#include<functional>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
#define M -1;
using namespace std;
int dp[1001][1001];
vector<int> value,weight;
int main(void) 
{
	int i,j,w,n,mm,mx,cnt=0;;
	while(1) {
		cin>>w;
		if(w==0) break;
		cin>>n;
		weight.resize(n);
		value.resize(n);
		for(i=0;i<n;i++) scanf("%d,%d",&value[i],&weight[i]);
		for(i=0;i<=n;i++){
			for(j=0;j<=w;j++) dp[i][j]=M;
		}
		dp[0][0]=0;
		for(i=0;i<n;i++) {
			for(j=0;j<=w;j++) {
				if(dp[i][j]!=-1) {
					dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
					if(j+weight[i]<=w) {
						dp[i+1][j+weight[i]]=max(dp[i+1][j+weight[i]],dp[i][j]+value[i]);
					}
				}
			}
		}
		mm=-1;mx=-1;
		for(i=0;i<=w;i++) {
			if(mx<dp[n][i]) {
				mx=dp[n][i];
				mm=i;
			}
		}
		cnt++;
		cout<<"Case"<<" "<<cnt<<":"<<endl;
		cout<<mx<<endl<<mm<<endl;
	}
	exit(0);
}

C++: #include <algorithm>
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define IREP(it,array) for(vector<int>::iterator it=array.begin(); it!=array.end(); ++it)
#define SREP(it,array) for(vector<string>::iterator it=array.begin(); it!=array.end(); ++it)

#define MP       make_pair
#define PB       push_back
#define ALL(x)   (x).begin(),(x).end()

const int INF = 1<<29;
const double EPS = 1e-9;
double zero(double d){
  return d < EPS ? 0.0 : d;
}

typedef long long LL;
typedef pair<string,string> P;


vector<P> graph; 
set<string> delay;
set<string> delay2;

int main()
{
  cout.setf(ios::fixed, ios::floatfield);
  cout.precision(7);

  int targetNum=0;
  int W,N;    
  while(cin >> W, W){
    targetNum++;
    cin >> N;

    int v[N], w[N];
    REP(i,N){
      int co,we;
      char b;
      cin >> co >> b >> we;
      v[i] = co;
      w[i] = we;
    }

    int dp[N+1][W+1];
    memset(dp,0,sizeof(dp));

    REP(i,N){
      REP(j,W+1){
	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] );
	}
      }
    }

    cout << "Case " << targetNum << ":" << endl;

    int rj=0;
    REP(j,W+1){
      if( dp[N][rj] < dp[N][j] ){
	rj = j;
      }
    }


    cout << dp[N][rj] << endl << rj << endl;
  }

  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>
#include<set>
 
using namespace std;
 
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define INF 1<<25
 
typedef long long ll;

int dy[8]={1,1,1,0,0,-1,-1,-1};
int dx[8]={-1,0,1,-1,1,-1,0,1};
struct S{
	int a,b,c;
};
bool asc(const S& left,const S& right){
	return left.c > right.c;
}

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

int main(){
	int cnt=1;
	while(cin>>W){
		if(W==0)break;
		cin>>N;
		rep(i,N)cin>>v[i]>>w[i];
		REP(i,1,N+1){
			rep(j,W+1){
				if(w[i-1]<=j)
					dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i-1]]+v[i-1]);
				else 
					dp[i][j]=dp[i-1][j];
			}
		}
		int ans_v=0,ans_w=0;
		rep(i,W+1){
			if(dp[N][i]>ans_v){
				ans_v=dp[N][i];
				ans_w=i;
			}
		}
		cout<<"Case "<<cnt<<<<":"<<endl;
		cout<<ans_v<<endl;
		cout<<ans_w<<endl;
		cnt++;
	}
	return 0;
}

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

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { char c;for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> c >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int w, n;

// index, weight
int dp[1010][1010];
PArray treasure;

int main(void)
{
	ios::sync_with_stdio(false);
	int count = 1;
	while(cin >> w && w)
	{
		n = getInt();
		for (int i = 0; i <= n; i++)
		{
			memset(dp[i], 0, sizeof(dp[i]));
		}
		treasure.resize(n);
		readAll(treasure);
		P ans = P(-1, INF);
		for (int i = 0; i < n; i++)
		{
			int price = treasure[i].first, weight = treasure[i].second;
			for (int j = 0; j <= w; j++)
			{
				int nw = j + weight;
				if(nw <= j)
					dp[i + 1][nw] = max(dp[i][nw], max(dp[i + 1][nw - 1], max(dp[i + 1][nw], dp[i][j] + price)));
				if(j != 0) dp[i + 1][j] = max(dp[i + 1][j], max(dp[i + 1][j - 1], dp[i][j]));
				if(ans.first == dp[i + 1][j] && ans.second > j)
					ans = P(dp[i + 1][j], j);
				else if(ans.first < dp[i + 1][j]) ans = P(dp[i + 1][j], j);
			}
		}

		cout << "Case " << count << ':' << endl;
		cout << ans.first << endl << ans.second << endl;
		count++;
	}
	return 0;
}