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>

#define SIZE 300005
#define MOD 1000000007LL
#define INF 1 << 30
#define LLINF 1LL << 60
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define DOWN(i,b,a) for(int i=b;i>=a;i--)
#define SET(a,c) memset(a,c,sizeof a)
#define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FOREACH(i,c) for(auto (i) : (c))
#define BIT(i,j) ((i)>>(j))&1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end())
#define SQ(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef valarray<int> Array;
typedef pair<ll,ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i];if(i==a.size()-1)cout<<endl;else cout<<" ";}}
int gcd(int a,int b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);}
int lcm(int a,int b){return a==0||b==0?0:a*b/gcd(a,b);}

int W, N;
int v[1010];
int w[1010];
const int NMAX = 1000;
const int VMAX = 10000;
int dp[NMAX*VMAX+10];

void solve()
{
  int NV = N*10000;
  fill_n(dp,NV+1,INF);
  dp[0] = 0;
  REP(i,N) DOWN(j,NV,v[i])
    dp[j] = min(dp[j],dp[j-v[i]]+w[i]);
  DOWN(i,NV,0) if(dp[i] <= W)
  {
    printf("%d\n%d\n",i,dp[i]);
    return;
  }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  for(int i = 1; scanf("%d",&W), W; i++)
  {
    scanf("%d",&N);
    REP(i,N) scanf("%d,%d",&v[i],&w[i]);
    printf("Case %d:\n",i);
    solve();
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <functional>
#include <sstream>
#include <complex>
#include <climits>
using namespace std;

#define REP(i,a,n) for(int i=(a);i<(int)(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define EPS 1e-8

int dp[2][1010];

int main(){
	int w,n;
	pair<int,int> kati[1000];
	for(int T=1; scanf("%d",&w),w; T++){
		printf("Case %d:\n",T);
		memset(dp,0,sizeof(dp));
		scanf("%d",&n);
		rep(i,n){
			int a,b; scanf("%d,%d",&a,&b);
			kati[i].first = a;
			kati[i].second = b;
		}
		rep(i,n){
			int cur = i%2;
			int next = (i+1)%2;
			for(int j=0; j<=w; j++){
				if( kati[i].second>j ){
					dp[next][j] = dp[cur][j];
				}else{
					dp[next][j] = max(dp[next][j],
												max(dp[cur][j-kati[i].second]+kati[i].first,dp[cur][j]));
				}
			}
			rep(i,w+1)dp[cur][i]=0;
		}
		int sum=0,omosa=0;
		rep(i,w+1)if( sum<dp[n%2][i] ){
			sum = dp[n%2][i];
			omosa = i;
		}
		printf("%d\n%d\n",sum,omosa);
	}
	return 0;
}

C++: //参考(http://algorithms.blog55.fc2.com/blog-category-6.html)
#include<cstdio>

using namespace std;

int ka[1001],wei[1001],data[1001][1001];
int main(void){
	int w,n,set=0,maxw;
	int i,j,z;

	while(1){
		scanf("%d",&w);
		if(w==0)	break;
		
		maxw=0;
		set++;
		//初期化
		for(i=0;i<1001;i++){
			ka[i]=0;
			wei[i]=0;
			for(j=0;j<1001;j++)	data[i][j];
		}

		//入力
		scanf("%d",&n);
		for(i=1;i<=n;i++){
			scanf("%d,%d",&ka[i],&wei[i]);
		}
		
		//処理
		for(i=1;i<=n;i++){
			for(j=1;j<=w;j++){
				if(j<wei[i])	data[i][j]=data[i-1][j];
				else{
					if(data[i-1][j] < data[i-1][j-wei[i]]+ka[i]){
						data[i][j]=data[i-1][j-wei[i]]+ka[i];
					}
					else{
						data[i][j]=data[i-1][j];
					}
				}
			}
		}
		i--;
		j--;
		for(z=j-1;z>0;z--){
			if(data[i][j]==data[i][z])	j=z;
		}
		while(1){
			if(data[i][j]==0)	break;
			if(data[i][j]==data[i-1][j])	i--;
			else{
				maxw+=wei[i];
				j=j-wei[i];
				i--;
			}
		}
		printf("Case %d:\n%d\n%d\n",set,data[n][w],maxw);
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  for(int c=1,w; cin>>w,w; c++){
    int n,t[1001][2],dp[1001][1001]={0},m=0,ans;
    cin >>n;
    for(int i=1; i<=n; i++) scanf("%d,%d",&t[i][0],&t[i][1]);
    for(int i=1; i<=n; i++){
      for(int j=0; j<=w; j++){
	if(j>=t[i][1]) dp[i][j] = max(dp[i-1][j],dp[i-1][j-t[i][1]]+t[i][0]);
	else dp[i][j] = dp[i-1][j];
      }
    }
    for(int i=0; i<=w; i++){
      if(m<dp[n][i]){m = dp[n][i];ans = i;}
    }
    cout <<"Case "<<c<<":"<<endl<<m<<endl<<ans<<endl;
  }
  return 0;
}

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

int main()
{
	int w, n, i, j, cs, val[1001], wg[1001], dp[1001], inserted[1001];
	cs = 1;
	
	while( cin >> w , w)
	{
		memset( dp, 0, sizeof(dp));
		memset( val, 0, sizeof(val));
		memset( wg, 0, sizeof(wg));
		memset( inserted, 0, sizeof(inserted));
		
		inserted[0] = 1;
		
		cin >> n;
		
		for( i = 1; i <= n; i++)
		{	
			char c;
			cin >> val[i] >> c >> wg[i];
		}
		
		
		for( j = 1; j <= n; j++)
			for( i = 0; i <= w; i++)
				if( inserted[i] && i+wg[j] <= w && dp[i+wg[j]] < dp[i] + val[j] )
				{
						dp[i+wg[j]] = max( dp[i] + val[j], dp[i+wg[j]] );
						inserted[i+wg[j]] = 1;
						i = (i+wg[j]) + 1;
				}
		
		for( i = w; i >= 0; i--)
		{
			if( dp[i] > dp[i-1] && w != 0)
			{
				cout << "Case " << cs << ":" << endl;
				cout << dp[i] << endl;
				cout << i << endl;
				cs++;
				break;
			}
		}
		
	}
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){

	int w,n,cnt=1;
	int dp[100][1005],V[1001],W[1001];
	while(cin>>w,w){
		cin>>n;
		for(int i=0;i<n;i++)scanf("%d,%d",&V[i],&W[i]);
		
		for(int i=1;i<=n;i++){
			for(int j=0;j<=w;j++){
				if(j-W[i-1]>=0)dp[i][j]=max(dp[i-1][j],dp[i-1][j-W[i-1]]+V[i-1]);
			}
		}
		int tmp=w;
		for(int i=w;i>=0;i--)if(dp[n][i]==dp[n][w])tmp=i;
		cout<<"Case "<<cnt<<":"<<endl<<dp[n][w]<<endl<<tmp<<endl;
		cnt++;
		for(int i=0;i<100;i++)for(int j=0;j<=1000;j++)dp[i][j]=0,V[i]=W[i]=0;
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
#define W_MAX 1000
#define N_MAX 1000

int W, N, dp[N_MAX + 1][W_MAX + 1], w[N_MAX + 1], v[N_MAX + 1];

int main(){
	int c = 1;
	while (1){
		scanf("%d %d", &W, &N);
		if (W == 0)
			break;
		for (int i = 1; i <= N; ++i){
			char ch = ',';
			scanf("%d %s %d", &v[i], &ch, &w[i]);
		}
		for (int i = 1; i <= W; ++i)
			dp[0][i] = 0;
		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] = std::max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
				}
			}
		}
		int minv = 1;
		for (int i = 0; i <= W; ++i){
			if (dp[N][minv] < dp[N][i])
				minv = i;
		}
		printf("%d %s \n", "Case", c, ":");
		printf("%d\n", dp[N][W]);
		printf("%d\n", minv);
		++c;
	}
	return 0;
}