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 <algorithm>
#include <utility>
#include <string>
#include <sstream>
#define rep(i, a, n) for(int i = a;i < n;i++)
using namespace std;


using P = pair<bool, int>;


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


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


int search(int i, int w){
	if(i == N) return 0;
	if(tw[i] <= w) return max(search(i+1, w-tw[i]) + tv[i], search(i+1, w));
	else if(tw[i] > w) return search(i+1, w);
	return 0;
}


int main(){
	int d = 0;
	while(d++, cin >> W, W){
		input();
		cout << search(0, W) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#define shosu(x) fixed<<setprecision(x)
using namespace std;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,P> pip;
const int inf=(1<<31)-1;

int W,N,num=0;
char t;
vi v,w,dp;

int main(){
	while(1){
		cin>>W;
		if(!W) break;
		num++;
		cin>>N;
		v=vi(N);
		w=vi(N);
		dp=vi(W+1,0);
		for(int i=0;i<N;i++){
			cin>>v[i]>>t>>w[i];
		}
		for(int i=0;i<N;i++){
			for(int j=W;j>=0;j--){
				if(j-w[i]<0) continue;
				else dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
			}
		}
		cout<<"Case "<<num<<':'<<endl;
		for(int i=W;i>0;i--){
			if(dp[i]!=dp[i-1]){
				cout<<dp[i]<<endl;
				cout<<i<<endl;
				break;
			}
		}
	}
}

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=kati[i].second; j<=w; j++){
				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++: #include <iostream>
#include <string>
#include <math.h>
#include <map>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <bitset>

using namespace std;



int main() {
    int furosiki;
    int case_count = 1;

    
    while(cin>>furosiki){
        if(furosiki==0){
            break;
        }
        
        
        
        int num;
        cin>>num;
        size_t bits;
        bits = num;
        bitset<1000> bs;
        
        
        int val[num];
        int wei[num];
        for(int i=0; i<num; i++)scanf("%d,%d",&val[i],&wei[i]);
        int binary_max=1;
        for(int i=0; i<num; i++)binary_max*=2;
        int max_val = 0;
        int max_wei = 0;
        for(int i=0; i<=binary_max; i++){
            bs = i;
            int temp_val=0;
            int temp_wei=0;
            for(int j=0; j<num; j++){
                temp_val+=bs[j]*val[j];
                temp_wei+=bs[j]*wei[j];
            }
            if(temp_wei<=furosiki){
                if(max_val<temp_val){
                    max_val=temp_val;
                    max_wei=temp_wei;
                }else if(max_val==temp_val && max_wei>temp_wei){
                    max_val=temp_val;
                    max_wei=temp_wei;
                }
            }
        }
        cout<<"case"<<case_count<<":"<<endl;
        cout<<max_val<<endl;
        cout<<max_wei<<endl;
        case_count++;
    }
    
    
    return 0;
}

C++: #include <iostream>

using namespace std;

int main() {
    int w, n;
    int value[1000], weight[1000];
    int dp[2][1001];
    for (int case_num=1; true; case_num++) {
        cin >> w;
        if (w == 0) {
            return 0;
        }
        cin >> n;
        for (int i=0; i<n; i++) {
            cin >> value[i];
            cin.ignore(1);
            cin >> weight[i];
        }
        for (int i=0; i<=1000; i++) {
            dp[1][i] = 0;
        }
        for (int i=0; i<n; i++) {
            for (int j=0; j<=w; j++) {
                if (weight[i] > j) {
                    dp[i%2][j] = dp[(i+1)%2][j];
                }
                else {
                    dp[i%2][j] = max(dp[(i+1)%2][j], dp[(i+1)%2][j-weight[i]]+value[i]);
                }
            }
        }
        for (int j=w; j>=0; j--) {
            if (dp[(n+1)%2][j] != dp[(n+1)%2][w]) {
                cout << "Case " << case_num << ':' << endl;
                cout << dp[(n+1)%2][w] << endl;
                cout << j+1 << endl;
                break;
            }
        }
    }
}

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iterator>
#include <cstdio>
#include <numeric>
using namespace std;
int main(){
  for(int tc = 1;; ++tc){
    int W;
    cin >> W;
    if(W == 0){
      break;
    }
    int n;
    cin >> n;
    int value[n], weight[n];
    for(int i = 0; i < n; ++i){
      scanf("%d,%d", &value[i], &weight[i]);
    }
    int table[n+1][W+1];    
    fill_n((int*)table, (n+1)*(W+1), 0);
    table[0][0] = 0; 
    for(int i = 0; i < n; ++i){
      for(int w = 0; w <= W; ++w){
	if(table[i][w] != -1){
	  table[i+1][w] = table[i][w];
	}
      }
      for(int w = weight[i]; w <= W; ++w){
	int tmp = w - weight[i];
	if(table[i][tmp] >= 0){
	  table[i+1][w] = max(table[i+1][w], table[i][tmp] + value[i]);
	}
      }    
    }
    int ans_w = 0, ans_v = 0;
    for(int w = 0; w <= W; ++w){
      if(ans_v < table[n][w]){
	ans_v = table[n][w];
	ans_w = w;
      }
    }
    cout << "Case " << tc << ":" << endl;
    cout << ans_v << endl;
    cout << ans_w << endl;
  }
  return 0;
}