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 <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <map>
#define INF 100000000
#define rep(i,a) for(int i=0;i<(a);i++)

using namespace std;
typedef long long ll;

int dp[2000][20000];    //dp[i][j]  i番目以前の物で、重さがj以下の時の価値
int W[2000],V[2000];

int w,n;

void solve(){
    for(int i=n-1;i>=0;i--){
        for(int j=0;j<=w;j++){
            if(j-W[i]<0){
                dp[i][j]=dp[i+1][j];
            }
            else{
                dp[i][j]=max(dp[i+1][j],dp[i+1][j-W[i]]+V[i]);
            }
            
        }
    }
    
    cout<<dp[0][w]<<endl;
    for(int i=w;i>=1;i--){
        if(dp[0][i]!=dp[0][i-1]){
            cout<<i<<endl;
            break;
        }
    }
}

int main(){
    int count=1;
    while(1){
        memset(dp,0,sizeof(dp));
    
        cin>>w>>n;
        if(w==0)break;
        char a;
        rep(i,n)cin>>V[i]>>a>>W[i];
    
        cout<<"Case "<<count<<":"<<endl;
        count++;
        solve();
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#define rep(i, a, n) for(int i = a;i < n;i++)
using namespace std;


int W, N;
int tv[1010];
int tw[1010];
int dp[1010][1010];


void input(){
	string s;
	stringstream ss;
	cin >> N;
	rep(i, 1, N+1){
		cin >> s;
		replace(s.begin(), s.end(), ',', ' ');
		ss.clear();
		ss << s;
		ss >> tv[i] >> tw[i];
	}
}


void makeDP(){
	rep(i, 0, N){
		rep(j, 0, W+1){
			if(tw[i+1] > j) dp[i+1][j] = dp[i][j];
			else dp[i+1][j] = max(dp[i][j-tw[i+1]] + tv[i+1], dp[i][j]);
		}
	}
}

int main(){
	int d = 0;
	while(d++, cin >> W, W){
		input();
		makeDP();
		auto v = max_element(&dp[N][0], &dp[N][W+1]);
		auto w = v - &dp[N][0];

		cout << "Case " << d << ':' << endl;
		cout << *v << endl;
		cout << w << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
main(){
  int b,c,N,K[1010],P[1010],i,m,j,W,dp[1010],h=1,y,g;
  char d;
  while(1){
    b=0;
    cin >> W;
    for(i=0;i<=W;i++){
      dp[i]=0;
    }
    if(W==0){
      break;
    }
    cin >> N;
    for(i=0;i<N;i++){
      cin >> P[i] >> d >> K[i];
      c=K[i];
      dp[K[i]]=P[i];
      for(j=W;j>=1;j--){
	if(j!=c){
	  if(dp[j]!=0){
	    if(dp[c+j]!=0){
	      if(dp[c+j]<dp[c]+dp[j]){
		dp[c+j]=dp[c]+dp[j];
	      }
	      else{
		continue;
	      }
	    }
	    if(dp[c+j]==0){
	      dp[c+j]=dp[c]+dp[j];
	    }
	  }
	}
      }
    }
    for(i=1;i<=W;i++){
      if(dp[i]>b){
	y=i;
	b=dp[i];
      }
    }
    cout << "Case:" << " " << h << endl;
    cout << b << endl << y << endl;
    h++;
  }
}

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

using namespace std;

void thief(int num,int wei,int total);

int value[1000],weight[1000];
int W,N;
int maxvalue,maxweight;

int main(){
	int ans;
	int Case =1;
	while(cin >> W,W!=0){
		cin>>N;
		maxvalue=0;
		maxweight=1000;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&value[i],&weight[i]);
		}
		thief(0,0,0);
		cout<<"Case "<<Case<<':'<<endl;
		cout<<maxvalue<<endl;
		cout<<maxweight<<endl;
		Case++;
	}
	return 0;
}

void thief(int num,int wei,int total)
{
	if(num==N || wei>W){
		if(maxvalue<=total){
			if(maxvalue == total){
				if(maxweight>wei)
					maxweight=wei;
			}
			else maxweight=wei;
			maxvalue=total;
		}
		return;
	}
	if(wei+weight[num]<W) thief(num+1,wei+weight[num],total+value[num]);
	thief(num+1,wei,total);
	return;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>
#include<set>
 
using namespace std;
 
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define INF 1<<25
 
typedef long long ll;

int dy[8]={1,1,1,0,0,-1,-1,-1};
int dx[8]={-1,0,1,-1,1,-1,0,1};
struct S{
	int a,b,c;
};
bool asc(const S& left,const S& right){
	return left.c > right.c;
}

int W,N;
int v[1001],w[1001];
int dp[1001][1001];

int main(){
	int cnt=1;
	char tmp;
	while(cin>>W){
		if(W==0)break;
		cin>>N;
		rep(i,N)cin>>v[i]>>tmp>>w[i];
		REP(i,1,N+1){
			rep(j,W+1){
				if(w[i-1]<=j)
					dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i-1]]+v[i-1]);
				else 
					dp[i][j]=dp[i-1][j];
			}
		}
		int ans_v=0,ans_w=0;
		rep(i,W+1){
			if(dp[N][i]>ans_v){
				ans_v=dp[N][i];
				ans_w=i;
			}
		}
		cout<<"Case "<<cnt<<":"<<endl;
		cout<<ans_v<<endl;
		cout<<ans_w<<endl;
		cnt++;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int main(void){
  int W,N,v[1001],w[1001];
  int cas = 1;
  while(cin>>W){
    if(W==0) break;
    int dp[1001]={0};
    printf("Case %d:\n",cas++);
    cin>>N;
    REP(i,N){
      scanf("%d,%d",&v[i],&w[i]);
    }
   for(int i=0; i<N; i++){
     for(int j=W;j-w[i]>=0;j--){
      dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
     }
   }
  printf("%d\n",dp[W]);
  for(int i=0; i<=W; i++){
    if(dp[i]==dp[W]){
      printf("%d\n",i);
      break;
    }
  }

  }
  return 0;
}

C++: #include<cstdio>

int main(){
	int w,n,count=1;
	scanf("%d",&w);
	while(w){
		scanf("%d", &n);
		int a[n][2];
		for(int i = 0; i < n; i++){
			scanf("%d,%d", &a[i][0], &a[i][1]);
		}
		unsigned long long c = 1 << n,b;
		int max = 0,weight, weightm = 0,sum;
		while(c){
			sum = 0;
			weight = 0;
			b = 1;
			for(int i = 0; i < n; i++){
				if(c & b){
					sum += a[i][0];
					weight += a[i][1];
				}
				if(weight > w){
					break;
				}
				if(sum > max){
					max = sum;
					weightm = weight;
				}
				if(sum == max && weight < weightm){
					weightm = weight;
					printf("%d\n", weight);
				}
				b = b << 1;
			}
			c--;
		}
		printf("Case %d:\n%d\n%d\n", count++, max, weightm);
		scanf("%d", &w);
	}

}