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 <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main() {
	int W, N;
	char d;
	int counter = 1;
	int takara[1000][2];
	while(1) {
		cin >> W;
		if (W == 0) {
			break;
		}
		cin >> N;
		for (int i = 0; i < N; i++) {
			cin >> takara[i][0] >> d >> takara[i][1];
		}
		vector< vector<int> > dp(W, vector<int> (N+1));
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[j][i] = 0;
			}
		}
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				if (i == 0) {
					continue;
				}
				if (j < takara[i-1][1]) {
					dp[j][i] = dp[j][i-1];
				} else {
					dp[j][i] = max(dp[j][i-1], dp[j-takara[i-1][1]][i-1] + takara[i-1][0]);
				}
			}
		}
		int MAX[2];
		MAX[0] = 0;
		MAX[1] = 10000;
		for (int i = 0; i <= W; i++) {
			if (MAX[0] == dp[i][N]) {
				MAX[1] = min(MAX[1],i);
			} else if (MAX[0] < dp[i][N]){
				MAX[0] = dp[i][N];
				MAX[1] = i;
			}
		}
		cout << "Case " << counter << ':' << endl << MAX[0] << endl << MAX[1] << endl;
		counter++;
	}
	return 0;
}

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

using namespace std;

int main()
{
    for( int iii=1; ; ++iii ) {
        int w, n;
        cin >> w;
        if ( w == 0 )
            break;
        cin >> n;

        struct { int v, w; } item[1000] = {{0, 0}};
        for( int i=0; i<n; ++i ) {
            scanf( "%d,%d", &item[i+1].v, &item[i+1].w );
        }

        int dp[1002][1002] = {};

        int max_w = 0, max_v = 0;
        for( int ni=1; ni<n+1; ++ni ) {
            for( int wi=1; wi<w+1; ++wi ) {
                int const bef_ni = ni - 1;
                int const bef_wi = wi - item[ni].w;

                dp[ni][wi] = [&]() -> int {
                    if ( bef_wi >= 0 ) {
                        return max( dp[bef_ni][wi], dp[bef_ni][bef_wi] + item[ni].v );
                    } else {
                        return dp[bef_ni][wi];
                    }
                }();

                if ( dp[ni][wi] > max_v || ( dp[ni][wi] == max_v && wi < max_w ) ) {
                    max_v = dp[ni][wi];
                    max_w = wi;
                }
            }
        }

        cout << "Case " << iii << ":" << endl
             << max_v << endl
             << max_w << endl;
    }
}

C++: //-AOJ0042 -----------------------------------------------------------//
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <string>

#define rep(x,to) for(int x=0;x<to;x++)
#define rep2(x,from,to) for(int x=from;x<to;x++)

struct stk{
	int kt,wg,ct; 
	stk(int kt,int wg,int ct) : kt(kt), wg(wg), ct(ct) { }
};
using namespace std;


int main(void){

int w,n,cnt=0;
char k;
vector<vector<int> > dp;
vector<int> tk;
vector<int> tw;
	
	while(!cin.eof()){
		cin >> w ;
		if(cin.eof() || w==0) break;
		cin.ignore();
		cin >> n;
		dp.resize(n+1,vector<int>(w+1,0));
		tk.resize(n,0);
		tw.resize(n,0);
		cnt++;
		rep(i,n) cin >> tk[i] >> k >> tw[i];

		rep(i,n) rep(j,w+1){
			if(tw[i]>j){
				dp[i+1][j]=dp[i][j];
			}else{
				dp[i+1][j]=max(dp[i][j],dp[i][j-tw[i]]+tk[i]);
			}
		}	
//		rep(i,n+1) {rep2(j,1,w+1){
//			cout << dp[i][j] << "\t";
//		} cout <<endl;}

		while(dp[n][w]==dp[n][w-1]){
			w--;
		}

		printf("Case %d:\n%d\n%d\n",cnt,dp[n][w],w);
	

	}
	
	return 0;
}

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

struct treasure{
	int value;
	int weight;
};

bool comp(treasure t1, treasure t2){
	return (t1.weight < t2.weight);
}

int main(){
	int count = 1;
	int w;
	int n;
	char c;
	const int max = 1001;
	int cost[max];
	int best[max];
	while(cin >> w){
		if(w == 0) break;
		for(int i = 0; i < max; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		vector<treasure> vt;
		cin >> n;
		for(int i = 0; i < n; i++){
			treasure t;
			cin >> t.value >> c >> t.weight;
			vt.push_back(t);
		}
		treasure t;
		t.value = -1;
		t.weight = -1;
		vt.push_back(t);
		sort(vt.begin(), vt.end(), comp);
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= w; j++){
				if(vt[i].weight <= j ){
					if(cost[j] < cost[j-vt[i].weight] + vt[i].value){
						if(best[j-vt[i].weight] != i){
							cost[j] = cost[j-vt[i].weight] + vt[i].value;
							best[j] = i;
						}else{
							cost[j] = cost[j-vt[i].weight];
							best[j] = best[j-vt[i].weight]; 
						}
					}
				}
			}
		}
		int i = w;
		while(cost[i] == cost[i-1] && i > 1) i--;
		cout << "Case " << count++ << ":" << endl;
		cout << cost[w-1] << endl;
		cout << i << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define latte WORLD

struct Data{
    int v,w;
    Data(){}
    Data(int _v,int _w){
        v=_v; w=_w;
    }
    bool operator <(const Data &a)const{
        if(v==a.v) return w>a.w;
        return v<a.v;
    }
    Data operator +(const Data &a)const{
        return Data(v+a.v,w+a.w);
    }
};

int N,W;
Data t[1000],memo[1000][1001];

Data latte(int n,Data st){
    if(st.w>=W) return Data(-1,-1);
    if(n==N) return st;
    if(memo[n][st.w].v!=-1) return memo[n][st.w];
    memo[n][st.w] = max(latte(n+1,st+t[n]),latte(n+1,st));
    return memo[n][st.w];
}

int main(){
    for(int ds=1;cin>>W,W;ds++){
        cin>>N;
        for(int i=0;i<N;i++) scanf("%d,%d",&t[i].v,&t[i].w);
        fill_n((Data*)memo,1000*1001,Data(-1,-1));
        Data ans = latte(0,Data(0,0));
        printf("Case %d:\n%d\n%d\n",ds,ans.v,ans.w);
    }
}

C++: #include<iostream>

using namespace std;

const int MAX_W = 1001;
const int MAX_T = 1001;

int dp[MAX_T][MAX_W];

void init(){
  for(int i = 0; i < MAX_T; i++){
    fill(dp[i],dp[i]+MAX_W,0);
  }
}

int main(){
  int rev = 0, max;
  while(cin >> max && max){
    rev++;
    init();
    int num;
    cin >> num;
    for(int i = 1; i <= num; i++){
      char trash;
      int price, weight;
      cin >> price >> trash >> weight;
      for(int j = 0; j <= max; j++){
	if(weight <= j){
	  dp[i][j] = price + dp[i-1][j-weight];
	}
	if(dp[i][j] < dp[i-1][j]) dp[i][j] = dp[i-1][j];
      }
    }
    int ablep = 0, ablew = 0;
    for(int i = 0; i <= num; i++){
      for(int j = 0; j <= max; j++){
	if(ablep < dp[i][j]){
	  ablep = dp[i][j];
	  ablew = j;
	}else if(ablep == dp[i][j]){
	  if(ablew > j) ablew = j;
	}
      }
    }
    cout << "Case " << rev << ":" << endl;
    cout << ablep << endl << ablew << endl;

  }
  return 0;
}