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 <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

int MAX = 1000*1000;

void solve(int n, int W, vector<int> v, vector<int> w) {
	int **dp = new int*[n];
	for (int i=0; i<n; i++) {
		dp[i] = new int[MAX+2];
		for (int j=0; j<MAX+2; j++)
			dp[i][j] = 0;
	}

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

	int val = -1, weight = 0;
	for (int j=0; j<=W; j++) {
		if (val < dp[n-1][j]) {
			val = dp[n-1][j];
			weight = j;
		}
	}

	cout << val << endl << weight << endl;

	for (int i=0; i<n; i++)
		delete [] dp[i];
	delete [] dp;
}

int main() {
	

	int n, W;
	int ver = 1;

	cin >> W;
	while (W!=0) {
		cin >> n;

		vector<int> v(n), w(n);
		for (int i=0; i<n; i++)
			scanf("%d,%d", &v[i], &w[i]);

		cout << "Case " << ver << ":" << endl;
		solve(n, W, v, w);

		cin >> W;
		ver++;
	}

	return 0;
}

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

using namespace std;

int dp[1000][1001],t[1000],w[1000];

int main(){
  for(int c=1;;c++){
    int n,q,i,j;
    scanf("%d",&q); if(q==0) break;
    scanf("%d",&n);

    for(i=0; i<n; i++)
      scanf("%d,%d",&t[i],&w[i]);

    for(i=0; i<=q; i++)
      dp[0][i] = (i>=w[0])?t[0]:0;

    for(i=1; i<n; i++)
      for(j=0; j<=q; j++)
	if(j>=w[i])
	  dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+t[i]);
	else
	  dp[i][j] = dp[i-1][j];

    int mw=0,mt=0;
    for(j=0; j<=q; j++)
      if(dp[n-1][j]>mt){
	mt = dp[n-1][j];
	mw = j;
      }

    printf("Case %d:\n%d\n%d\n",c,mt,mw);
  }
}

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

int dp[1000],w[1000],v[1000],a,n,t=0;

int main(){

  while(cin >> a, a){

    t++;
    cin >> n;

    fill(dp,dp+a+1,0);

    for(int i=0;i<n;i++) scanf("%d,%d",&v[i],&w[i]);

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

    int mv = 0;
    for(int i=0;i<=a;i++){
      if(dp[i] > dp[mv]) mv = i;
    }

    cout << "Case " << t << ":" << endl;
    cout << dp[mv] << endl << mv << endl;
  }

}

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

int N,W;

int main(){
  int c = 1;
  while(cin >> W , W){
    cin >> N;
    for(int i=0; i<N; i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    memset(dp,0,sizeof(dp));

    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];
      }
    }
    
    int weight=1000000;
    for(int i=0; i<=N; i++){
      for(int j=0; j<=W; j++){
        if(dp[i][j] == dp[N][W]){
          weight = min(weight,j);
        }
      }
    }
  out:;
    printf("Case %d:\n%d\n%d\n", c++, dp[N][W], weight);
  }

  return 0;
}

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;

}