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<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;
    int in[1001][1001]={0};
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      rep(j,m+1){
	if(j>=b)
	  in[i][j]=max(in[i][j],in[i-1][j-b]+a);
	in[i][j]=max(in[i][j],in[i-1][j]);
      }
    }
    int max1=0;
    rep(i,m+1)max1=max(max1,in[n][i]);
    rep(i,m+1)
      if(in[n][i]==max1){
	cout<<"Case "<<co<<endl;
	cout<<max1<<endl<<i<<endl;
	break;
      }
  }
}

/*int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    char d;
    cin>>n;
    vector<int>w(n),v(n);
    vector<vector<int> >dp(n+1);
    rep(i,m+1)dp[0].pb(0);
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      dp[i].pb(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;
    int in[1001][1001]={0};
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      rep(j,m+1){
	if(j>=b)
	  in[i][j]=max(in[i][j],in[i-1][j-b]+a);
	in[i][j]=max(in[i][j],in[i-1][j]);
      }
    }
    int max1=0;
    rep(i,m+1)max1=max(max1,in[n][i]);
    rep(i,m+1)
      if(in[n][i]==max1){
	cout<<"Case "<<co<<":"<<endl;
	cout<<max1<<endl<<i<<endl;
	break;
      }
  }
}

/*int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    char d;
    cin>>n;
    vector<int>w(n),v(n);
    vector<vector<int> >dp(n+1);
    rep(i,m+1)dp[0].pb(0);
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      dp[i].pb(0);
      

}
*/

C++: #include <iostream>
#include <cstring>
#include <algorithm>
#define MAX 1000
using namespace std;

int main(void){

  int n,W;
  int w[MAX+1];
  int v[MAX+1];
  int dp[MAX+1][MAX+1];
  char ten;
  int cnt = 1;

  while( cin >> W , W ){
    cin >> n;
    for( int i = 0 ; i < n ; i++ ){
      cin >> v[i] >> ten >> w[i];
    }

    for( int i = 0 ; i < MAX ; i++ ){
      for( int j = 0 ; j < MAX ; j++ ){
	dp[i][j] = 0;
      }
    }

    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];
	else dp[i+1][j] = max( dp[i][j] , dp[i][j-w[i]] + v[i] );
      }
    }
    
    cout << "Case " << cnt++ << ":" << endl;
    for( int i = W ; i >= 0 ; i-- ){
      if( dp[n][i] != dp[n][W] ){
	cout << dp[n][W] << endl << i+1 << endl;
	break;
      }
    }
  }  
  return 0;
}

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

const int MAX_N = 1001;
const int MAX_W = 1001;

int t;
int n, W;
int v[MAX_N], w[MAX_N];

int dp[MAX_N][MAX_W];

int main() {
	while (1) {
		std::cin >> W;
		if (w == 0)
			break;
		std::cin >> n;
		char ch;
		for (int i = 1; i <= n; ++i)
			std::cin >> v[i] >> ch >> w[i];
		std::memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j <= W; ++j) {
				if (w[i] > j)
					continue;
				dp[i][j] = std::max(dp[i - 1][j - w[i]] + v[i], dp[i][j]);
			}
		}
		int max = 0, maxv;
		for (int i = 0; i <= W; ++i) {
			if (max < dp[n][i]) {
				maxv = i;
				max = dp[n][i];
			}
		}
		++t;
		std::cout << "Case " << t << ':' << std::endl;
		std::cout << max << std::endl << maxv << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cstdio>
#define rep(i,k,n) for(int i = k ; i < n ; i++ )
#define INF 10000000
int v[1001],w[1001];
int dp[1001][1001];

using namespace std;

int main(){
  int W,n;
  int cas = 1;
  while(cin >> W ,W){
    fill(v,v+1001,0);
    fill(w,w+1001,0);
    fill(dp[0],dp[1001],0);
	 cin >> n;
	 char c;
	 for(int i = 0 ; i < n ; i++ ){
	   cin >> v[i] >> c >> 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]; 
	     } else {
	       dp[i+1][j] = max(dp[i][j],dp[i][j - w[i]] + v[i]);
	     }
	   }
	 }
	 int wight = 0,mx = 0;
	 for(int i = 0 ; i <= W ; i++ )if(dp[n][i] > mx)mx = dp[n][i],wight = i;
	 cout << "Case " << cas++ << ":"<<endl;
	 cout << dp[n][W] << endl;
	 cout << wight << endl;
	 }
      return 0;
  }

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

using namespace std;

int main() {
    for(int CASE = 1; ; ++CASE) {
        int W;
        cin >> W;
        if(!W) break;

        int N;
        cin >> N;
        vector<pair<int, int> > v;
        for(int i = 0; i < N; ++i) {
            int val, w;
            char comma;
            cin >> val >> comma >> w;
            v.push_back(make_pair(val, w));
        }

        vector<vector<int> > dp(2, vector<int>(W+1, 0));
        int vn = 0;
        int mv = 0;
        for(int i = 0; i < N; ++i) {
            vector<int> &vec = dp[vn];
            vector<int> &prev = dp[1-vn];

            for(int w = 0; w <= W; ++w) {
                int pw = w - v[i].second;
                if(pw >= 0) {
                    vec[w] = max(vec[w], prev[pw] + v[i].first);
                    mv = max(mv, vec[w]);
                }
            }
            vn = 1-vn;
        }

        cout << "Case " << CASE << ":" << endl;
        for(int w = 0; w <= W; ++w) {
            if(dp[1-vn][w] == mv) {
                cout << mv << endl;
                cout << w << endl;
                break;
            }
        }
    }
    return 0;
}