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 <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

void solve(int W,int N){
    vector<int> v(N),w(N);
    char c;
    rep(i,N) cin>>v[i]>>c>>w[i];

    // dp[i][j] : look until i-th , weight sum <= j
    vector<vector<int>> dp(N+10,vector<int>(W+10,0));
    for(int i=0;i<N;i++){
        // use v[i],w[i]
        for(int j=0;j<=W;j++){
            if(j+w[i]<=W){
                dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]],dp[i][j]+v[i]);
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }else{
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }
        }
    }

    {   // check
        int mx=0;
        for(int i=0;i<=N;i++) for(int j=0;j<=W;j++) mx = max(mx,dp[i][j]);
        assert(mx==dp[N][W]);
    }

    for(int i=0;i<=W;i++) if(dp[N][i]==dp[N][W]){
        printf("%d\n%d\n",dp[N][W],i);
        return;
    }
}

int main(){
    int w,n;
    int cnt=0;
    while(1){
        cin>>w;
        if(w==0) break;
        cin>>n;
        printf("Case %d:\n",++cnt);
        solve(w,n);
    }
}

C++: #include <iostream>
#include <math.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std;

const int MAX_N=1000, MAX_U=1000;
int u,n,v,w;
int dp[MAX_U+1];
int res_w[50],res_v[50];
int c=1;

int main(){
	while(cin >> u,u){		
		cin >> n;
		memset(dp, 0, sizeof(dp));

		for(int i=0;i<n;i++){
			scanf("%d,%d", &v,&w);
			for(int j=u;j>=0;j--) if(j-w>=0) dp[j] = max(dp[j], dp[j-w]+v);
		}
		
		int tmp = u;
		for(int i=u;i>=0;i--) if(dp[i]>=dp[tmp]) tmp=i;
		
		cout << "Case " << c << ":" << endl;
		cout << dp[tmp] << endl << tmp << endl;
		c++;
	}
	
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int dp[1000],N,v[100],w[100],W;
signed main(){
    for(int l=1;;l++){
        fill(dp,dp+1000,0);
        /*fill(v,v+100,0);
        fill(w,w+100,0);*/
        cin>>W;
        if(W==0)break;
        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>=w[i];j--){
                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
            }
        }
        for(int i=W;i>=0;i--){
            if(dp[W]!=dp[i]){
                cout<<"Case "<<l<<":"<<endl<<dp[W]<<endl<<i+1<<endl;
                break;
            }
        }
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int dp[1001][1001];
  int n,W;
  int v[1000],w[1000];
  int num=0;
  char c;
  int cnt=0;
  while(cin>> W>> n,W){
    cnt++;

    for(int i=0;i<n;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }

    for(int i=1;i<=n;i++){
      for(int j=1;j<=W;j++){
	if(j<w[i]){
	  dp[i][j]=dp[i-1][j];
	} else {
	  dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
	}
      }
    }

    int ansv=0,answ;
    for(int i=1;i<=W;i++){
      if(ansv<dp[n][i]){
	ansv=dp[n][i]; answ=i;
      }
    }
    printf("Case %d:\n%d\n%d\n",cnt,ansv,answ);
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<int,int> P;
const ll MOD=1234567;
const ll INF=1000000010;
const ll LINF=1000000000000000010LL;
const int MAX=10000010;
const double EPS=1e-3;
int dx[4]={0,1,0,1};
int dy[4]={0,0,1,1};
int dp[1010][1010];
int main(){
	int W;
	int c=0;
	while(cin>>W,W){
		int n;cin>>n;
		int v[1010],w[1010];
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n;i++)scanf("%d,%d",&v[i],&w[i]);
		for(int i=0;i<n;i++){
			for(int j=0;j<=W;j++){
				dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
				if(j+w[i]<=W){
					dp[i+1][j+w[i]]=max(dp[i+1][j+w[i]],dp[i][j]+v[i]);
				}
			}
		}
		int ans=0;
		int m;
		for(int i=0;i<=W;i++){
			if(ans<dp[n][i]){
				ans=dp[n][i];
				m=i;
			}
		}
		cout<<"Case "<<c<<":"<<endl;
		cout<<ans<<endl;
		cout<<m<<endl;
		c++;
	}
}

C++: //A Thief
#include <cstdio>
#include <iostream>
#include <string.h>
#include <cstdlib>

using namespace std;

int dp[1000][1000];
int n;
int w[1000], v[1000];
int W;
int weight;

int rec(int i, int j)
{
	if(dp[i][j] >= 0){
		return dp[i][j];
	}
	
	int res;
	if(i == n){
		res = 0;
	}
	else if(j < w[i]){
		res = rec(i + 1, j);
	}
	else{
		if(rec(i + 1, j) < rec(i + 1, j - w[i]) + v[i]){
			res = rec(i + 1, j - w[i]) + v[i];
			weight += w[i];
		}
		else{
			res = rec(i + 1, j);
			weight -= w[i];
		}
	}
	return dp[i][j] = res;
}

void solve()
{
	memset(dp, -1, sizeof(dp));
	cout << rec(0, W) << endl;
}

int main()
{
	int count = 1;
	while(scanf("%d", &W) != EOF){
		if(W == 0){ break; }
		char c;
		char temp[10000];
		cin >> n;
		for(int i = 0; i < n; i++){
			scanf("%d,%d", &v[i], &w[i]);
		}
		weight = 0;
		cout << "Case " << count << ":" << endl;
		solve();
		cout << weight << endl;
		count++;
	}
	
	return 0;
}