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++: #define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

int main() {
	int W, N;

	for (int h = 1; cin >> W&& W; h++) {
		cin >> N;
		int v, w;
		int dp[1001] = {};
		for (int i = 0; i < N; i++) {
			char c;
			cin >> v >> c >> w;
			for (int j = W; j > 0; j--) {
				if (j - w < 0)continue;
				dp[j] = max(dp[j], dp[j - w] + v);
			}
		}
		int a = 0, b = 0;
		for (int i = W; i >= 0; i--) {
			if (a < dp[i]) {
				a = dp[i];
				b = i;
			}
		}
		cout << "Case " << h << ":" << endl;
		cout << a << endl;
		cout << b << endl;
	}
	return 0;
}

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

int cnt, W, N, w[1000], v[1000], dp[1000][1001];

void solve(){
    for(int i = 0; i < N; i++){
        dp[i+1][w[i]] = v[i];
        for(int j = w[i]+1; j <= W; j++){
            if( dp[i][j-w[i]] ){
                dp[i+1][j] = dp[i][j-w[i]] + v[i];
            }
        }
    }
    int c,v = 0;
    for(int i = 0; i < W; i++){
        if( dp[N][i] > v ){
            v = dp[N][i];
            c = i;
        }
    }
    printf("Case %d:\n%d\n%d\n", cnt, v, c);
}

int main(){
    cnt = 0;
    while( true ){
        cnt++;
        scanf("%d", &W);
        if( !W ) break;
        scanf("%d", &N);
        for(int i = 0; i < N; i++){
            scanf("%d,%d", v+i, w+i);
        }
        solve();
    }
    return 0;
}

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

using namespace std;

int main( void )
{
	int map[1001], save[1001];
	int w; long long count;
	count = 1;
	while(cin >> w, w) {
		++w;
		for(int i=0; i < w; ++i) map[i] = save[i] = 0;
		int n, value, weight;
		cin >> n;
		scanf("%d,%d", &value, &weight);
		save[weight] = map[weight] = max(save[weight], value);
		for(int i=1; i < n; ++i) {
			scanf("%d,%d", &value, &weight);
			save[weight] = max(save[weight], value);
			for(int j=0; j < w; ++j) {
				if(map[j]) {
					int ww = j + weight;
					if(ww < w)
						save[ww] = max(save[ww], map[j] + value);
					else
						save[j] = max(save[j], map[j]);
				}
			}
			for(int j=0; j < w; ++j) {
				map[j] = save[j]; save[j] = 0;
			}
		}
		int Max = 0, idx;
		for(int i=0; i < w; ++i) {
			if(Max < map[i]) {
				Max = map[i];
				idx = i;
			}
		}
		cout << "Case " << count++ << ':' << endl;
		cout << Max << endl;
		cout << idx << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>

using namespace std;

#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define rep(i,n) for(int i=0;i<(n);i++)
#define tr(it,container) for(typeof(container.begin()) it = container.begin(); \
                             it != container.end(); ++it)
#define mp(a,b) make_pair((a),(b))
#define eq ==

typedef long long ll;
typedef complex<double> point;

// up right down left
const int dy[] = {-1,0,1,0};
const int dx[] = {0,1,0,-1};

const double EPS = 1e-9;
const int days[]     = {31,28,31,30,31,30,31,31,30,31,30,31};
const int daysleap[] = {31,29,31,30,31,30,31,31,30,31,30,31};

int main(){

    for(int loop = 1;;loop++){
        int N,W;
        cin >> W;
        if(W == 0) break;
        cin >> N;
        // value,weight
        vector<pair<int,int> > T(N);
        char c;
        rep(i,N) cin >> T[i].first >> c >> T[i].second;
        vector<vector<int> > DP(N,vector<int>(W+1));

        for(int i=0;i<=W;i++){
            if(T[0].second <= i){
                DP[0][i] = T[0].first;
            }
        }

        for(int i=1;i<N;i++){
            for(int j=0;j<=W;j++){
                if(0 <= j-T[i].second){
                    DP[i][j] = max(DP[i-1][j],DP[i-1][j-T[i].second]+T[i].first);
                }else{
                    DP[i][j] = DP[i-1][j];
                }
            }
        }

        int maxi = -1;
        int maxk = -1;
        for(int i=0;i<=W;i++){
            if(maxi < DP[N-1][i]){
                maxi = DP[N-1][i];
                maxk = i;
            }
        }
        cout << "Case " << loop << ":" << endl;
        cout << maxi << endl << maxk << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (int i = (int)a; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MOD 1000000007
#define INF 1000000000
#define PI 3.14159265359
#define EPS 1e-12

int W, N, v[1010], w[1010], dp[1010];

int main(void) {
  int ca = 1;
  while (true) {
    cin >> W;
    if(W == 0) break;
    cin >> N;
    REP(i, N) {
      cin >> v[i];
      getchar();
      cin >> w[i];
    }

    for (int i = 0; i < N; i++) {
      for (int j = W; j >= w[i]; j--) {
        dp[j] = max(dp[i], dp[j - w[i]] + v[i]);
      }
    }

    int a = -INF, b = 0;
    for (int i = 0; i < W; i++) {
      if (a < dp[i]) {
        a = dp[i];
        b = i;
      }
    }
    cout << "Case " << ca << ":" << endl << a << endl << b << endl;
    ca++;
  }
  return 0;
}

C++: // 重さの総和W以下, 同率なら軽い方を出力

#include <iostream>
#include <cstring>

using namespace std;

const int N_MAX=1000;
const int W_MAX=10000;
int value[N_MAX], weight[N_MAX];

int memoW[W_MAX];
int NUM;
int VTOTAL=-1, WTOTAL=9999999;
int W;

int max(int a, int b);
int maximumValue(int v, int w, int n); // v:現在の価値, w:現重量 n番目の要素に対して

int main() {
  int caseN=0;
  int flag=0;
  while(flag==0) {
    VTOTAL=-1;
    WTOTAL=9999999;
    cin>>W;
    if(W==0) {
      flag=1;
      break;
    }
    cin>>NUM;
  caseN++;
    for(int i=0; i<NUM; i++) {
      cin>>value[i]; // 価値
      cin.ignore(); // 1文字読み飛ばし(",")
      cin>>weight[i]; // 重さ
    }
    memset(memoW, -1, sizeof(memoW));
    cout<<"case "<<caseN<<":"<<endl;
    cout<<maximumValue(0, 0, 0)<<endl;
    cout<<WTOTAL<<endl;
  }
}

int max(int a, int b) {
  if(a>b) {
    return a;
  }
  else {
    return b;
  }
}

int maximumValue(int v, int w, int n) {
  if(n==NUM) { // 最深部
    if(v>VTOTAL) {
      VTOTAL=v;
      WTOTAL=w;
    }
    if(v==VTOTAL && w<WTOTAL) {
      WTOTAL=w;
    }
    return v;
  }
  else if(w+weight[n]<=W) {
    return max(maximumValue(v, w, n+1), maximumValue(v+value[n], w+weight[n], n+1));
  }
  else {
    return maximumValue(v, w, n+1);
  }
}