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<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 <iostream>
#include <stdio.h>

void update(int *dst, int src) {
	if (*dst < src) *dst = src;
	return;
}

int main() {
	for (int cnt = 1; 1; cnt++) {
		// 入力
		int W, N, Value[1000], Weight[1000];
		std::cin >> W;
		if (W == 0) return 0;
		std::cin >> N;
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &Value[i], &Weight[i]);
		}
		// 演算
		int dp[1001][1001];
		for (int i = 0; i <= W; i++) {
			dp[0][i] = 0;
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				update(&dp[i + 1][j], dp[i][j]);
				if (j + Weight[i] <= W) {
					update(&dp[i + 1][j + Weight[i]], dp[i][j] + Value[i]);
				}
			}
		}
		// 出力
		std::cout << "Case " << cnt << ":" << std::endl;
		int MaxValue = 0, MaxIndex = 0;
		for (int i = 0; i <= W; i++) {
			if (MaxValue < dp[N][i]) {
				MaxValue = dp[N][i];
				MaxIndex = i;
			}
		}
		std::cout << MaxValue << std::endl;
		std::cout << MaxIndex << std::endl;
	}
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(long i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-12;
const LL INF = 1e18;


long dp[1002];
long w[1001],v[1001];


int main(){
	int maxw,N;
	int cnt=1;
	while(cin>>maxw){
		if(maxw==0)break;
		cin>>N;
		CLR(dp);
		char c;
		REP(i,N)cin>>v[i]>>c>>w[i];
		
		REP(i,N){
			for(int j=maxw;j>=0;j--){
				if(j+w[i]<=maxw){
					dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i]);
				}
			}
		}
		cout<<"Case "<<cnt<<":"<<endl;
		cout<<dp[maxw]<<endl;
		long res=0;
		REP(i,maxw+1){
			if(dp[i]==dp[maxw]){
				res=i;break;
			}
		}
		cout<<res<<endl;
		cnt++;
	}
	return 0;
}
/*

*/

C++: #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<string>

using namespace std;


void Solution()
{
    int n;
    int weight;
    int num = 1;
    
    while(cin>>weight, weight)
    {
        cin >> n;
        
        int dp[1000]={0};
        
        int v,w;
        char comma;
        
        while(n--)
        {
            cin>>v>>comma>>w;
            
            for(int i = weight - w; i >= 0; i--)
            {
                dp[i + w] = max(dp[i + w], dp[i]+v);
            }
        }
        
        int mi = weight;
        
        for(int i = weight; i >= 0; i--)
        {
            if(dp[i] != dp[mi])
            {
                cout << "Case " << num++ << ":\n";
                cout << dp[mi] << "\n" << mi << "\n";
                break;
            }
            
            mi = i;
        }
    }
}

int main()
{
    Solution();
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;

int value[1001];
int cost[1001];
int dp[1001][1001];

void cut(string s,int& a,int& b){
	for(int i=0; i<s.size(); ++i){
		if(s[i]==','){
			string tmp1=s.substr(0,i);
			string tmp2=s.substr(i+1);
			a=atoi(tmp1.c_str());
			b=atoi(tmp2.c_str());
			break;
		}
	}
}

int main(){
	int W,N,cnt=1;
	while(cin>>W&&W){
		cin >> N;
		for(int i=0; i<N; ++i){
			for(int j=0; j<=1000; ++j){
				dp[i][j]=INF;
			}
		}
		for(int i=0; i<N; ++i){
			string tmp;
			cin >> tmp;
			cut(tmp,value[i],cost[i]);
			//cout << value[i] << " " << cost[i] << "\n";
		}
		dp[0][value[0]]=cost[0];
		dp[0][0]=0;
		for(int i=1; i<N; ++i){
			for(int j=0; j<=1000; ++j){
				if(j-value[i]>=0){
					if(dp[i-1][j-value[i]]+cost[i]<=W){
						dp[i][j]=min(dp[i-1][j-value[i]]+cost[i],dp[i-1][j]);
					} else {
						dp[i][j]=dp[i-1][j];
					}
				} else {
					dp[i][j]=dp[i-1][j];
				}
			}
		}
		for(int i=1000; i>=0; --i){
			if(dp[N-1][i]<INF){
				cout << "Case " << cnt << ":\n" << i << "\n" << dp[N-1][i] << "\n";
				break;
			}
		}
		++cnt;
	}
	return 0;
}