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 <vector>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
//	cut here before submit 
//	freopen("testcase.thief", "r", stdin);
	string str = "";
	int count = 0;

	while (getline (cin, str ) ){
		int max_wight;
		stringstream ssw (str );
		ssw >> max_wight;

		if (max_wight == 0 ){
			break;
		} // end if
		++count;
		
		int n;
		getline (cin, str );
		stringstream ssn(str);
		ssn >> n;

		vector <int> W (n, 0 );
		vector <int> V (n, 0 );
		vector <int> DP (max_wight + 1, 0 );
		int i,j;

		for (i = 0; i < n; ++i){
			getline (cin, str );
			while (str.find (',') != string::npos ){
				str = str.replace ( str.find (','), 1, " " );
			} // end while
			stringstream ss(str);
			ss >> V[i] >> W[i];
		} // end for

		for (i = 0; i < n; ++i){
			vector <int> dp = DP;
			for (j = W[i]; j <= max_wight; ++j){
				int p = j - W[i];
				if (dp[p] + V[i] > DP[j] ){
					DP[j] = dp[p] + V[i];
				} // end if 
			} // end for
		} // end for

		int maxvalue = DP[max_wight];
		int minwight = max_wight;

		for (j = max_wight - 1; j >= 0; --j){
			if (DP[j] >= maxvalue){
				minwight = j;
				maxvalue = DP[j];
			} // end if
		} // end for
		cout << "Case " << count << ':' << endl;
		cout << maxvalue << endl;
		cout << minwight << endl;
			
	} // end while	
	
	return 0;
}

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(){
  int x = 1;
  while(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++){
	rec(i,j);
      }
    }
    
    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++: x=i=0
while(c=gets.to_i)>0
m=(1..gets.to_i).inject([0]*1e3){|s|v,w=gets.split(?,).map &:to_i;(0..c).map{|j|x=[s[j],w>j ?0:s[j-w]+v].max}}
puts "Case #{i+=1}:",x,m.index(x)
end

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  for(int c=1,w; cin>>w,w; c++){
    int n,tr[1000][2] = {0};
    cin >>n;
    for(int i=0; i<n; i++){
      scanf("%d,%d",&tr[i][0],&tr[i][1]);
    }
    int dp[100][100] = {0};
    for(int i=0; i<n; i++){
      for(int j=0; j<=w; j++){
	if(j>=tr[i][1]){dp[j][i] = max(dp[j][i-1],dp[j-tr[i][1]][i-1]+tr[i][0]);}
      }
    }
    int pr = -1, val;
    for(int j=0; j<=w; j++){
      for(int i=0; i<n; i++){
	if(pr<dp[j][i]){pr = dp[j][i];val = j;}
      }
    }
    cout <<"Case "<<c<<":"<<endl;
    cout <<pr<<endl<<val<<endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

int dp[2000][2000];



int main(){
	int W;
	int n;
	int c=0;
	while(cin>>W){
		c++;
		if(W==0)break;
		char ch;
		int w[2000],v[2000];
		rep(i,2000)rep(j,2000)dp[i][j]=-999999999;
		cin>>n;
		rep(i,n){
			cin>>v[i]>>ch>>w[i];
		}

		int maxval=-1;
		int maxwait=-1;

		dp[0][0]=0;
		for(int i=1;i<=n;i++){
			rep(j,W){
				dp[i][j] = max(dp[i][j],dp[i-1][j]);
				if(j-w[i-1]>=0)
					dp[i][j] = max(dp[i][j],dp[i-1][j-w[i-1]]+v[i-1] );

				if(maxval<dp[i][j]){
					maxval=dp[i][j];
					maxwait=j;
				}
				if(maxval==dp[i][j] && maxwait>j){
					maxval=dp[i][j];
					maxwait=j;
				}
			}
		}
		cout<<"Case "<<c<<":"<<endl;
		cout<<maxval<<endl;
		cout<<maxwait<<endl;
	}
}

C++: #include<iostream>
using namespace std;
int main(){
  int dp[1001][1001];
  int n,W;
  int v[1000],w[1000];
  int num=0;
  while(cin>> W,W){
    num++;
    cin>> n;
    for(int i=0;i<n+1;i++){
      dp[0][i]=0;
    }

    for(int i=0;i<n;i++){
      char c;
      cin>> v[i]>> c>> w[i];
    }

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

    int mv=0,mw;
    for(int j=0;j<=W;j++){
      if(mv<dp[n][j]){
	mv=dp[n][j];
	mw=j;
      }
    }
    /*for(int i=0;i<=n;i++){
      for(int j=0;j<=W;j++){
	cout<< dp[i][j];
      }
      cout<< endl<< endl;
    }
    */
    cout<< "Case "<< num<< ":"<< endl;
    cout<< mv<< endl;
    cout<< mw<< endl;
  }
}

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

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

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

void swap(int *a,int *b){
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void babbleSort(int *data,int n){
	for(int i=0;i<n;i++){
		for(int j=n-1;j>i;j--){
			if(data[j]>=data[j-1]){
				swap(&data[j],&data[j-1]);
			}
		}
	}
}


using namespace std;

/*
cin.ignore();
	getline(cin,dataStr);
	data = new int[n];
	stringToInteger(&dataStr,data,n);
*/

void stringToInteger(string *str,int *data,int n){
	for(int i=1;i<=n;i++){
		int spaceN = str->find(' ',0);

		data[i] = atoi(str->substr(0,spaceN).c_str());
		str->erase(0,spaceN+1);
	}
}



int dp[1010];
//dp[i] i:重さ




int main(){


	int w,n;
	int cnt=1;
	while(1){
		cin>>w;
		if(w==0)break;

		cin>>n;

		for(int i=0;i<=w;i++){
			dp[i]=0;
		}
		int value,weight;
		for(int i=0;i<n;i++){
			scanf("%d,%d",&value,&weight);
			for(int j=w;j>=0;j--){
				if((j==0||dp[j]>0)&&((j+weight)<=w)){
					int tmp = dp[j]+value;
					dp[j+weight] = max(tmp,dp[j+weight]);
				//	cout<<j+weight<<" "<<dp[j+weight]<<endl;
				}
			}
		}

		int ans=0;
		int ansCnt=0;
		for(int i=0;i<=w;i++){
			if(ans<dp[i]){
				ans=dp[i];
				ansCnt=i;
			}
		}
		cout<<Case<<" "<<cnt<<":"<<endl;
		cout<<ans<<endl;
		cout<<ansCnt<<endl;
		cnt++;
	}



	return 0;
}