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 <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define Rrep(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

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

int main() {
	int W, N;
	int c = 1;
	while(1) {
		scanf("%d", &W);
		if(W == 0) break;
		memset(dp, 0, sizeof(dp));
		scanf("%d", &N);
		int wans = 0;
		rep(i, N) scanf("%d,%d", &v[i], &w[i]);
		rep(i, N) rep(j, W + 1) {
			if(j < w[i]) dp[i + 1][j] = dp[i][j];
			else {
				dp[i+1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
			}
		}
		Rrep(i, W) if(dp[N][i] != dp[N][W]){
			printf("Case %d:\n", c++);
			printf("%d\n", dp[N][W]);
			printf("%d\n", i + 1);
			break;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

int main(){
    int W;
    int cnt = 0;
    while(cin >> W,W){
        int n;
        cin >> n;

        int v[1005], w[1005];
        rep(i,n){
            char g;
            cin >> v[i] >> g >> w[i];
        }

        int dp[2005] = {0};
        rep(i,W + 1) dp[i] = -1;
        dp[0] = 0;

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

        int ans_v = 0, ans_w = 0;
        rep(i,W + 1){
            if(ans_v < dp[i]){
                ans_v = dp[i];
                ans_w = i;
            }
        }

        cout << "Case " << cnt + 1 << ":" << endl;
        cout << ans_v << endl;
        cout << ans_w << endl;
        cnt++;
    }
}

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

typedef struct{
  int g;
  int ye;
}SET;

int main(){
  
  int n,w;
  int cnt=1;
  while(1){
    
    scanf("%d",&w);
    if(w==0)break;
    scanf("%d",&n);
    SET data[1111]={0}; 
    int memo[1111][1111]={{0}};
    int max_y=0;
    int max_g=1111;
    
    for(int i=0;i<n;i++){
      scanf("%d,%d",&data[i].ye,&data[i].g);
    }
    memo[0][data[0].g]=data[0].ye;
    for(int i=1;i<n;i++){
      if(data[i].ye>memo[i-1][data[i].g]){
	memo[i][data[i].g]=data[i].ye;
      }
      for(int j=1;j<=w;j++){
	if(memo[i-1][j]>0){
	  if(memo[i-1][j]>memo[i][j]){
	    memo[i][j]=memo[i-1][j];
	  }
	  if(j+data[i].g<=w){
	    memo[i][j+data[i].g]=memo[i-1][j]+data[i].ye;
	  }
	}
	if(max_y<=memo[i][j]){
	  if(max_y==memo[i][j]){
	    if(max_g>j){
	      max_g=j;
	    }
	  }
	  else{
	    max_g=j;
	    max_y=memo[i][j];
	  }
	}
      }    
    }
    printf("Case %d:\n%d\n%d\n",cnt,max_y,max_g);
    cnt++;
  }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
#define W_MAX 1000
#define N_MAX 1000

int W, N, dp[N_MAX + 1][W_MAX + 1], w[N_MAX + 1], v[N_MAX + 1];;

int main(){
	int c = 1;
	while (1){
		scanf("%d %d", &W, &N);
		if (W == 0)
			break;
		for (int i = 1; i <= N; ++i){
			char ch = ',';
			scanf("%d %s %d", &v[i], &ch, &w[i]);
		}
		for (int i = 1; i <= W; ++i)
			dp[0][i] = 0;
		for (int i = 1; i <= N; ++i){
			for (int j = 1; j <= W; ++j){
				if (j < w[i])
					dp[i][j] = dp[i - 1][j];
				else{
					dp[i][j] = std::max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
				}
			}
		}
		int minv = 1;
		for (int i = 0; i <= W; ++i){
			if (dp[N][minv] < dp[N][i])
				minv = i;
		}
		printf("%d\n", "Case", c, ":");
		printf("%d\n", dp[N][W]);
		printf("%d\n", minv);
		++c;
	}
	return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
using namespace std;


int main() {
  int dp[2][1001],n,k[1000],w[1000],i,j,h,kg,mk,mw,cn=0;
  int a[1000],b[1000];
  while(true) {
	  cin >> kg;
	  if (kg==0) break;
	  for (i=0;i<=kg;i++) {dp[0][i]=0; dp[1][i]=0;}
	  cin >> n;
	  for (i=0;i<n;i++) scanf("%d,%d",&k[i],&w[i]);
	  for (i=0,j=0;i<n;i++) {
		  j=1-j;
		  for (h=kg;h>0;h--)
			  if (h-w[i]>=0) dp[j][h-w[i]]=(dp[1-j][h]+k[i])>dp[1-j][h-w[i]] ? dp[1-j][h]+k[i] : dp[1-j][h-w[i]];
	  }
  mk=0;
  for (i=kg;i>=0;i--) if (mk<dp[j][i]) {mk=dp[j][i]; mw=i;}
  a[cn]=mk; b[cn]=kg-mw; cn++;
  }
  for (i=0;i<cn;i++) {
	  cout << "Case " << i+1 << ":" << endl;
	  cout << a[i] << endl;
	  cout << b[i] << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

#define FOR(i, n, m) for(int i = (n);i < (m);i++)
#define REP(i, n) for(int i = 0;i < (n);i++)
#define PII pair<int, int>

typedef long long int lli;

using namespace std;

int n, W;
vector<int> v, w;
int dp[1000][1000+1];

int main(void){
    int cs = 1;
    while(cin >> W && W){
        cin >> n;
        REP(m, n){
            int a, b;
            scanf("%d, %d", &a, &b);
            v.push_back(a);
            w.push_back(b);
        }

        FOR(i, 1, n){
            FOR(j, 0, W+1){
                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 << "Case " << cs << ":" << endl;
        cout << dp[n-1][W] << endl;
        int min_w = 100000000;
        REP(j, W+1){
            if(dp[n-1][j] == dp[n-1][W])
                min_w = min(min_w, j);
        }
        cout << min_w << endl;
        cs++;
    }
    return 0;
}

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

using namespace std;

int W,N,c;
int w[1024],p[1024];
int dp[2][10001];

int main(void){
	while(scanf("%d", &W)&&W){
		int ans;
		scanf("%d", &N);
		for(int i = 1; i <= N; i++){
			scanf("%d,%d", &p[i], &w[i]);
		}
		for(int i = 1; i <= N; i++){
			for(int j = 1;j <= W; j++){
				if(j-w[i]<0)continue;
				dp[i%2][j] = max(dp[1-i%2][j],dp[1-i%2][j-w[i]]+p[i]);
			}
		}
		c++;
		printf("Case%2d:\n",c);
		printf("%d\n",dp[N%2][W]);
		for(int i = 0; i < W; i++){
			if(dp[N%2][W] == dp[N%2][W-i]){
				ans = i;
			}
		}
		printf("%d\n",W-ans);
		/*ツ渉可甘コツ可サ*/
		for(int i = 0; i < 1024; i++){
			w[i] = 0;
			p[i] = 0;
		}
		for(int i = 0; i < 2; i++){
			for(int j = 0; j < 10001; j++){
				dp[i][j] = 0;
			}
		}
	}
	return 0;
}