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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(itr,c) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define MAX 1010

typedef long long ll;

int W,N;
int w[MAX],v[MAX];
ll dp[MAX][MAX];

int main(){
  int i,j,cnt = 0;

  for(;;){
    scanf("%d",&W); if(W == 0) break;
    scanf("%d",&N);
    rep(i,N) scanf("%d,%d",&v[i],&w[i]);

    rep(i,MAX) rep(j,MAX) dp[i][j] = 0;

    int res = 0;
    for(i = 1; i <= N; i++){
      for(j = 1; j <= W; j++){
        if(j - w[i] < 0) dp[i][j] = dp[i-1][j];
        else {
          if(dp[i-1][j] < dp[i-1][j-w[i]] + v[i]){
            dp[i][j] = dp[i-1][j-w[i]] + v[i];
            res = j;
          }
          else dp[i][j] = dp[i-1][j];
        }
      }
    }

    printf("Case %d:\n%lld\n%d\n",++cnt,dp[N][W],res);
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w,n,v,w2;
  int cnt = 1;
  char c;
  while(cin >> w){
    if(w == 0) break;
    cin >> n;
    int dp[1001][1001] ={};
//     vector<vector<int>> dp2(n+1,vector<int>(w+1,0));
    REP(i,0,n){
      cin >> v>>c>>w2;
      rep(j,w){
        dp[i+1][j] = max(dp[i][j], j-w2 >= 0? dp[i][j-w2]+ v : 0);
      }
    }
    cout << "Case " + to_string(cnt) + ":"<<endl;
    rep(i,w+1){
      if(dp[n][i] == dp[n][w-1]){
        cout << dp[n][w-1]<<endl<<i<<endl;
        break;
      }
    }
    ++cnt;
  }
  return 0;
}

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

int W,N;
int dp[1001][1001]; // 宝の数×重さ
struct Obj {
	int price;
	int weight;
} obj[1001];

int main(void){
	int cnt = 1;
	while (cin >> W, W) {
		// input
		cin >> N;
		char buf;
		for (int i = 0; i < N; i++) {
			cin >> obj[i].price >> buf >> obj[i].weight;
		}
		
		// init
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = 0;
			}
		}
		dp[0][obj[0].weight] = obj[0].price;

		// calc
		int wei;
		for (int i = 0; i < N - 1; i++) {
			for (int j = 0; j <= W; j++) {
				if (j != 0 && dp[i][j] == 0) {
					continue;
				}
				// 下
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
				// 右下
				if (wei = j + obj[i + 1].weight, wei <= W) {
					dp[i + 1][wei] = max(dp[i + 1][wei], dp[i][j] + obj[i + 1].price);
				}
			}
		}
		
		struct Obj res = {0, 0};
		for (int i = 0; i <= W; i++) {
			for(int j = 0; j < N; j++) {
				if (res.price < dp[j][i]) {
					res.price = dp[j][i];
					res.weight = i;
				}
			}
		}

		// output
		printf("Case %d:\n%d\n%d\n", cnt, res.price, res.weight);
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define max_n 1000
#define max_w 10000
using namespace std;

int N,W;
int dp[max_n][max_w];
int dw[max_n][max_w];
int w[max_n],v[max_n];

int rec(int i,int j){
	int res;
	if(dp[i][j]!=-1)return dp[i][j];
	if(i==N){
		res=0;
		dw[i][j]=0;
	}else if(j<w[i]){
		res=rec(i+1,j);
		dw[i][j]=dw[i+1][j];
	}else{
		int r1=rec(i+1,j);
		int r2=rec(i+1,j-w[i])+v[i];
		if(r1>r2){
			res=r1;
			dw[i][j]=dw[i+1][j];
		}else if(r2>r1){
			res=r2;
			dw[i][j]=dw[i+1][j-w[i]]+w[i];
		}else{
			if(dw[i+1][j]>(dw[i+1][j-w[i]]+w[i])){
				res=r2;
				dw[i][j]=dw[i+1][j-w[i]]+w[i];
			}else{
				res=r1;
				dw[i][j]=dw[i+1][j];
			}
		}
	}
	return dp[i][j]=res;
}

int main(){
	int p=1;
	while(cin>>W,W){
		cin>>N;
		rep(i,N){
			scanf("%d,%d",&v[i],&w[i]);
		}
		memset(dp,-1,sizeof(dp));
		memset(dw,-1,sizeof(dw));
		cout<<"Case "<<p<<":"<<endl;
		cout<<rec(0,W)<<endl;
		cout<<dw[0][W]<<endl;
		p++;
		/*cout<<"value="<<rec(0,W)<<" cost="<<dw[0][W]<<endl;//????????§??????????????????
		cout<<"cost="<<dw[0][W]<<endl;*/
	}
	return 0;
}

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

using namespace std;


int main(void) {
	int W, N, i, j = 1, index, maxValue, minWeight;
	int result[1001], weight[1001], value[1001];
	char c;

	while (1) {
		cin >> W;
		if (W == 0) {
			break;
		}

		for (i = 0; i < 1001; i++) {
			value[i] = weight[i] = result[i] = 0;
		}

		cin >> N;
		for (i = 0; i < N; i++) {
			cin >> value[i] >> c >> weight[i];
		}
		
		for (i = 0; i < N; i++) {
			for (index = W; index >= weight[i]; index--) {
				result[index] = max(result[index], result[index - weight[i]] + value[i]);
			}
		}

		maxValue = result[W];
		for (i = 0; i < 1001; i++) {
			if (result[i] == maxValue) {
				minWeight = i;
				break;
			}
		}

		cout << "Case " << j << ":" << endl << maxValue << endl << minWeight << endl;
		j++;
	}

	return 0;
}