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<bits/stdc++.h>
using namespace std;

int n, w, value[1000], weight[1000], memo[1000][1000];
int dp(int val, int wei);

int main(){
  int data = 1;
  while(cin >> w, w){
    memset(memo, 0, sizeof(memo));
    cin >> n;
    for(int i = 0; i < n; i++){
      scanf("%d,%d", &value[i], &weight[i]);
    }

    int wgtsum = 0;
    int maxi = dp(0, w);
    for(int i = n-1; i >= 0 && !maxi; i++){
      for(int j = n-1; j >= 0 && !maxi; j++){
	if(memo[i][j] == maxi) wgtsum = i;
      }
    }
      
    cout << "Case " << data << ":" << endl;
    cout << maxi << endl;
    cout << w - wgtsum - 1 << endl;
    data++;
  }
}

int dp(int val, int wei){
  if(memo[wei][val]) return memo[wei][val];
  if(val >= n) return 0;
  if(wei < weight[val]) return memo[wei][val] = dp(val+1, wei);
  else return memo[wei][val] = max(dp(val+1, wei-weight[val])+value[val], dp(val+1, wei));
}

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

const int INF = 1 << 29;

int main() {
    int W, N, v[1000], w[1000];

    int n_case = 0;
    while (true) {
        ++n_case;
        cin >> W;
        if (W == 0) break;
        cin >> N;
        char ch;
        for (int j = 0; j < N; ++j) {
            cin >> v[j] >> ch >> w[j];
        }

        int dp[1001];
        fill(dp, dp+W+1, -INF);
        dp[0] = 0;

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

        int max_v = -INF, ww;
        for (int j = 0; j <= W; ++j) {
            if (max_v < dp[j]) {
                max_v = dp[j];
                ww = j;
            }
        }
        cout << "Case " << n_case << ":" << endl;
        cout << max_v << endl;
        cout << ww << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

int main(void){
  int W,N,cnt = 0;
  while(cin>>W,W){
    cin>>N;
    vector<int> w(N);
    vector<int> v(N);
    long long dp[1001][1001];
    memset(dp,0,sizeof(dp));
    
    rep(i,N)scanf("%d,%d",&v[i],&w[i]);

    for(int i = N-1 ; i >= 0 ; i --){
      rep(j,W+1){
	if(j < w[i]) 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 "<<++cnt<<":"<<endl;
    cout<<dp[0][W]<<endl;
    for(int i = W ; ; i--){
      if(dp[0][i] != dp[0][W]){
	cout<<i+1<<endl;
	break;
      }
    }
  }

  return 0;
}

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

const int MAX_N = 1000;
const int MAX_W = 1000;
const int MAX_V = 10000;
int dp[MAX_N][MAX_W];
int w[MAX_N];
int v[MAX_N];

#define rep(i,a,b) for(int i=(a); i<(b); i++)

// // i未満の宝から価格がj以下になるように買った時の価格の最大値
// int solve(int i , int j){
//   int res;
//   if(dp[i][j] == -1){
//     if(i == 0){
//       res = 0;
//     } else if(j < w[i-1]){
//       res = solve(i-1,j);
//     } else {
//       res = max( solve(i-1,j) , solve(i-1,j-w[i-1])+v[i-1]);
//     }
//     dp[i][j] = res;
//   } else {
//     res = dp[i][j];
//   }
//   return res;
// }

int W;
int N;

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

int findw(){
  int i = N - 1;
  int j = W;
  int ret = 0;
  while(i >= 0 && j >= 0){
    if(j > w[i] && dp[i][j] < dp[i][j-w[i]] + v[i]){
      ret += w[i];
      j-=w[i];
    }
    i--;
    // printf("%d %d\n",i,j);
  }
  return ret;
}

int main(){
  int c=1;
  while(cin >> W , W){
    cin >> N;
    rep(i,0,N){
      scanf("%d,%d", &v[i], &w[i]);
    }
    memset(dp,0,sizeof(dp));
    printf("Case %d:\n%d\n%d\n",c++,solve(),findw());
  }

  return 0;
}

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

#define INF (1<<21)
#define swap(a,b) {a=a+b; b=a-b; a=a-b;}
#define abs(a) (a > 0 ? a:-a)
#define min(a,b) {a > b ? b:a}
#define max(a,b) {a > b ? a:b}
using namespace std;

//O(W*N)??§??????
#define MAX_W 1000
#define MAX_N 1000
#define MAX_VALUE 10000
#define MAX_WEIGHT 1000

int dp[MAX_W+1];
vector<pair<int, int> > Item;
void q42() {
	int W, N;
	int cnt = 1;
	for (; cin >> W;) {
		if (!W) break;

		cin >> N;
		for (int i = 0; i < N; i++) {
			int value, weight;
			char tmp;
			cin >> value >> tmp >> weight;
			Item.push_back(make_pair(value, weight));
		}
		for (int i = 0; i <= W; i++) {
			dp[i] = -1;
		}
		dp[0] = 0;

		for (int i = 0; i < N; i++) {
			int value = Item[i].first;
			int weight = Item[i].second;
			for (int j = W; j >= 0; j--) {
				if (j - weight < 0) continue;
				if (dp[j - weight] == -1) continue;
				dp[j] = max(dp[j], dp[j - weight] + value);
			}
		}

		int ans_value = 0, ans_weight = 0;
		for (int i = 0; i <= W; i++) {
			cout << dp[i] << endl;
			if (ans_value < dp[i]) {
				ans_value = dp[i];
				ans_weight = i;
			}
		}

		cout << "Case " << cnt++ << ":" << endl;
		cout << ans_value << endl;
		cout << ans_weight << endl;
	}
}

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

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

using namespace std;

int main(){
	int w, n, p = 1;
	while(cin >> w, w){
		cin >> n;
		vector<pair<int, int> > t(n);
		for(int i=0; i< n; i++) scanf("%d,%d", &t[i].first, &t[i].second);
		long long dp[1010][1010];
		for(int i=0; i < 1010; i++) for(int j=0; j< 1010; j++) dp[i][j] = 0;
		for(int i=n-1; 0 <= i; i--){
			for(int j=0; j <= w; j++){
				if(j < t[i].second) dp[i][j] = dp[i+1][j];
				else dp[i][j] = max(dp[i+1][j], dp[i+1][j-t[i].second] + t[i].first);
			}
		}
		cout << "Case " << p++ << ":" << endl;
		cout << dp[0][w] << endl;
		for(int i = w; ; i--){
			if(dp[0][i] != dp[0][w]){cout << i+1 << endl; break;}
		}
	}
}