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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int N, W, v[1002], w[1002]; // v[i],w[i] := iツ氾板姪堋づ個品ツ閉ィツづ個可ソツ値ツ、ツ重ツつウ
int dp[1002][1002]; // dp[i][j] := iツ氾板姪堋づ個品ツ閉ィツづ慊づ考ツ猟カツつオツつスツ篠楪づ個、ツ重ツつウjツづ可つィツつッツづゥツ催妥・ツづ個可ソツ値
void solve() {
  memset(dp, 0, sizeof dp);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j + w[i] <= W)
        dp[i+1][j+w[i]] = max(dp[i][j+w[i]], dp[i][j]+v[i]);
      else
        dp[i+1][j+w[i]] = dp[i][j+w[i]];
    }
  }
  // int *itr = max_element(dp[N], dp[N]+W+1);
  // printf("%d\n%d\n", *itr, itr - dp[N]);
  int ans = -1, itr = -1;
  for (int i = 0; i <= W; ++i) if (ans < dp[N][i]) ans = dp[N][i], itr = i;
  printf("%d\n%d\n", ans, itr);
}

int main() {
  int z = 1;
  while (scanf("%d",&W),W) {
    scanf("%d",&N);
    rep(i,N) scanf("%d,%d",v+i,w+i);
    printf("Case %d:\n", z);
    solve();
    ++z;
  }
  return 0; 
}

C++: #include<iostream>
#include<cstring>
using namespace std;

int item_w[1001];
int item_p[1001];
int n;
int w;
int dp[1001][1001];//kati,omosa

pair<int,int> check(int i,int ww,int pp)
{
  //  cout<<i<<","<<ww<<","<<pp<<endl;
  pair<int,int> tmp;
  pair<int,int> tmp2;
  if(ww>w)
    return make_pair(0,0);
  if(i>n)
    return make_pair(ww,pp);
  if(dp[i][ww]>pp)
    return make_pair(ww,dp[i][ww]);
  dp[i][ww]=pp;
  tmp=check(i+1,ww,pp);
  tmp2=check(i+1,ww+item_w[i],pp+item_p[i]);
  if(tmp.second>tmp2.second ||(tmp.second==tmp2.second &&tmp.first<tmp2.first))
    {
      return tmp;
    }
  else
    {
      return tmp2;
    }
}

int main()
{
  int casenum = 1;
  for(;cin>>w,w;)
    {
      cin>>n;
      memset(dp,-1,sizeof(dp));
      for(int i=0;i<n;i++)
	{
	  char tmp;
	  cin>>item_p[i]>>tmp>>item_w[i];
	}
      int wa=0;
      int ans=0;
      pair<int,int> ans2=check(0,0,0);
      /*
      for(int i=0;i<=w;i++)
	{
	  //	  cout<<"test: i,dp[i]="<<i<<","<<dp[i]<<endl;
	  if(ans<dp[i])
	    {
	      ans=dp[i];
	      wa=i;
	    }
	}
      */
            cout<<"Case "<<casenum++<<":"<<endl<<ans2.second<<endl<<ans2.first<<endl;
	    //cout<<"Case "<<casenum++<<":"<<endl<<ans<<endl<<wa<<endl;
    }
}

C++: #include<stdio.h>

int main()
{
	for(int c=1;;c++)
	{
		int w,n,kati[100],weit[100],weitk=0,katik=0;
		scanf("%d",&w);
		if(w==0)
		{
			return 0;
		}
		scanf("%d",&n);
		
		int katisum[100][100]={0},weitsum[100][100]={0};
		for(int x=0;x<n;x++)
		{
			scanf("%d,%d",&kati[x],&weit[x]);
		}
		for(int y=0;y<n;y++)
		{
			for(int z=y;z<n;z++)
			{
				for(int r=y;r<=z;r++)
				{
					katisum[y][z]+=kati[r];
					weitsum[y][z]+=weit[r];
				}
			}
		}

		for(int y=0;y<n;y++)
		{
			for(int z=0;z<n;z++)
			{
				if(w>=weitsum[y][z]&&katik<katisum[y][z])
				{
					weitk=weitsum[y][z];
					katik=katisum[y][z];
				}
				
			}
		}
		for(int y=0;y<n;y++)
		{
			for(int z=0;z<n;z++)
			{
				if(katik==katisum[y][z]&&weitk>weitsum[y][z])
				{
					weitk=weitsum[y][z];
				}
			}
		}
		printf("Case %d:\n%d\n%d\n",c,katik,weitk);
	}
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define MP make_pair
#define PB push_back
#define REP(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long LL;
typedef vector<int> VI;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int dp[1001][1001];

int main(){
	int W,N,lp=1;
	while(true){
		cin >> W;
		if(W == 0) break;

		cin >> N;
		vector<pair<int,int> > data;
		REP(i,N){
			int a,b;
			char c;
			cin >> a >> c >> b;
			data.PB(MP(a,b));
		}

		

		REP(i,N+1){
			REP(j,W+1){
				dp[i][j] = -1;
			}
		}
		dp[0][0] = 0;

		REP(i, N){
			REP(j, W+1){
				dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
				if(dp[i][j] != -1 && data[i].second+j <= W){
					dp[i+1][data[i].second+j] = max(dp[i+1][data[i].second+j], dp[i][j]+data[i].first);
				}
			}
		}

		int ans1=0, ans2=0;
		REP(i,W+1){
			if(ans1 < dp[N][i]){
				ans1 = dp[N][i];
				ans2 = i;
			}
		}
		cout << "Case " << lp << ":" << endl;
		cout << ans1 << endl;
		cout << ans2 << endl;
		lp++;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
#include <cmath>
//#include <string>
//#include <sstream>
#include <iomanip>
//#include <complex>
#include <stdio.h>
//小数制度 	cout << fixed << setprecision(5) << tmp << endl;
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)

int dp[1010][1010];
int v[1010];
int w[1010];
int main(){
  int wei,n;
  int cnt = 1;
  while(cin >> wei,wei){
    cin >> n;
    REP(i,1010) REP(j,1010) dp[i][j] = 0;
    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<=wei;j++){
        if(j-w[i]<0) dp[i+1][j] = dp[i][j];
        else  dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
      }
    }
    /*for(int i=0;i<n+1;i++){
      for(int j=0;j<wei+1;j++){
        cout << dp[i][j] << " ";//l<<endl;
      }
      cout << endl;
    }*/
    int maxv=0;
    int ans=-1;
    for(int i=0;i<wei+1;i++){
      if(maxv < dp[n][i]){
        maxv = dp[n][i];
        ans = i;
      }
    }
    cout << "Case " << cnt << ":" <<endl;
    cout << maxv << endl << ans << endl;
    cnt++;
  }
}

C++: #include<stdio.h>
#include<string.h>
int w[1000],v[1000],W,N,dp[10000][1000],dpw[10000][1000];

bool solve(int num)
{
	memset(dp,0,sizeof(dp));
	memset(dpw,0,sizeof(dpw));
	scanf("%d",&W);
	if(W==0)
		return false;
	scanf("%d",&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=0;j<=W;j++)
		{
			if(j<w[i])
			{
				dp[i+1][j]=dp[i][j];
				dpw[i+1][j]=dpw[i][j];
			}
			else
			{
				int t1=dp[i][j], t2=dp[i][j-w[i]]+v[i], w1=dpw[i][j], w2=dpw[i][j-w[i]]+w[i];
				if(t1>t2||(t1==t2&&w1<w2))
				{
					dp[i+1][j]=t1;
					dpw[i+1][j]=w1;
				}
				else
				{
					dp[i+1][j]=t2;
					dpw[i+1][j]=w2;
				}
			}
		}
		printf("Case %d:\n%d\n%d\n",num,dp[N][W],dpw[N][W]);
		return true;
}

int main()
{
	for(int i=0;;i++)
		if(!solve(i+1))
			return 0;
}