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 <utility>
#include <vector>
#include <cstring>
using namespace std;
const int MAX_N = 1000,MAX_M = 10000;
int n;
typedef pair<int,int> P;
P ilist[MAX_N];
P memo[MAX_N + 1][MAX_M + 1];

P comp(const P &a,const P &b){
	if(a.first < b.first) return b;
	return a;
}

P dp(int now,int lim){
	if(memo[now][lim].first > -1){
		return memo[now][lim];
	}
	P res;
	if(now == n) return make_pair(0,0);
	else if(lim < ilist[now].second){
		res = dp(now + 1,lim);
	}
	else{
		res = dp(now + 1,lim - ilist[now].second);
		res.second += ilist[now].second;
		res.first += ilist[now].first;
		res = comp(dp(now + 1,lim),res);
	}
	return memo[now][lim] = res;
}

int main(){
	int lim,cnt = 0,w,v;
	P item;
	char buf;
	while(cin >> lim,lim){
		for(int i = 0;i <= MAX_N;++i){
			for(int j = 0;j <= MAX_M;++j){
				memo[i][j] = make_pair(-1,-1);
			}
		}
		++cnt;
		cin >> n;
		for(int i = 0;i < n;++i){
			cin >> v >> buf >> w;
			ilist[i] = make_pair(v,w);
		}
		item = dp(0,lim);
		cout << "Case " << cnt << ":" << endl;
		cout << item.first << endl << item.second << endl;
	}
	return 0;
}

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

int dp[1000], w[1000], v[1000];

int main(){
    int W;
    int N;
    int c = 1;

    while(cin >> W, W != 0){
        fill(dp, dp+W+1, 0);
        cin >> 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=W; j>=0; j--){
                if(j+w[i] <= W){ //ibvUbNÉiÔÚÌi¨ªüêçêé
                    dp[j+w[i]] = max(dp[j+w[i]], dp[j]+v[i]);
                    /* dp[j+w[i]] Íj+w[i]ÜÅÌd³Åìêé¿lÌ(bè)Åål
                     * dp[j]+v[i] ÍjÜÅÌd³Åìêé¿lÌÅålÆiÔÚÌàÌð«µ½Ì
                     * ¿lðvµ½àÌ
                     * å«¢ûªj+w[i]ÜÅÌå«³Åìêé¿lÌbèÅålÉÈé
                     */
                }
            }
        }

        int max_value = 0;
        for(int i=0; i<=W; i++){
            if(dp[i] > dp[max_value]){
                max_value = i;
            }
        }

        cout << "Case " << c << ":" << endl;
        cout << dp[max_value] << endl;
        cout << max_value << endl;

        c++;
    }
    return 0;
}

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

using namespace std;

typedef long long ll;

ll value[1501], weight[1501];
ll dp[1501][1501]; // 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]+1501*1501, 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]);
				}
			}
		}
		ll 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 <vector>
#include <algorithm>
#include <cmath>
#include <utility>

#define ll (long long)
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)

using namespace std;

/*thief??????*/

int main(void){
    
    /*(x,y) = (??????,??????)*/
    int endurew;
    int number;
    int answer = 0;
    int casecounter = 1;
    while(cin >> endurew,endurew)
    {
        int dp[1002][1002] = {0};
        int w[1002] = {0},v[1002] = {0};
        cin >> number;
        for(int i  =1; i<= number;i++)
            scanf("%d%*c%d",&v[i],&w[i]);
        
        for(int j = 1; j<= number;j++)
            for(int k = 0;k<=endurew;k++)
            {
              if(j == 1 && k>=w[j])
                  dp[j][k] = v[j];
              else if(k >= w[j])
                  dp[j][k] = v[j]+dp[j-1][k-w[j]];
              else
                  dp[j][k] = dp[j-1][k];
                
            }
        int max = 0;
        for(int i = 1;i<=endurew;i++)
        {
            if(max < dp[number][i])
            {
                answer = i;
                max = dp[number][i];
            }
            
        }
        cout << "Case " << casecounter<< ":" << endl << dp[number][answer]<< endl;
        cout << answer << endl;
        casecounter++;
    }
    return 0;
    
}

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

/*
int cost,n;
int V[3000],C[3000];
int dp[1100][1100];
int therock=0;
int tt=0;


int rec(int a,int b){
  int res=0;
  if(a == n){
 if(tt<=res){
      cout << "tt  = " << b+C[a] << endl;
therock=b+C[a];
      tt=res;
    }


return 0;
}
  if(dp[a][b] != -1){
 if(tt<=res){
      cout << "tt  = " << b+C[a] << endl;
therock=b+C[a];
      tt=res;
    }

return dp[a][b];
  }
  
  // cout << "rest =" << therock << endl;
  if(cost<b+C[a]){
 if(tt<=res){
      cout << "tt  = " << b+C[a] << endl;
therock=b+C[a];
      tt=res;
    }

    return 0;
  }
  else{
    res = max(rec(a+1,b),rec(a+1,b+C[a])+V[a]);
    if(tt<=res){
      cout << "tt  = " << b+C[a] << endl;
therock=b+C[a];
      tt=res;
    }
  }
  
  return dp[a][b]= res;

}



int main(){
  int g=1;

  while(1){
    cin >> cost;
    if(cost == 0)break;
    cin >> n;

    for(int i=0;i<n;i++){
      scanf("%d,%d",&V[i],&C[i]);
    }
    memset(dp,-1,sizeof(dp));
    cout << "Case " << g << ":" << endl;
    cout << rec(0,0) << endl;
       
    for(int i=0;i<n;i++){
      for(int j=0;j<cost;j++){
	cout << dp[i][j] << " ";
      }
      cout << endl;
    }

      cout << therock << endl;
    g++;
    therock=0;
  }




}
*/
int n,m;
int V[1100],C[1100];
int dp[1100][1100];
int cc=0;
void solve(){
  for(int i=0;i<m;i++){
    for(int j=0;j<=n;j++){
      if(j<C[i]){
	dp[i+1][j]=dp[i][j];
      }else{
	dp[i+1][j]=max(dp[i][j],dp[i][j-C[i]]+V[i]);
      }
    }
  }
  cout << dp[m][n] << endl;
  for(int i=0;i<=n;i++){
    if(dp[m][i] == dp[m][n]){
      cout << i << endl;
      break;
    }
  }
  


}


int main(){

  int ghc=1;
  while(1){
    cin >> n ;
    if(n == 0)break;
    cin >> m;

    for(int i=0;i<m;i++)scanf("%d,%d",&V[i],&C[i]);
    memset(dp,0,sizeof(dp));
    cout << "Case " << ghc << ":" << endl;
    solve();
    ghc++;
  }


}