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 <algorithm>
#include <string>
#define N1 1001
using namespace std;
int Change(string);
void DP();
int N,W,n,v[N1],w[N1];
int main(){
  int t,i,j;
  string in;
  n=1;
  while(1){
    cin >> W;
    if(W==0) break;
    cin >> N;
    for(i=1;i<=N;i++){
      cin >> in;
      for(j=0;j<in.size();j++){
	if(in[j]==','){
	  t=j;
	  break;
	}
      }
      v[i]=Change(in.substr(0,t));
      w[i]=Change(in.substr(t+1,in.size()-t-1));
    }
    DP();
  }
  return 0;
}
int Change(string str){
  int len=str.size(),ret=0,x=1,i=0;
  while(len){
    ret=ret*x+(str[i]-'0');
    i++;
    x=10;
    len--;
  }
  return ret;
}
void DP(){
  int dp[N1][N1],c[N1][N1],maxv,minw,x,y,t,sum,i,j,k;
  for(i=0;i<=W;i++) dp[0][i]=0;
  for(i=0;i<=N;i++) dp[i][0]=0;
  maxv=0;
  for(i=1;i<=N;i++){
    for(j=1;j<=W;j++){
      if(j>=w[i]){
	if(dp[i-1][j]<dp[i-1][j-w[i]]+v[i]){
	  dp[i][j]=dp[i-1][j-w[i]]+v[i];
	  c[i][j]=1;
	}
	else{
	  dp[i][j]=dp[i-1][j];
	  c[i][j]=0;
	}
      }else{
	dp[i][j]=dp[i-1][j];
	c[i][j]=0;
      }
      maxv=max(maxv,dp[i][j]);
    }
  }
  minw=2000000000;
  for(i=1;i<=N;i++){
    for(j=1;j<=W;j++){
      if(maxv==dp[i][j]){
	t=j;
	sum=0;
	for(k=i;k>=1;k--){
	  if(c[k][t]==1){
	    sum+=w[k];
	    t-=w[k];
	  }
	}
	minw=min(minw,sum);
      }
    }
  }
  cout << "Case " << n << ':' << endl;
  cout << maxv << endl << minw << endl;
  n++;
}

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

using namespace std;

typedef pair<int, int> obj;

int main(){
  int N, W, weight, value;
  char ch;
  int case_num = 1;

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

    //printf("W = %d N = %d\n", W, N);

    vector<obj> obj_list;
    int dp[2][1010];
    memset(dp, -1, sizeof(dp));
    
    for(int i = 0; i < N; i++){
      cin >> value >> ch >> weight;
      obj_list.push_back(obj(value, weight));
      //printf("value = %d weight = %d\n", value, weight);
    }

    dp[0][0] = 0;
    dp[1][0] = 0;

    for(int i = 0; i < N; i++){
      obj o = obj_list[i];

      for(int j = 0; j <= W; j++){
        if(dp[(i+1)%2][j] != -1 && j + o.second <= W){
          dp[i%2][j+o.second] = max(dp[i%2][j+o.second], dp[(i+1)%2][j] + o.first);
        }
      }
    }
    int answer = 0;
    int answer_weight = 0;

    for(int i = 0; i <= W; i++){
      if(answer < dp[(N+1)%2][i]){
        answer = dp[(N+1)%2][i];
        answer_weight = i;
      }
    }

    cout << "Case " << case_num << ":" << endl;
    cout << answer << endl;
    cout << answer_weight << endl;

    case_num++;
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>
#include<tuple>
#include<stack>


using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tpl;
int dx[4] = { 1,0,0,-1 };
int dy[4] = { 0,1,-1,0 };

#define ARRAY_MAX 1005

const int INF = 1 << 30;
const int MOD = 1e9 + 7;

int dp[ARRAY_MAX];//dp[i]:=重さがiとなる場合の最大価値

int main() {

	int W;
	int cnt = 1;
	while (cin >> W, W)
	{
		int n;
		char c;
		cin >> n;
		vector<int> v(n), w(n);
		for (int i = 0; i < n; i++)
		{
			cin >> v[i] >> c >> w[i];
		}

		for (int i = 0; i < ARRAY_MAX; i++)
		{
			dp[i] = 0;
		}

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

		for (int i = 0; i <= W; i++)
		{

			if (ans_v < dp[i])
			{
				ans_v = dp[i];
				ans_w = i;
			}
		}
		cout << "Case " << cnt << ":" << endl;
		cout << ans_v << endl << ans_w << endl;
		cnt++;
		v.shrink_to_fit();
		w.shrink_to_fit();
	}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <random>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)

int caseId = 1;

void solve42(int W) {
    cout << "Case " << caseId << ":" << endl;
    int N;
    cin >> W >> N;
    int dp[1001] = {0}; // dp[i]: maxvalue under weight i
    REP(i,N) {
        int v, w;
        scanf("%d,%d", &v, &w);
        for(int j = W; j >= w; j--) {
            dp[j] = max(dp[j], dp[j-w] + v);
        }
    }
    int value = 0, weight = 0;
    REP(i,W+1) {
        if (value < dp[i]) {
            value = dp[i];
            weight = i;
        }
    }
    cout << value << endl << weight << endl;
}

int main() {
    int W;
    while (cin >> W, W) {
        solve42(W);
        caseId++;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    cin>>n;
    int in[1001][1001]={0};
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      rep(j,m+1){
	if(j>=b)
	  in[i][j]=max(in[i-1][j],in[i-1][j-b]+a);
	//in[i][j]=max(in[i][j],in[i-1][j]);
      }
    }
    int max1=0;
    rep(i,m+1)max1=max(max1,in[n][i]);
    rep(i,m+1)
      if(in[n][i]==max1){
	cout<<"Case "<<co<<":"<<endl;
	cout<<max1<<endl<<i<<endl;
	break;
      }
  }
}

/*int main(){
  int m;
  int co=0;
  while(cin>>m,m){
    co++;
    int n;
    char d;
    cin>>n;
    vector<int>w(n),v(n);
    vector<vector<int> >dp(n+1);
    rep(i,m+1)dp[0].pb(0);
    rep(q,n){
      int i=q+1;
      int a,b;char d;
      cin>>a>>d>>b;
      dp[i].pb(0);
      

}
*/

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

#define MAX_N 1000
#define MAX_W 1000

int main(void) {
  int W, N, cnt = 1;
  while (cin >> W, W) {
    cin >> N; cin.ignore();

    int dp[1001][1001] = {0};
    for (int i = 0; i < N; i++) {
      int v, w;
      scanf("%d,%d", &v, &w);
      for (int j = 0; j <= W; j++) {
        dp[i+1][j] = max(dp[i][j], (j - w >= 0 ? dp[i][j-w] + v : 0));
      }
    }
    cout << "Case " << cnt++ << ":" << endl;
    for (int j = 0; j < W + 1; j++) {
      if (dp[N][j] == dp[N][W]) {
        cout << dp[N][j] << endl << j << endl;
        break;
      }
    }
  }
  return 0;
}