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<vector>
#include<cstdio>
#include<algorithm>
#include<string>

using namespace std;

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

int w,n;
vector<int> v;
vector<int> g;

void init(){
	v.clear();
	g.clear();
}
bool input(){
	cin>>w;
	if(w==0)return false;
	cin>>n;
	
	rep(i,n){
		int a,b;
		scanf("%d,%d",&a,&b);
		v.push_back(a);
		g.push_back(b);
	}
	return true;
}

const int W = 1111;
const int N = 1111;
const int INF = 1000000000;
int dp[N][W];

pair<int,int> solve(){
	rep(i,N)rep(j,W)dp[i][j]=0;
	
	reps(i,g[0],w+1)dp[0][i]=v[0];
	
	reps(i,1,n){
		rep(j,w+1){
			if(j!=0)dp[i][j] = dp[i-1][j];
			dp[i][j] = max(dp[i][j],dp[i-1][j]);
			if(j-g[i]>=0)dp[i][j] = max(dp[i][j], dp[i-1][j-g[i]]+v[i]);
		}
	}
	
	int maxp = 0;
	rep(j,w+1){
		if(dp[n-1][maxp]<dp[n-1][j])maxp=j;
	}
	
	/*
	rep(i,n){
		rep(j,w+1)printf("%3d ",dp[i][j]);puts("");
	}*/
	
	return pair<int,int>(dp[n-1][maxp],maxp);
}
int main(){
	for(int i=1;init(),input();i++){
		pair<int,int> ans = solve();
		printf("Case %d:\n%d\n%d\n",i,ans.first,ans.second);
	}
}

/*
10
5
60,2
100,4
120,6
210,9
10,1
0


10
2
100,5
90,4
0

*/

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>

using namespace std;

const int INF = 1 << 29;
typedef pair<int, int> P;

int n, w;
int dp[1001][1001];//お宝の番号、重さ
int main(void)
{
	int caseCount = 0;
	while(cin >> w && w)
	{
		cin >> n;
		vector<int> ps(n), ws(n);
		memset(dp, 0, sizeof(dp));
		char c;
		for (int i = 0; i < n; i++)
		{
			cin >> ps[i] >> c >> ws[i];
		}

		if(ws[0] <= w) dp[0][ws[0]] = ps[0];
		for (int i = 1; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				/*if(ws[i] <= j)
					dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ws[i]] + ps[i]);*/
				if(j + ws[i] <= w)
					dp[i][j + ws[i]] = max(dp[i][j], max(dp[i - 1][j + ws[i]], dp[i - 1][j] + ps[i]));
				else dp[i][j] = max(dp[i - 1][j], dp[i][j]);
			}
		}

		cout << "Case " << ++caseCount << ":" << endl;
		int ansp = 0, answ = INF;
		for (int i = 0; i <= w; i++)
		{
			if(ansp < dp[n - 1][i])
			{
				ansp = dp[n - 1][i];
				answ = i;
			}
		}
		cout << ansp << endl << answ << endl;
	}
	return 0;
}

C++: //0042 A Thief
//vñFibvTbNâè

#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;

int main(void)
{

	const int MAX_WEIGHT = 1000;
	const int MAX_OBJECT = 10000;
	const int MAX_SCORE = 1000;

	struct Obj
	{
		int w;
		int score;
	};
	struct Cloth
	{
		int index;
		int totalScore;
	};

	int caseNum = 1;
	while(1)
	{
		//ÇÌd³ÌÆ«àAÅãÉüê½CfbNXA»êÜÅÌ¿lð¶ÝµÈ¢lÉ
		int totalScore[MAX_WEIGHT+1] = {0};
		int intoIndex[MAX_WEIGHT+1];	//ÔÁ¿á¯±êÍú»µÈ­ÄàâèÈµ

		//\¢Ì¶áfobOÅl©êÈ¢æI
		//Cloth total[MAX_WEIGHT+1];

		Obj obj[MAX_OBJECT];

		int w, n;
		int i, j;
		char dummy;

		cin >> w;
		if(w == 0)
			break;
		cin >> n;

		for(i = 0; i < n; i++)
		{
			cin >> obj[i].score >> dummy >> obj[i].w;
			
			for(j = w-obj[i].w; j >= 1; j--)
			{
				//·ÅÉ»Ìd³ÜÅÏÜê½àÌª é
				if(totalScore[j])
				{
					//æèæ¢ÂÝûð­©
					if(totalScore[j+ obj[i].w] < totalScore[j] + obj[i].score)
					{
						totalScore[j+ obj[i].w] = totalScore[j] + obj[i].score;
						intoIndex[j+ obj[i].w] = i;
					}
				}
			}
			//ÅÌÍüêÄà¢¢Ìæ
			if(totalScore[obj[i].w] < obj[i].score)
			{
				totalScore[obj[i].w] = obj[i].score;
				intoIndex[obj[i].w] = i;
			}
		}

		cout << "Case " << caseNum << ":" << endl;
		caseNum++;

		//zñÉüÁ½vfÌÅålÆA»ÌCfbNXðT·
		int maxScore = 0;
		int maxIndex = -1;
		for(i = 0; i <= w; i++)
		{
			if(totalScore[i] && maxScore<totalScore[i])
			{
				maxScore = totalScore[i];
				maxIndex = i;
			}
		}
		//½©vfÍüÁÄ½H
		if(maxIndex >= 0)
		{
			cout << totalScore[maxIndex] << endl;

			//d³aÌvZ
			int totalW = 0;
			int index = maxIndex;
			while(index>0)
			{
				totalW += obj[ intoIndex[index] ].w;
				index -= obj[ intoIndex[index] ].w;
			}
			cout << totalW << endl;
		}
		//êÂàüçÈ©Á½H
		else
			cout << "0" << endl << "0" << endl;
	}
	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w,n;
  int cnt = 1;
  char c;
  while(cin >> w){
    if(w == 0) break;
    cin >> n;
    vary(int,vs,n,0);
    vary(int,ws,n,0);
    vector<vector<pii>> dp(n+1,vector<pii>(w+1,make_pair(0,0)));
//     vector<vector<int>> dp2(n+1,vector<int>(w+1,0));
    rep(i,n){
      cin >> vs[i] >> c >> ws[i];
    }
    REP(i,0,n){
      rep(j,w){
        dp[i+1][j] = max(dp[i][j], make_pair((j-ws[i] >= 0)? dp[i][j-ws[i]].fi + vs[i] : 0,j));
      }
    }
    cout << "Case " + to_string(cnt) + ":"<<endl;
    rep(i,w+1){
      if(dp[n][i] == dp[n][w-1]){
        cout << dp[n][w-1].fi<<endl<<i<<endl;
        break;
      }
    }
    ++cnt;
  }
  return 0;
}