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 <cstdio>
#include <cstring>

#define MAX(A, B) (A) > (B) ? (A) : (B)

using namespace std;

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

int main() {
	int N, W;
	int c = 0;
	memset(dp, 0, sizeof dp);
	while (cin >> W, W) {
		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 = W; j >= 0; 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];
			}
		}
		while (dp[N][W] == dp[N][W - 1]) W--;
		cout << "Case " << ++c << ':' << endl;
		cout << dp[N][W] << endl << W << endl;
	}

	return 0;
}

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

constexpr int MAX = 1000;

int dp[MAX][MAX + 1];
int W, n;
int v[MAX], w[MAX];

int main() {
	int cnt = 0;
	while (1) {
		std::cin >> W;
		if (W == 0)
			break;
		++cnt;
		std::cin >> n;
		char ch;
		for (int i = 0; i < n; ++i)
			std::cin >> v[i] >> ch >> w[i];
		std::memset(dp, 0, sizeof(dp));
		dp[0][w[0]] = v[0];
		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])
					dp[i][j] = std::max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
			}
		}
		int ans = 0, maxv;
		for (int i = W; i >= 0; --i) {
			std::cout <<i<<' '<< dp[n - 1][i] << std::endl;
			if (ans <= dp[n - 1][i]) {
				ans = dp[n - 1][i];
				maxv = i;
			}
		}
		std::cout << "Case " << cnt << ':' << std::endl
			<< ans << std::endl << maxv << std::endl;
	}
	return 0;
}

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

using namespace std;


int main(){
	int dp[10100];
	
	int W,N;
	int v[1010],w[1010];
	
	int count=0;
	
	while(cin>>W,W){
		cin>>N;
		count++;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&v[i],&w[i]);
		}
		
		for(int j=0;j<W+1;j++){
			if(j<w[0]){
				dp[j]=0;
			}else{
				dp[j]=v[0];
			}
		}
		
		for(int i=1;i<N;i++){
			for(int j=W;j;j--){
				if(j<w[i]){
					dp[j]=dp[j];
				}else{
					dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
				}
			}
		}
		for(int i=1;i<W+1;i++){
			cout<<dp[i]<<" "<<endl;
		}
		cout<<"Case "<<count<<":"<<endl;
		cout<<dp[W]<<endl;
		while(dp[W]==dp[W-1])W--;
		cout<<W<<endl;
	}
	
	
	
	
	return 0;
	
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <ctype.h>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> dP;

const int INF = 1000000000;
const double EPS = 1e-10;

int main(){
//	freopen("test.txt", "r", stdin);
	int casenum = 0;
	while(1){
		int W, n;
		cin >> W >> n;
		if(!W) break;

		vector<int> w(n+1);
		vector<int> v(n+1);
		FOR(i, 1, n+1){
			scanf("%d,%d", &v[i], &w[i]);
		}

		vector<vector<int> > table(n+1, vector<int>(W+1));

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


		int idx = 0;
		rep(i, W+1){
			if(table[n][idx] < table[n][i]){
				idx = i;
			}
		}

		cout << "Case " << ++casenum << ':' << endl;
		cout << table[n][idx] << endl;
		cout << idx << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
using namespace std;
int inf = 1000000000;
// dp[i][j] = i ???????????§??§?????? j ??§?????????
// dp[i][j] = dp[i][j-?] + v[i];

int dp[1001][1001];

int main(){

  int W, N, l;
  l = 1;
  while(cin >> W){
    if( W == 0 ) break;
    cin >> N;
    vector<int> v(N);
    vector<int> w(N);
    for(int i=0; i<N; i++) scanf("%d,%d", &v[i], &w[i]);

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

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

    int ansi = 0;
    int ans = 0;
    for(int i=0; i<=W; i++){
      if( ans < dp[N][i] ){
        ans = dp[N][i];
        ansi = i;
      }
    }

    cout << "Case " << l << ":" << endl;
    cout << ansi << endl;
    cout << ans << endl;

    l++;
  }




  return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <cstring>
using namespace std;

int main() {
    int val[1001], W, N, v, w, caseNo = 1;
    char c;
    while (cin >> W, W) {
        memset(val, -1, sizeof val);
        val[0] = 0;
        cin >> N;
        for (int i=0; i<N; ++i) {
            cin >> v >> c >> w;
            for (int j=W; j>=0; --j) {
                if (0 <= val[j] && j + w <= W) {
                    val[j + w] = max(val[j + w], val[j] + v);
                }
            }
        }
        int maxv = 0, maxw = 0;
        for (int i=0; i<=W; ++i) {
            if (maxv < val[i]) {
                maxv = val[i];
                maxw = i;
            }
        }
        cout << "Case " << caseNo << ":" << endl;
        cout << maxv << endl;
        cout << maxw << endl;
        ++ caseNo;
    }
    return 0;
}

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

int main(){
  int W, N, cs=0;
  int cost[1001], weight[1001];
  int dp[1001];
  
  while(cin >> W, W){
    cin >> N;
    for(int i = 0 ; i < N ; i++){
      scanf("%d,%d", &cost[i], &weight[i]);
    }
    fill(dp, dp+1001, -1);
    dp[0] = 0;

    for(int i = 0 ; i < N ; i++){
      for(int j = W-weight[i] ; j >= 0 ; j--){
	if(dp[j] >= 0) dp[j+weight[i]] = max(dp[j+weight[i]], dp[j]+cost[i]);
      }
    }

    int Cost = 0, Weight = 0;
    
    for(int i = 0 ; i < W ; i++){
      if(dp[i] > Weight){
	Cost = dp[i];
	Weight = i;
      }
    }
    printf("Case %d:\n%d\n%d\n", ++cs, Cost, Weight);
  }
  return 0;
}