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 <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]>kg) dp[j][h]=dp[1-j][h];
			  else dp[j][h]=(dp[1-j][h+w[i]]+k[i])>dp[1-j][h] ? dp[1-j][h+w[i]]+k[i] : dp[1-j][h];
	  }
  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 <stdio.h>
#include <algorithm>
using namespace std;
int W, N, v[1000], w[1000], dp[1001][1001], c, r;
int main() {
	while (~scanf("%d", &W)) {
		if (!W) break;
		scanf("%d", &N);
		for (int i = 0; i < N; i++) scanf("%d,%d", &v[i], &w[i]);
		for (int i = 1; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = dp[i - 1][j];
				if (j >= w[i - 1]) {
					dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
				}
			}
		}
		printf("Case %d:\n", ++c);
		printf("%d\n", dp[N][W]);
		printf("%d\n", lower_bound(dp[N], dp[N] + W + 1, dp[N][W]) - dp[N]);
	}
	return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
#include <cctype>

#define mp make_pair
#define pb push_back
#define REP(i,a,n) for(int i = a;i < (n);i++)
#define rep(i,n) for(int i = 0;i < (n);i++)
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
#define range(x,min,max) ((min) <= (x) && (x) <= (max))
#define xyrange(x, minX, maxX, y, minY, maxY) (range(x, minX, maxX) && range(y, minY, maxY))

using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef pair<int,int> PII;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};
const int INF = INT_MAX / 2;

int W, N;
VI ps, wes;
int cc = 1;

int main(){
	while (cin >> W, W) {
		ps.clear();
		wes.clear();
		cin >> N;
		ps.resize(N);
		wes.resize(N);
		VVI dp(N + 1, VI (W + 1, -1));
		rep(i, N) rep(j, W) dp[i][j] = 0;
		rep(i, N) {
			char c;
			cin >> ps[i] >> c >> wes[i];
		}
		dp[0][0] = 0;
		rep (i, N) {
			rep (j, W + 1) {
				if (dp[i][j] == -1) continue;
				dp[i + 1][j] = max(dp[i + 1][j] ,dp[i][j]);
				if (j + wes[i] <= W) {
					dp[i + 1][j + wes[i]] = max(dp[i + 1][j + wes[i]], dp[i][j] + ps[i]);
				}
			}
		}
		int ans_p = *max_element(all(dp[N]));
		int ans_w = 0;
		rep (i, W + 1) {
			if (dp[N][i] == ans_p) {
				ans_w = i;
				break;
			}
		}
		cout << "Case " << cc++ << ":" << endl;
		cout << ans_p << endl;
		cout << ans_w << endl;
	}
	return 0;
}

C++: #include<stdio.h>
int a,s,d[1001],f[1001],g[1001][1001],h,z,i,j,x;
int main(void)
{
	x=0;
	scanf("%d",&a);
	while(a!=0){
		x++;
		scanf("%d",&s);
		for(i=0;i<s;i++){
			scanf("%d,%d",&d[i],&f[i]);
		}
		for(i=0;i<=s;i++){
			for(j=0;j<=a;j++){
				g[i][j]=0;
			}
		}
		for(i=0;i<=s;i++){
			for(j=0;j<=a;j++){
				if(g[i+1][j+f[i]]<g[i][j]+d[i]&&j+f[i]<=a){
					g[i+1][j+f[i]]=g[i][j]+d[i];
				}
				if(g[i+1][j]<g[i][j]){
					g[i+1][j]=g[i][j];
				}
			}
		}
	/*	for(i=0;i<=s;i++){
			for(j=0;j<=a;j++){
				printf("%d ",g[i][j]);
			}
			printf("\n");
		}*/
		h=0;
		z=0;
		for(i=0;i<=a;i++){
			if(h<g[s][i]){
				h=g[s][i];
				z=i;
			}
		}
		printf("Case %d:\n",x);
		printf("%d\n%d\n",h,z);
		scanf("%d",&a);
	}

	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <utility>
#define mp make_pair
#define f first
#define s second
#define dm(x) cerr << #x << " = " << x << endl;
using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
typedef pair<long,long> pp;

pp dp[1001][1001];
int p[1001],w[1001];
int W, N;

pp operator+(pp a,pp b)
{
  pp ret(a.f+b.f, a.s+b.s);
  return ret;
}

//i番目以降でj以下の重さで価値を最大化。 {最大の価値, その時の重さ}
pp calc(int i,int j)
{
  pp ret;
  if(dp[i][j].f>=0) return dp[i][j];
  if(i==N) ret=mp(0,0);
  else if(j<w[i]) ret=calc(i+1,j);
  else{
    pp a=calc(i+1,j);
    pp b=calc(i+1,j-w[i]) + mp(p[i],w[i]);
    if(a.f>b.f) ret = a;
    else if(a.f<b.f) ret = b;
    else{
      if(a.s > b.s) ret = b;
      else ret = a;
    }
  } 
  return dp[i][j] = ret;
}

int main()
{
  int n=1;
  while(scanf("%d", &W),W){
    scanf("%d", &N);
    rep(i,N) scanf("%d, %d",p+i, w+i);
    // rep(i,N) printf("%d: p:%d w:%d\n",i , p[i], w[i]);
    rep(i,N+1) rep(j,W+1) {dp[i][j]=mp(-1,-1);}
    pp ret=calc(0,W);
    // printf(","); rep(j,W+1) printf("%d,",j); printf("\n");
    // rep(i,N+1){
    //   printf("%d,",i);
    //   rep(j,W+1)
    //     printf("%d,",calc(i,j).f);
    //   printf("\n");
    // }
    // rep(i,N+1){
    //   printf("%d,",i);
    //   rep(j,W+1)
    //     printf("%d,",calc(i,j).s);
    //   printf("\n");
    // }
    
    printf("Case %d:\n%ld\n%ld\n", n++, ret.f, ret.s);
  }
  return 0;
}

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

using namespace std;

const int NUM_MAX = 1001;
int dp[NUM_MAX][NUM_MAX];
int we[NUM_MAX], v[NUM_MAX];
int w, n;

void solve()
{
    int max_idx;

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

    max_idx = 0;
    for (int j = 0; j <= w; j++)
        if (dp[n][j] == dp[n][w]) {
            max_idx = j;
            break;
        }
        
    cout << dp[n][w] << '\n' << max_idx << endl;
}

int main()
{
    int cas = 1;

    while (cin >> w, w) {
        char ch;

        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> v[i] >> ch >> we[i];

        for (int i = 0; i <= n; i++)
            for (int j = 0; j <= w; j++)
                dp[i][j] = 0;

        cout << "Case " << cas++ << ":\n";
        solve();
    }

    return 0;
}