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 <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;
}