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>
#include<cmath>
#include<map>
#include<cstdio>
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a; i ++)
int main(void){
  int w;
  int n;
  int num=0;
  while(cin>>w,w){
    cout<<"Case "<<++num<<":"<<endl;
    cin>>n;
    vector<pair<int,int> > in(n);
    rep(i,n){
      int a,b;
      scanf("%d,%d",&a,&b);
      in[i]=make_pair(a,b);
   }    
    int resw = 0;
    int resm = 0;
    int last = 1<<n;
    for(int i = 1 ; i < last+1 ; i ++){
      //cout<<i<<endl;
      int nw = 0;
      int money = 0;
      rep(j,n){
	bool use = (i/(1<<j))%2;

	//cout<<use<<" ";
	if(use){
	  money += in[j].first;
	  nw += in[j].second;
	}
      }
      if(nw>w){
	//cout<<"con"<<endl;
	continue;
      }
      
      //cout<<endl;
      if(resm<money){
	//cout<<endl;
	  
	resm = money;
	resw = nw;
      }
      if(money==resm && resw>nw){
	resw = nw;
      }
      //cout<<i<<" "<<resw<<endl;
      //cout<<endl;
    }
    cout<<resm<<endl<<resw<<endl;

    //*/
  }
}

C++: #include <iostream>
#include <stdio.h>

void update(int *dst, int src) {
	if (*dst < src) *dst = src;
	return;
}

int main() {
	for (int cnt = 1; 1; cnt++) {
		// 入力
		int W, N, Value[1000], Weight[1000];
		std::cin >> W;
		if (W == 0) return 0;
		std::cin >> N;
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &Value[i], &Weight[i]);
		}
		// 演算
		int dp[1001][1001];
		for (int i = 0; i <= W; i++) {
			dp[0][i] = 0;
		}
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				update(&dp[i + 1][j], dp[i][j]);
				if (j + Weight[i] <= W) {
					update(&dp[i + 1][j + Weight[i]], dp[i][j] + Value[i]);
				}
			}
		}
		// 出力
		std::cout << "Case " << cnt << ":" << std::endl;
		int MaxValue = 0, MaxIndex = 0;
		for (int i = 0; i <= W; i++) {
			if (MaxValue < dp[N][i]) {
				MaxValue = dp[N][i];
				MaxIndex = i;
			}
		}
		std::cout << MaxValue << std::endl;
		std::cout << MaxIndex << std::endl;
	}
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <complex>
#include <math.h>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(long i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

const long double EPS = 1e-12;
const LL INF = 1e18;


long dp[1002];
long w[1001],v[1001];


int main(){
	int maxw,N;
	int cnt=1;
	while(cin>>maxw){
		if(maxw==0)break;
		cin>>N;
		CLR(dp);
		char c;
		REP(i,N)cin>>v[i]>>c>>w[i];
		
		REP(i,N){
			for(int j=maxw;j>=0;j--){
				if(j+w[i]<=maxw){
					dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i]);
				}
			}
		}
		cout<<"Case "<<cnt<<":"<<endl;
		cout<<dp[maxw]<<endl;
		long res=0;
		REP(i,maxw+1){
			if(dp[i]==dp[maxw]){
				res=i;break;
			}
		}
		cout<<res<<endl;
		cnt++;
	}
	return 0;
}
/*

*/

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

using namespace std;


void Solution()
{
    int n;
    int weight;
    int num = 1;
    
    while(cin>>weight, weight)
    {
        cin >> n;
        
        int dp[1000]={0};
        
        int v,w;
        char comma;
        
        while(n--)
        {
            cin>>v>>comma>>w;
            
            for(int i = weight - w; i >= 0; i--)
            {
                dp[i + w] = max(dp[i + w], dp[i]+v);
            }
        }
        
        int mi = weight;
        
        for(int i = weight; i >= 0; i--)
        {
            if(dp[i] != dp[mi])
            {
                cout << "Case " << num++ << ":\n";
                cout << dp[mi] << "\n" << mi << "\n";
                break;
            }
            
            mi = i;
        }
    }
}

int main()
{
    Solution();
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;

int value[1001];
int cost[1001];
int dp[1001][1001];

void cut(string s,int& a,int& b){
	for(int i=0; i<s.size(); ++i){
		if(s[i]==','){
			string tmp1=s.substr(0,i);
			string tmp2=s.substr(i+1);
			a=atoi(tmp1.c_str());
			b=atoi(tmp2.c_str());
			break;
		}
	}
}

int main(){
	int W,N,cnt=1;
	while(cin>>W&&W){
		cin >> N;
		for(int i=0; i<N; ++i){
			for(int j=0; j<=1000; ++j){
				dp[i][j]=INF;
			}
		}
		for(int i=0; i<N; ++i){
			string tmp;
			cin >> tmp;
			cut(tmp,value[i],cost[i]);
			//cout << value[i] << " " << cost[i] << "\n";
		}
		dp[0][value[0]]=cost[0];
		dp[0][0]=0;
		for(int i=1; i<N; ++i){
			for(int j=0; j<=1000; ++j){
				if(j-value[i]>=0){
					if(dp[i-1][j-value[i]]+cost[i]<=W){
						dp[i][j]=min(dp[i-1][j-value[i]]+cost[i],dp[i-1][j]);
					} else {
						dp[i][j]=dp[i-1][j];
					}
				} else {
					dp[i][j]=dp[i-1][j];
				}
			}
		}
		for(int i=1000; i>=0; --i){
			if(dp[N-1][i]<INF){
				cout << "Case " << cnt << ":\n" << i << "\n" << dp[N-1][i] << "\n";
				break;
			}
		}
		++cnt;
	}
	return 0;
}