Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

const MaxW = 1000
const MaxN = 1000

type Tuple struct {
	Value  int
	Weight int
}

func nextLine(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func solve(w int, n int, vws []Tuple) Tuple {
	var dp [MaxN + 1][MaxW + 1]int

	for i := 0; i < n; i++ {
		for j:= w; j > 0; j-- {
			if j < vws[i].Weight {
				dp[i + 1][j] = dp[i][j]
			} else {
				dp[i + 1][j] = int(math.Max(float64(dp[i][j]), float64(dp[i][j - vws[i].Weight] + vws[i].Value)))
			}
		}
	}

	maxValue := dp[n][w]
	minWeight := w

	for j := w - 1; j > 0; j-- {
		if maxValue == dp[n][j] {
			minWeight = j
		} else {
			break
		}
	}

	return Tuple{Value: maxValue, Weight: minWeight}
}

func main() {
	var sc = bufio.NewScanner(os.Stdin)
	index := 1
	for {
		var w, n int
		w, _ = strconv.Atoi(nextLine(sc))
		if w == 0 {
			return
		}
		n, _ = strconv.Atoi(nextLine(sc))

		vws := make([]Tuple, n, n)
		for i := 0; i < n; i++ {
			array := strings.Split(nextLine(sc), ",")
			v, _ := strconv.Atoi(array[0])
			w, _ := strconv.Atoi(array[1])
			vws[i] = Tuple{Value: v, Weight: w}
		}

		t := solve(w, n, vws)
		fmt.Printf("Case %d:\n", index)
		fmt.Println(t.Value)
		fmt.Println(t.Weight)
		index++
	}
}

C++: #include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <map>
#define INF 100000000
#define rep(i,a) for(int i=0;i<(a);i++)

using namespace std;
typedef long long ll;

int dp[2000][20000];    //dp[i][j]  i番目以前の物で、重さがj以下の時の価値
int W[2000],V[2000];

int w,n;

void solve(){
    for(int i=n-1;i>=0;i--){
        for(int j=0;j<=w;j++){
            if(j-W[i]<0){
                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<<dp[0][w]<<endl;
    for(int i=w;i>=1;i--){
        if(dp[0][i]!=dp[0][i-1]){
            cout<<i<<endl;
            break;
        }
    }
}

int main(){
    int count=1;
    while(1){
        memset(dp,0,sizeof(dp));
    
        cin>>w>>n;
        if(w==0)break;
        char a;
        rep(i,n)cin>>V[i]>>a>>W[i];
    
        cout<<"Case "<<count<<":"<<endl;
        count++;
        solve();
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#define rep(i, a, n) for(int i = a;i < n;i++)
using namespace std;


int W, N;
int tv[1010];
int tw[1010];
int dp[1010][1010];


void input(){
	string s;
	stringstream ss;
	cin >> N;
	rep(i, 1, N+1){
		cin >> s;
		replace(s.begin(), s.end(), ',', ' ');
		ss.clear();
		ss << s;
		ss >> tv[i] >> tw[i];
	}
}


void makeDP(){
	rep(i, 0, N){
		rep(j, 0, W+1){
			if(tw[i+1] > j) dp[i+1][j] = dp[i][j];
			else dp[i+1][j] = max(dp[i][j-tw[i+1]] + tv[i+1], dp[i][j]);
		}
	}
}

int main(){
	int d = 0;
	while(d++, cin >> W, W){
		input();
		makeDP();
		auto v = max_element(&dp[N][0], &dp[N][W+1]);
		auto w = v - &dp[N][0];

		cout << "Case " << d << ':' << endl;
		cout << *v << endl;
		cout << w << endl;
	}

	return 0;
}

C++: #include<iostream>
using namespace std;
main(){
  int b,c,N,K[1010],P[1010],i,m,j,W,dp[1010],h=1,y,g;
  char d;
  while(1){
    b=0;
    cin >> W;
    for(i=0;i<=W;i++){
      dp[i]=0;
    }
    if(W==0){
      break;
    }
    cin >> N;
    for(i=0;i<N;i++){
      cin >> P[i] >> d >> K[i];
      c=K[i];
      dp[K[i]]=P[i];
      for(j=W;j>=1;j--){
	if(j!=c){
	  if(dp[j]!=0){
	    if(dp[c+j]!=0){
	      if(dp[c+j]<dp[c]+dp[j]){
		dp[c+j]=dp[c]+dp[j];
	      }
	      else{
		continue;
	      }
	    }
	    if(dp[c+j]==0){
	      dp[c+j]=dp[c]+dp[j];
	    }
	  }
	}
      }
    }
    for(i=1;i<=W;i++){
      if(dp[i]>b){
	y=i;
	b=dp[i];
      }
    }
    cout << "Case:" << " " << h << endl;
    cout << b << endl << y << endl;
    h++;
  }
}

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

using namespace std;

void thief(int num,int wei,int total);

int value[1000],weight[1000];
int W,N;
int maxvalue,maxweight;

int main(){
	int ans;
	int Case =1;
	while(cin >> W,W!=0){
		cin>>N;
		maxvalue=0;
		maxweight=1000;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&value[i],&weight[i]);
		}
		thief(0,0,0);
		cout<<"Case "<<Case<<':'<<endl;
		cout<<maxvalue<<endl;
		cout<<maxweight<<endl;
		Case++;
	}
	return 0;
}

void thief(int num,int wei,int total)
{
	if(num==N || wei>W){
		if(maxvalue<=total){
			if(maxvalue == total){
				if(maxweight>wei)
					maxweight=wei;
			}
			else maxweight=wei;
			maxvalue=total;
		}
		return;
	}
	if(wei+weight[num]<W) thief(num+1,wei+weight[num],total+value[num]);
	thief(num+1,wei,total);
	return;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>
#include<set>
 
using namespace std;
 
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define INF 1<<25
 
typedef long long ll;

int dy[8]={1,1,1,0,0,-1,-1,-1};
int dx[8]={-1,0,1,-1,1,-1,0,1};
struct S{
	int a,b,c;
};
bool asc(const S& left,const S& right){
	return left.c > right.c;
}

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

int main(){
	int cnt=1;
	char tmp;
	while(cin>>W){
		if(W==0)break;
		cin>>N;
		rep(i,N)cin>>v[i]>>tmp>>w[i];
		REP(i,1,N+1){
			rep(j,W+1){
				if(w[i-1]<=j)
					dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i-1]]+v[i-1]);
				else 
					dp[i][j]=dp[i-1][j];
			}
		}
		int ans_v=0,ans_w=0;
		rep(i,W+1){
			if(dp[N][i]>ans_v){
				ans_v=dp[N][i];
				ans_w=i;
			}
		}
		cout<<"Case "<<cnt<<":"<<endl;
		cout<<ans_v<<endl;
		cout<<ans_w<<endl;
		cnt++;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int main(void){
  int W,N,v[1001],w[1001];
  int cas = 1;
  while(cin>>W){
    if(W==0) break;
    int dp[1001]={0};
    printf("Case %d:\n",cas++);
    cin>>N;
    REP(i,N){
      scanf("%d,%d",&v[i],&w[i]);
    }
   for(int i=0; i<N; i++){
     for(int j=W;j-w[i]>=0;j--){
      dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
     }
   }
  printf("%d\n",dp[W]);
  for(int i=0; i<=W; i++){
    if(dp[i]==dp[W]){
      printf("%d\n",i);
      break;
    }
  }

  }
  return 0;
}

C++: #include<cstdio>

int main(){
	int w,n,count=1;
	scanf("%d",&w);
	while(w){
		scanf("%d", &n);
		int a[n][2];
		for(int i = 0; i < n; i++){
			scanf("%d,%d", &a[i][0], &a[i][1]);
		}
		unsigned long long c = 1 << n,b;
		int max = 0,weight, weightm = 0,sum;
		while(c){
			sum = 0;
			weight = 0;
			b = 1;
			for(int i = 0; i < n; i++){
				if(c & b){
					sum += a[i][0];
					weight += a[i][1];
				}
				if(weight > w){
					break;
				}
				if(sum > max){
					max = sum;
					weightm = weight;
				}
				if(sum == max && weight < weightm){
					weightm = weight;
					printf("%d\n", weight);
				}
				b = b << 1;
			}
			c--;
		}
		printf("Case %d:\n%d\n%d\n", count++, max, weightm);
		scanf("%d", &w);
	}

}