Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	Case := 1
	for {
		scanner.Scan()
		W, _ := strconv.Atoi(scanner.Text())
		if W == 0 {
			break
		}
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		w := make([]int, n+1)
		v := make([]int, n+1)
		for i := 1; i <= n; i++ {
			scanner.Scan()
			info := strings.Split(scanner.Text(), ",")
			v[i], _ = strconv.Atoi(info[0])
			w[i], _ = strconv.Atoi(info[1])
		}
		// fmt.Println(v)
		// fmt.Println(w)
		dp := make([][]int, n+1)
		for i := range dp {
			dp[i] = make([]int, W+1)
		}
		maxV := -(1 << 21)
		minW := 1 << 21
		for i := 1; i <= n; i++ {
			for j := 1; j <= W; j++ {
				// fmt.Println(dp)
				if j-w[i] >= 0 {
					dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i])
					if maxV < dp[i][j] {
						maxV = dp[i][j]
						minW = j
					} else if maxV == dp[i][j] {
						minW = min(minW, j)
					}
				} else {
					dp[i][j] = dp[i-1][j]
				}
			}
		}
		fmt.Printf("Case %d:\n", Case)
		Case += 1
		fmt.Println(maxV)
		fmt.Println(minW)
	}
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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++;
  }


}