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 <stdio.h>
#include <stdlib.h>
#include <string.h>

int dp[1002][1002];

int main(void)
{
	int w;
	int n;
	int cnt;
	int max;
	
	cnt = 1;
	while (1){
		int value[1000];
		int weight[1000];
		
		scanf("%d", &w);
		
		if (w == 0){
			break;
		}
		
		scanf("%d", &n);
		
		memset(dp, -1, sizeof(dp));
		
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &value[i], &weight[i]);
			dp[i][0] = 0;
		}
		
		for (int i = 1; i <= n; i++){
			for (int j = 0; j <= w; j++){
				if (dp[i - 1][j] != -1 && j + weight[i - 1] <= w){
					dp[i][j + weight[i - 1]] = dp[i - 1][j] + value[i - 1];
				}
				if (dp[i - 1][j] != -1){
					dp[i][j] = dp[i - 1][j];
				}
			}
		}
		
		max = 0;
		for (int i = 1; i <= w; i++){
			if (dp[n][i] > dp[n][max]){
				max = i;
			}
		}
		
		printf("Case %d:\n%d\n%d\n", cnt++, dp[n][max], max);
	}
	
	return (0);
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;

int N;
int W;
vector<int> w, v;
int main(void) {
    int count = 1;
    while (1) {
        cin >> W;
        if (W == 0) return 0;
        cin >> N;
        for (int i = 0; i < N; i++) {
            int tmp, tmp2;
            scanf("%d, %d", &tmp, &tmp2);
            v.push_back(tmp);
            w.push_back(tmp2);
        }
        // dp[i][j]はでjまでで重さがiまで選んでいるとき得られる最高の価値
        long long dp[N+1][W+1];
        for (int i = 0; i <= N; i++) {
            for (int j = 0; j <= W; j++) {
                dp[i][j] = 0;
            }
        }
        for (int i = 1; i <= N; i++) {
            for (int j = 0; j <= W; j++) {
                if (j - w[i-1] >= 0) {
                    dp[i][j] = max(dp[i-1][j-w[i-1]] + v[i-1], dp[i][j]);
                }
            }
        }
        int topvalue = 0, indexweight = 0;
        for (int j = 0; j <= W; j++) {
            if (topvalue < dp[N][j]) {
                topvalue = dp[N][j];
                indexweight = j;
            }
        }
        cout << "Case " << count << ":" << endl;
        cout << topvalue << endl;
        cout << indexweight << endl;
        count++;
    }
    return 0;
}

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

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

int main()
{
  int cnt = 1;
  while (cin >> W) {
    if (W == 0)
      break;
    cin >> N;
    for (int i = 0; i < N; ++i) {
      char c;
      cin >> v[i] >> c >> w[i];
    }
    for (int i = 0; i < 1001; ++i) {
      for (int j = 0; j < 1001; ++j)
	dp[i][j] = 0;
    }

    int value = 0, weight = 1 << 30;
    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]);
	else
	  dp[i+1][j] = dp[i][j];
      }
    }

    value = dp[N][W];
    for (int i = 0; i <= N; ++i) {
      for (int j = 0; j <= W; ++j) {
	if (value == dp[i][j])
	  weight = min(weight, j);
      }
    }

    cout << "Case " << cnt++ << ":" << endl << value << endl << weight << endl;
  }
  return 0;
}

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

int main(){
	int dp[10010];
	int W,N;
	int v[1010],w[1010];
	int c=0;
	while(scanf("%d",&W),W!=0){
		c++;
		scanf("%d",&N);;
		for(int i=0;i<N;i++){
			scanf(" %d,%d",&v[i],&w[i]);
		}
		for(int i=0;i<W+5;i++){
			dp[i]=0;
		}
		for(int i=0;i<N;i++){
			for(int j=W;j;j--){
				if(j<w[i]){
					dp[j]=dp[j];
				}else{
					dp[j]=std::max(dp[j],dp[j-w[i]]+v[i]);
				}
			}
		}
		int ans_w=W;
		for(int i=W-1;i;i--){
			if(dp[i]==dp[i+1]){
				ans_w=i;
			}else{
				break;
			}
		}
		cout<<"Case "<<c<<":"<<endl<<dp[W]<<endl<<ans_w<<endl;
		
	}
	
	
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int dp[1005][10005];
int main(){
      int w,n,v1,w1,solve,solve_,count = 0;
      do{
            cin >> w;
            if(!w)break;
            cin >> n;

            count++;
            solve =solve_=  -1;
            for(int i = 0;i<1005;i++)for(int j = 0;j<10005;j++)dp[i][j] = -1;
            dp[0][0] = 0;
            for(int i =0 ;i< n;i++)
            {
                  scanf("%d,%d",&v1,&w1);
                  for(int j = 0;j<=w;j++)
                  {
                        if(dp[i][j]!=-1)
                        {
                              dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
                              dp[i+1][j+w1] = max(dp[i][j]+v1,dp[i+1][j+w1]);
                        }
                  }
            }
            for(int i = 0;i<=w;i++){
                  if(dp[n][i]>solve){
                        solve = dp[n][i];
                        solve_ = i;
                  }
            }
            cout << "Case "<<count << ':'<<endl<< solve<<endl<<solve_<<endl;



      }while(1);
}

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

int a[1001] = { 0 };
int b[1001] = { 0 };
int w, n, mv, mw;
void solve(int W, int f, int sum) {
	f = min(f, n) + 1;
	for (int i = f; i < n; i++) {
		if (W - b[i] < 0) {
			int tmp = w - W;
			mw = max(w - W, mw);

			if (sum == mv) mw = min(tmp, mw);
			mv = max(sum, mv);
			return;
		}
		solve(W - b[i], f, sum + a[i]);
	}
}

int main() {
	int u = 0;
	while (scanf("%d", &w)) {
		if (w == 0) break;
		scanf("%d", &n);

		mv = 0;
		mw = w;
		u++;
		for (int i = 0; i < n; i++) {
			scanf("%d,%d", &a[i], &b[i]);
		}

		solve(w, -1, 0);
		printf("Case %d:\n%d\n%d\n", u, mv, mw);
	}
	return 0;
}

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

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	while(cin >> w){
		if(w == 0) break;
		int n;
		int cost[maxw + 1];
		int best[maxw + 1];
		for(int i = 0; i <= maxw; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		int values[maxw + 1];
		int weights[maxw + 1];
		cin >> n;
		for(int i = 0; i < n; i++){
			char c;
			cin >> values[i] >> c >> weights[i];
		}
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= weights[j])
					if(cost[k] < cost[k - weights[j]] + values[j])
					{
						cost[k] = cost[k - weights[j]] + values[j];
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;w>0; w--) if(cost[w] != cost[w-1]) break;
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - weights[best[l]] >= 0;){
			if(l - weights[best[l]] >= 0){
				total += weights[best[l]];
				l -= weights[best[l]];
			}
		}
		cout << total << endl;
	}
	return 0;
}