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 <algorithm>
#include <vector>
#include <map>
#include <cmath>

#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)
#define repr(a, b, c) for (int a = b; a > (c); a--)
#define repre(a, b, c) for (int a = b; a >= (c); a--)

using namespace std;

int main() {
    for (int c = 1; ; c++) {
        int W, N;
        cin >> W;
        if (W == 0) break;
        cin >> N;
        vector< vector<int> > dp(N + 1, vector<int>(W + 1, 0));
        int v, w;
        char ch;
        rep(i, 0, N) {
            cin >> v >> ch >> w;
            repe(j, 0, W) {
                if (j >= w)
                    dp[i + 1][j] = max(dp[i][j], dp[i][j - w] + v);
                else
                    dp[i + 1][j] = dp[i][j];
            }
        }
        int ans_w = 0, ans_v = 0;
        repre(i, W, 0) {
            if (ans_v <= dp[N][i]) {
                ans_w = i;
                ans_v = dp[N][i];
            }
        }
        cout << "Case " << c << ":" << endl;
        cout << ans_v << endl;
        cout << ans_w << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <cstdlib>
#include <stdio.h>
#define INF 100000000
#define INFLL (long long)1e14
#define Loops(i, n) for(int i = 0; i < (int)n; i++)
#define Loops1(i, n) for(int i = 1; i <= (int)n; i++)
int dp[2000][2000];
int w[2000];
int v[2000];
int N;
int W;
int count=1;
int main(void){
    while(1){
        std::cin>>W;
        if(W==0){
            break;
        }
        std::cin>>N;
        Loops(i,N){
            int vi,wi;
            scanf("%d,%d",&vi,&wi);
            v[i]=vi;
            w[i]=wi;
//          std::cin>>w[i]>>v[i];
        }
        Loops(i,1000){
            Loops(j,1000){
                dp[i][j]=0;
            }
        }
        Loops(i,N){
            Loops(j,W+1){
                if(j-w[i]>=0){
                    dp[i+1][j]=std::max(dp[i][j],dp[i][j-w[i]]+v[i]);
                }else {
                    dp[i+1][j]=dp[i][j];                
                }
            }
        }
        int kati=0,omosa=0;
        Loops(i,W+1){
            if(kati<dp[N][i]){
                kati=dp[N][i];
                omosa=i;
            }
        }
        printf("Case %d:\n",count);
        std::cout<<kati<<std::endl;
        std::cout<<omosa<<std::endl;
        count++;
/*  Loops(i,N+1){
        Loops(j,W+1){
            std::cout<<dp[i][j]<<" ";
        }
        std::cout<<std::endl;
    }*/
 
    }
    return 0;
}

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

using namespace std;

#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))

int W, n;
int v[1002], w[1002];
int dp[1002][1002];

int rec(int i, int j) {
    if(dp[i][j] >= 0) return dp[i][j];

    int res = 0;
    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() {
    for(int data = 1; ; data++) {
        ZERO(v); ZERO(w); MINUS(dp);
        scanf("%d", &W);
        if(W == 0) break;
        scanf("%d", &n);
        for(int i=0; i<n; i++) scanf("%d,%d", v+i, w+i);

        int ans = rec(0, W), wret = 0;
        while(dp[0][wret] != ans) wret++;
        printf("Case %d:\n", data);
        printf("%d\n", ans);
        printf("%d\n", max(0,wret-1));
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

int main() {
	int W, N;

	for (int h = 1; cin >> W&& W; h++) {
		cin >> N;
		int v, w;
		int dp[1001] = {};
		for (int i = 0; i < N; i++) {
			char c;
			cin >> v >> c >> w;
			for (int j = W; j > 0; j--) {
				if (j - w < 0)continue;
				dp[j] = max(dp[j], dp[j - w] + v);
			}
		}
		int a = 0, b = 0;
		for (int i = 0; i <= W; i++) {
			if (a < dp[i]) {
				a = dp[i];
				b = i;
			}
		}
		cout << "Case " << h << ":" << endl;
		cout << a << endl;
		cout << b << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <map>

using namespace std;

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

int maxm;
int supw;
int vs[SUPmaxm];
int wos[SUPmaxm];
int dp[SUPmaxm][SUPsupw]; //m ika no item tukatte, w ika no omomi de, sum v no max
int rev,rew;

void solve(){
	FOR(m,0,maxm+1) FOR(w,0,supw) dp[m][w]=0;
	FOR(m,1,maxm+1){
		FOR(w,0,wos[m]) dp[m][w] = dp[m-1][w];
		FOR(w,wos[m],supw) dp[m][w] = max(dp[m-1][w],dp[m-1][w-wos[m]]+vs[m]);
	}
}

void findRe(){
	rew = supw-1;
	rev = dp[maxm][rew];
	while(true){
		if(dp[maxm][--rew]!=rev){++rew;break;}
	}
}



int main(){
	for(int casei=1;true;++casei){
		cin >> supw;			if(!supw) return 0;
		cin >> maxm;
		FOR(m,1,maxm+1) scanf("%d,%d",vs+m,wos+m);
		solve();
		findRe();
		cout << "Case " << casei << ":" << endl;
		cout<<rev<<endl;
		cout<<rew<<endl;
	}
}

C++: //A Thief
#include<bits/stdc++.h>
using namespace std;

int dp[1001][1001];

int main(){
  int W, n=0;
  while(true){
    n++;
    cin>>W;
    if(W==0)break;
    for(int i=0; i<1001; i++)
      for(int j=0; j<1001; j++)
        dp[i][j]=0;
    int N;
    int v[1001], w[1001];
    cin>>N;
    for(int i=0; i<N; i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    for(int i=0; i<N; i++){
      for(int j=0; j<=W; j++){
        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]);
        }
      }
    }
    cout<<"Case "<<n<<":"<<endl;
    cout<<dp[N][W]<<endl;
    int i;
    for(i=W; i>0; i--){
      if(dp[N][i]==dp[N][i-1])continue;
      else break;
    }
    cout<<i<<endl;
  }
  return 0;
}