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


}

C++: #include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<cstdio>
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a; i ++)
int main(void){
  int w;
  int n;
  int num=0;
  while(cin>>w,w){
    cout<<"Case "<<++num<<":"<<endl;
    cin>>n;
    vector<pair<int,int> > in(n);
    rep(i,n){
      int a,b;
      scanf("%d,%d",&a,&b);
      in[i]=make_pair(a,b);
   }    
    int resw = 0;
    int resm = 0;
    int last = 1<<n;
    for(int i = 1 ; i < last+1 ; i ++){
      //cout<<i<<endl;
      int nw = 0;
      int money = 0;
      rep(j,n){
	bool use = (i/(1<<j))%2;

	//cout<<use<<" ";
	if(use){
	  money += in[j].first;
	  nw += in[j].second;
	}
      }
      if(nw>w){
	//cout<<"con"<<endl;
	continue;
      }
      
      //cout<<endl;
      if(resm<money){
	//cout<<endl;
	  
	resm = money;
	resw = nw;
      }
      if(money==resm && resw>nw){
	resw = nw;
      }
      //cout<<i<<" "<<resw<<endl;
      //cout<<endl;
    }
    cout<<resm<<endl<<resw<<endl;

    //*/
  }
}

C++: #include <cstdio>

#include <cstdlib>

#include <cmath>

#include <climits>

#include <cfloat>

#include <map>

#include <utility>

#include <set>

#include <iostream>

#include <memory>

#include <string>

#include <vector>

#include <algorithm>

#include <functional>

#include <sstream>

#include <complex>

#include <stack>

#include <queue>

#include <numeric>

#include <cstring>



#define rep(i, n) for(int i = 0; i < (n); i++)

#define pb push_back



using namespace std;



typedef long long ll;

typedef pair<int, int> P;

typedef vector<int> vi;

typedef vector<vi> vvi;



const int INF = 1 << 29;

const double EPS = 1e-9;

const int MAX_N = 1000;

const int MAX_W = 10000;



int dp[MAX_N + 1][MAX_W + 1];



int main(){

	int n, cap;

	int v[MAX_N], w[MAX_N];

//	int dp[MAX_N + 1][MAX_W + 1];



	while(1){

		memset(dp, 0 , sizeof(dp));

		scanf("%d", &cap);

		if(cap == 0) break;

		scanf("%d", &n);



		rep(i, n){

			scanf("%d,%d\n", &v[i], &w[i]);

		}



		for(int i = 0; i < n; i++){

			for(int j = 0; j <= cap; j++){

				if(w[i] > cap){

					dp[i+1][j] = dp[i][j];

				}

				if(w[i] < cap){

					dp[i+1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);

				}

			}

		}

		cout << dp[n][cap] << endl;

	}

}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int w[1000],v[1000];
int W,N;
int memo[1001][1001];
int func(int i,int j){
    if(memo[i][j]>=0)return memo[i][j];

    int res;
    if(i==N)res=0;
    else if(j-w[i]<0)res=func(i+1,j);
    else res=max(func(i+1,j),func(i+1,j-w[i])+v[i]);

    return memo[i][j]=res;
}
int main(){
    int cnt=0;
    while(cin>>W,W){
        cin>>N;
        cnt++;
        for(int i=0;i<N;i++)(cin>>v[i]).ignore()>>w[i];
        fill(memo[0],memo[0]+1001*1001,-1);
        memo[0][0]=0;
        func(0,W);

        for(int i=W;i>=0;i--){
            if(memo[0][i]==-1)continue;
            if(memo[0][i]>memo[0][i-1]){
                cout<<"Case "<<cnt<<":"<<endl;
                cout<<memo[0][i]<<endl<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

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

int T, N, V[1010], W[1010];

int main() {

	for ( int t = 1; scanf( "%d", &T ) && T; t++ ) {
	
		scanf( "%d", &N );
	
		for ( int i = 0; i < N; i++ ) { scanf( "%d,%d", &V[i], &W[i] ); }
	
		int A[1010][1010] = {{0}};
	
		for ( int i = 1; i <= N; i++ ) {
		
			for ( int j = 0; j <= T; j++ ) {
	
				if ( j < W[i-1] ) {
	
					A[i][j] = A[i-1][j];
	
				} else {
	
					A[i][j] = max( A[i-1][j], A[i-1][j-W[i-1]] + V[i-1] );
					
				}
	
			}
	
		}
		
		int best = -1, best_w = -1;
		
		for ( int j = 0; j <= T; j++ ) {
		
			if ( A[N][j] > best ) {
			
				best = A[N][j];
				best_w = j;
				
			}
		
		}
		
		printf( "Case %d:\n%d\n%d\n", t, best, best_w );
	
	}

}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fr first
#define sc second
using namespace std;

int w,n;
typedef pair<int,int> P;
P dp[1001][1001];
bool chg[1001][1001];
vector <P> dat;

P rec(int idx,int wi){
    if(idx == n){
        return P(0,wi);
    }
    if(chg[idx][wi]) return dp[idx][wi];
    P p;
    if(wi - dat[idx].sc >= 0){
        p = rec(idx+1,wi-dat[idx].sc);
        p.first += dat[idx].fr;
        p = max(p,rec(idx+1,wi));
    }
    else p = rec(idx+1,wi);
    chg[idx][wi] = true;
    return dp[idx][wi] = p;
}

int main(){
    while(cin >> w,w){
        int a = 0;
        memset(dp,-1,sizeof(dp));
        memset(chg,false,sizeof(chg));
        cin >> n;
        for(int i=0;i<n;i++){
            int t,m; scanf("%d,%d",&t,&m); dat.push_back(P(t,m));
        }
        P p = rec(0,w);
        cout << "Case " << ++a << ":\n" << p.fr << endl << w - p.sc << endl;
        dat.clear();
    }
}