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>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
// #define first first
// #define second se
using namespace std;
typedef pair<int, int> P;
typedef vector<int> vi;

int dp[1100][1100];

signed main(){
    int W;
    int cnt = 0;
    while(cin >> W, W){
        int n;
        cin >> n;
        cnt++;
        vector<int> v(n), w(n);
        rep(i, 0, n){
            char c;
            cin >> v[i] >> c >> w[i];
        }
        rep(i, 0, 1010){
            rep(j, 0, 1010){
                dp[i][j] = 0;
            }
        }
        dp[0][0] = 0;
        rep(i, 0, n){
            rep(j, 0, W + 1){
                dp[i + 1][j] = dp[i][j];
                if(j - w[i] >= 0){
                    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
                }
            }
        }
        int ans = -1, wt;
        rep(i, 0, W + 1){
            if(dp[n][i] > ans){
                ans = dp[n][i];
                wt = i;
            }
        }
        cout << "Case " << cnt << ":" << endl;
        o(ans);
        o(wt);
    }
}

C++: #include<iostream>
#include<algorithm>
#include<map>
using namespace std;
typedef pair< int , int > P;

int W, N, value[1000], weight[1000];
P dp[1001][1001];

P rec(int i,int j){ //i番目以降の品物からj以下となるように選ぶ
  if(dp[i][j] != P(0,0)) return dp[i][j];
  P res;
  if(i == N) res = P(0,j);
  else if(j < weight[i]) return res = rec(i+1,j);
  else{
    P tmp = rec(i+1,j-weight[i]);
    res = max(rec(i+1,j),P(tmp.first+value[i],tmp.second));
  }
  return dp[i][j] = res;
}

int main(){
  int cnt = 0; char op;
  while(cin >> W , W){
    cin >> N;
    for(int i = 0 ; i < N ; i++ ) cin >> value[i] >> op >> weight[i];
    P p = rec(0,W);
    cout << "Case " << ++cnt << ":" << endl;
    cout << p.first << endl;
    cout << W - p.second << endl;
  }
}

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

#define INF 1000000000

int W,N;
int v[1000],w[1000];

// dp[i][j] : iテァツ閉ェテァツ崢ョテ、ツサツ・テゥツ卍催」ツ?ョテ」ツつづ」ツ?ョテ」ツ?凝」ツつ嘉ゥツ?催」ツ?陛ッツスツ甘、ツサツ・テ、ツクツ凝」ツ?ォテ」ツ?ェテ」ツつ凝」ツつ暗」ツ??」ツ?ォテゥツ?クテ」ツつ禿」ツ??ヲツ卍づ」ツ?ョテヲツ慊?・ツ、ツァテ・ツ?、
int dp[1000][1000];

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

int main(){

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

    cin >> N;
    memset(dp,-1,sizeof(dp));
    for(int i = 0; i < N; i++){
      v[i] = -1;
      w[i] = INF;
    }
    for(int i = 0 ; i < N ; i++){
      char tmp;
      cin >> v[i] >> tmp >> w[i];
      //  cout << v[i] <<" "<< w[i] << endl; 
    }
    
    for(int i = 0; i < N ; i++){
      for(int j = 0; j <= W ; j++){
	dp[i][j] = rec(i,j);
	//	cout << dp[i][j] << " ";
      }
      //cout << endl;
    }
    
    cout <<"Case " << x << endl;
    cout <<  dp[0][W] << endl;
    int t;
    for( t = 0; dp[0][W] != dp[0][t] ;t++){}
    cout << t << endl; 
    x++;

  }


  return 0;

}

C++: #include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>

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


#define MAX_W 1000
#define MAX_N 1000
#define MAX_VALUE 10000
#define MAX_WEIGHT 1000

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

		cin >> N;
		vector<pair<int, int> > Item;
		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++) {
			if (ans_value < dp[i]) {
				ans_value = dp[i];
				ans_weight = i;
			}
		}

		cout << "Case " << cnt++ << ":" <<"\n";
		cout << ans_value << "\n";
		cout << ans_weight << "\n";
	}
}

int main() {
	q42();
	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]=-1;
			}
		}
		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][cost[0]]=value[0];
		dp[0][0]=0;
		for(int i=1; i<N; ++i){
			for(int j=0; j<=W; ++j){
				if(j-cost[i]>=0){
					dp[i][j]=max(dp[i-1][j],dp[i-1][j-cost[i]]+value[i]);
				} else {
					dp[i][j]=dp[i-1][j];
				}
			}
		}
		int ans_v=-1;
		int ans_c;
		for(int i=0; i<=W; ++i){
			if(dp[N-1][i]>ans_v){
				ans_v=dp[N-1][i];
				ans_c=i;
			}
		}
		cout << "Case " << cnt << ":\n" << ans_v << "\n" << ans_c << "\n";
		++cnt;
	}
	return 0;
}