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>

using namespace std;
int N;
int W;
vector<int> takaraW;
vector<int> takaraK;
vector<int> dp;

int main(){
    int kl = 0;
    while(true){
        takaraK.clear();
        takaraW.clear();
        dp.clear();
        cin >> W;
        if(W==0) return 0;
        cin >> N;
        takaraK.resize(N);
        takaraW.resize(N);
        dp.resize(W+1);
        for(int i=0;i<N;i++){
            char comma;
            cin >> takaraK[i] >> comma >>  takaraW[i];
        }
        for(int i=0;i<N;i++){
            for(int j=0;j<W;j+=takaraW[i]+1){
                int neww = takaraW[i]+j;
                int newk = takaraK[i]+dp[j];
                int oldk = dp[neww];
                if(neww > W) continue;
                if(oldk < newk){
                    dp[neww] = newk;
                }
            }
        }
        int mw=W+2,mk=-1;
        for(int i=0;i<=W;i++){
            if(mk < dp[i]){
                mk = dp[i];mw = i;
            }
        }
        cout << "Case " << kl++ << endl;
        cout << mk << endl;
        cout << mw << endl;
    }
    return 0;
}

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

#define rep(i,n) for(int i=0; i<n; i++)
int dp[1001][1001];

int main(){
	int W, cnt=1;
	while( cin >> W, W ){
		int n, v[1000], w[1000];
		memset( dp, -1, sizeof(dp) );
		cin >> n;
		rep(i,n){
			scanf("%d,%d", &v[i], &w[i]);
		}
		dp[0][0] = 0;
		rep(i,n){
			rep(j,W+1){
				if( j<w[i] )
					dp[i+1][j] = dp[i][j];
				else
					dp[i+1][j] = max( dp[i][j], dp[i][j-w[i]]+v[i] );
			}
		}
		int mw = 0;
		rep(i,W+1){
			if( dp[n][i]>dp[n][mw] )	mw = i;
		}
		cout << "Case " << cnt << ":" << endl;
		cnt++;
		cout << dp[n][mw] << endl;
		cout << mw << endl;
	}
}

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

void sort( int* a, int* b, int n )
{
    for( int i = 0; i < n - 1; i++ )
        for( int j = i + 1; j < n; j++ )
            if( a[i] > a[j] )
            {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;

                temp = b[i];
                b[i] = b[j];
                b[j] = temp;
            }
}

int main()
{
    int w, n, cnt = 0;
    char c;

    while( cin >> w )
    {
        if( w == 0 )
            break;

        cin >> n;

        cnt++;

        int val[1000], we[1000];

        for( int i = 0; i < n; i++ )
            cin >> val[i] >> c >> we[i];

        sort( we, val, n );

        int sum1 = 0, sum2 = 0, max1 = 0, max2 = 0, k = 1;
        for( int i = 0; i < n - 1; i++ )
        {
            sum1 = we[i];
            sum2 = val[i];
            for( int j = i + k; j < n; j++ )
            {
                if( sum1 + we[j] <= w )
                {
                    sum1 += we[j];
                    sum2 += val[j];

                    if( sum2 > max2 )
                    {
                        max2 = sum2;
                        max1 = sum1;
                    }
                    else if( sum2 == max2 )
                    {
                        if( sum1 < max1 )
                        {
                            max2 = sum2;
                            max1 = sum1;
                        }
                    }
                    else;
                }
                else
                {
                    if( sum2 > max2 )
                    {
                        max2 = sum2;
                        max1 = sum1;
                    }
                    else if( sum2 == max2 )
                    {
                        if( sum1 < max1 )
                        {
                            max2 = sum2;
                            max1 = sum1;
                        }
                        else
                        {
                            sum1 = we[i];
                            sum2 = val[i];
                            k++;
                            j = i + k;
                        }   
                    }
                    else
                    {
                        sum1 = we[i];
                        sum2 = val[i];
                        k++;
                        j = i + k;
                    }
                }
            }
            //cout << "sum1: " << sum1 << " sum2: " << sum2 << endl;
            //cout << "max1: " << max1 << " max2; " << max2 << endl;
        }
        cout << "Case " << cnt << ":" << endl << max2 << endl << max1 << endl;

    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define ALL(C)     (C).begin(),(C).end()


int table[1000][1001];
void solve(int lim,int n,int *weight,int *value){
  rep(i,n){
    rep(j,lim+1){
      table[i][j]=0;
    }
  }
  

  if (weight[0] <= lim)table[0][weight[0]]=value[0];
  
  REP(i,1,n){
    rep(j,lim+1){
      table[i][j]=table[i-1][j];
      if (j-weight[i]<0)continue;
      table[i][j]=max(table[i][j],table[i-1][j-weight[i]]+value[i]);
    }
  }
  
  int maxi=0,answ;
  rep(i,lim+1){
    if (table[n-1][i] > maxi)maxi=table[n-1][i],answ=i;
  }
  cout << maxi<<endl;
  cout << answ << endl;
}

main(){
  int n,w;
  int value[1000],weight[1000];
  int tc=1;
  while(cin>>w>>n && w){
    char dummy;
    rep(i,n){
      cin>>value[i]>>dummy>>weight[i];
    }
    cout << "Case "<< tc++ << ":" << endl;

    solve(w,n,weight,value);
  }
  return false;
}

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;
vvi dp;

int main(){
	while(1){
		cin>>W;
		if(!W) break;
		num++;
		cin>>N;
		v=vi(N);
		w=vi(N);
		dp=vvi(N+1,vi(W+1));
		for(int i=0;i<=W;i++) dp[0][i]=0;
		for(int i=0;i<N;i++){
			cin>>v[i]>>t>>w[i];
		}
		for(int i=1;i<=N;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<<"Case "<<num<<':'<<endl;
		for(int i=W;i>0;i--){
			if(dp[N][i]!=dp[N][i-1]){
				cout<<dp[N][i]<<endl;
				cout<<i<<endl;
				break;
			}
		}
	}
}