Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

const MaxW = 1000
const MaxN = 1000

type Tuple struct {
	Value  int
	Weight int
}

func nextLine(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func solve(w int, n int, vws []Tuple) Tuple {
	var dp [MaxN + 1][MaxW + 1]int

	for i := 0; i < n; i++ {
		for j:= w; j > 0; j-- {
			if j < vws[i].Weight {
				dp[i + 1][j] = dp[i][j]
			} else {
				dp[i + 1][j] = int(math.Max(float64(dp[i][j]), float64(dp[i][j - vws[i].Weight] + vws[i].Value)))
			}
		}
	}

	maxValue := dp[n][w]
	minWeight := w

	for j := w - 1; j > 0; j-- {
		if maxValue == dp[n][j] {
			minWeight = j
		} else {
			break
		}
	}

	return Tuple{Value: maxValue, Weight: minWeight}
}

func main() {
	var sc = bufio.NewScanner(os.Stdin)
	index := 1
	for {
		var w, n int
		w, _ = strconv.Atoi(nextLine(sc))
		if w == 0 {
			return
		}
		n, _ = strconv.Atoi(nextLine(sc))

		vws := make([]Tuple, n, n)
		for i := 0; i < n; i++ {
			array := strings.Split(nextLine(sc), ",")
			v, _ := strconv.Atoi(array[0])
			w, _ := strconv.Atoi(array[1])
			vws[i] = Tuple{Value: v, Weight: w}
		}

		t := solve(w, n, vws)
		fmt.Printf("Case %d:\n", index)
		fmt.Println(t.Value)
		fmt.Println(t.Weight)
		index++
	}
}

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;
			}
		}
	}
}