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>
using namespace std;

int dp[1024][1024];

int main()
{
	int N,W,T=0;
	while(cin>>W, W)
	{
		cin >> N;

		int v,w; char c;
		for(int i=0; i<N; i++)
		{
			cin>>v>>c>>w;
			for(int j=0; j<=W-w; j++)
			{
				dp[i+1][j+w] = max(dp[i+1][j+w], dp[i][j]+v);
			}
		}

		int mi=W;
		for(int i=W; i>=0; i--)
		{
			if(dp[N][i]!=dp[N][mi])
			{
				cout << "Case " << ++T << ":\n";
				cout << dp[N][mi] << "\n" << mi << "\n";
				break;
			}
			mi=i;
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[1001][1001],wait[1001],value[1001];
void Initialization()
{
    for(int i=0;i<1001;i++)for(int j=0;j<1001;j++)dp[i][j]=-1;
}
int power(int n)
{
    int num=1;
    for(int i=1;i<=n;i++)num*=10;
    return num;
}
int serch(int i,int j,int n)
{
    int num;
    if(dp[i][j]>-1)return dp[i][j];
    else if(i==n)num=0;
    else if(j<wait[i])num=serch(i+1,j,n);
    else num=max(serch(i+1,j,n),serch(i+1,j-wait[i],n)+value[i]);
    return dp[i][j]=num;
}
int wserch(int i,int va,int ser,int n,int w)
{
    int num;
    if(i==n)num=w;
    else if(ser<va+value[i])num=wserch(i+1,va,ser,n,w);
    else if(ser==va+value[i])num=wait[i];
    else num=min(wserch(i+1,va,ser,n,w),wserch(i+1,va+value[i],ser,n,w)+wait[i]);
    return num;
}
int main()
{
    int n,w,point,num=1,wai,wa;
    string str;
    while(cin>>w&&w!=0){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>str;
            value[i]=0;wait[i]=0;
            for(int j=0;;j++){
                if(str[j]==','){
                    for(int k=0;k<j;k++)value[i]+=power(j-k-1)*(str[k]-'0');
                    point=j;
                }
                if(str[j]=='\0'){
                    for(int k=point+1;k<j;k++)wait[i]+=power(j-k-1)*(str[k]-'0');
                    break;
                }
            }
        }
        Initialization();
        point=serch(0,w,n);
        wai=w;
        for(int i=0;i<n;i++){
            if(value[i]<point){
                wa=wserch(i+1,value[i],point,n,w)+wait[i];
                if(wa<wai)wai=wa;
            }
            else if(value[i]==point&&wai>wa)wai=wa;
        }
        cout<<"Case "<<num<<":\n"<<point<<endl<<wai<<endl;
        now++;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX_N 1000

using namespace std;

int W, N;
int maxValue = 0, maxWeight = 0;
int value[MAX_N], weight[MAX_N];

void dfs(int pos, int Wsum, int Vsum) {
    if(Wsum > W) return;
    maxValue = max(maxValue, Vsum);
    if(maxValue == Vsum) maxWeight = Wsum;
    if(pos >= N) return;
    
    dfs(pos + 1, Wsum + weight[pos], Vsum + value[pos]);
    dfs(pos + 1, Wsum, Vsum);
    return;
}

int main(void) {
    int no = 1;
    while(scanf("%d %d", &W, &N), W != 0) {
        for(int r = 0; r < N; r++) {
            scanf("%d,%d", &value[r], &weight[r]);
        }
        dfs(0, 0, 0);
        cout << "Case " << no++ << ":" << endl;
        cout << maxValue << endl << maxWeight << endl;
        maxValue = maxWeight = 0;
    }
    return(0);
}

C++: #include <bits/stdc++.h>
using namespace std;

using db = double;
using ll = long long;
using vi = vector <int>;
#define op operator
#define pb push_back

const int N = 1e3L + 11;
int dp[N][N];

int main() {
	ios :: sync_with_stdio(0);

	int W, n;
	int _ = 0;
	while(cin >> W && W) {
		cin >> n;
		_ ++;
		memset(dp, 0, sizeof dp);
		for(int i = 1; i <= n; i ++) {
			int v, w; char c;
			cin >> v >> c >> w;
			for(int j = 0; j <= W; j ++)
				dp[i][j] = dp[i - 1][j];
			for(int j = w; j <= W; j ++)
				dp[i][j] = max(dp[i][j], dp[i - 1][j - w] + v);
		}
		cout << "Case " << _ << ":\n";
		cout << *max_element(dp[n], dp[n] + W + 1) << '\n';
		cout << max_element(dp[n], dp[n] + W + 1) - dp[n] << '\n';
	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <climits>
#include <map>
#include <stack>
using namespace std;
#define M_PI 3.141592
#define toRad 2.0*M_PI/360.0
#define inin(x) int x;cin>>x;
#define all(x) x.begin(),x.end()
#define debug(x) cout<<#x<<" "<<x<<endl;
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define first second
#define second first
typedef pair<int, int> pii;
int w, n;
pii memo[1000][1200];
vector<pii> a;
pii dp(int num, int W)
{
	if (W > w)return pii(INT_MIN, INT_MAX);
	if (num == n)return pii(0, 0);
	if (memo[num][W].first != -1)return memo[num][W];
	pii ans;
	if (dp(num + 1, W).first == dp(num + 1, W + a[num].second).first + a[num].first)
	{
		if (dp(num + 1, W).second < dp(num + 1, W + a[num].second).second)
		{
			ans = dp(num + 1, W);
		}
		else
		{
			pii p = dp(num + 1, W + a[num].second);
			p.first += a[num].first;
			p.second += a[num].second;
			ans = p;
		}
	}
	else if (dp(num + 1, W).first > dp(num + 1, W + a[num].second).first + a[num].first)
	{
		ans = dp(num + 1, W);
	}
	else
	{
		pii p = dp(num + 1, W + a[num].second);
		p.first += a[num].first;
		p.second += a[num].second;
		ans = p;

	}
	return memo[num][W] = ans;
}

int main()
{
	int Case = 0;
	while (cin >> w&&w&&++Case)
	{
		memset(memo, -1, sizeof memo);
		a.clear();
		cin >> n;
		rep(i, n)
		{
			pii x;
			scanf("%d,%d", &x.first, &x.second);
			a.push_back(x);
		}
		sort(all(a));
		pii val = dp(0, 0), wei;
		cout << "Case " << Case << ":" << endl;
		cout << val.first << endl << val.second << endl;
	}
}

C++: 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  int w,n;
  int t=0;
  while(1){
    t++;
    cin>>w;
    if(w==0)break;
    cout<<"Case "<<t<<":"<<endl; 
    cin>>n;
    vector<vi> dp(n+1,vi(w+1,-1));
    dp[0][0]=0;
    rep(i,n){
      int tv,tw;
      char c;
      cin>>tv>>c>>tw;
      rep(j,w+1)if(dp[i][j]>=0){
	dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
	if(j+tw<=w)
	  dp[i+1][j+tw]=max(dp[i+1][j+tw],dp[i][j]+tv);
      } 
    }
    int ans=0;
    rep(i,w+1)if(dp[n][i]>dp[n][ans])ans=i;

    cout<<dp[n][ans]<<endl;
    cout<<ans<<endl;
   
  }
}