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 <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) {
			if (sum == mv && sum != 0) {
				mw = min(w - W, mw);
				return;
			}
			mw = max(w - W, 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>
#include <cstdio>
using namespace std;
#define MAX 1001
typedef pair<int,int> P;

int dp[MAX][MAX],t,w,m;

void judge(vector<P> v){
    
    for(int i = 1 ; i <= m ; i++){
        for(int j = 1 ; j <= w ; j++){
            int k = j - v[i].first;
            if(k < 0){
                dp[i][j] = dp[i-1][j];
            }
            else{
                dp[i][j] = max(dp[i-1][j],dp[i-1][k]+v[i].second);
            }
        }
    }
    int ans_W=0,ans_C;
    for(int i = 1 ; i <= w ; i++){
        ans_W = max(ans_W,dp[m][i]);
    }
    for(int i = w ; i >= 1 ; i--){
        if(ans_W==dp[m][i]) ans_C = i;
    }
    
    cout << ans_W << endl;
    cout << ans_C << endl;
}

void print(){
    cout << "Case " << t++ << ":" << endl;
}

int main(){
    int val,wei;
    
    t = 1;
    while(cin >> w,w){
        cin >> m;
        vector<P> v;
        
        fill(dp[0],dp[MAX-1],0);
        v.push_back(P(0,0));
        
        print();
        for(int i = 1 ; i <= m ; i++){
            scanf("%d,%d" ,&val ,&wei);
            v.push_back(P(wei,val));
        }
        sort(v.begin(),v.end());
        judge(v);
    }
    
    return 0;
}

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

class treasure
{
public:
int value, weight;
bool have;
};

treasure tre[1000];
int N,W, VALUE = 0, WEIGHT = 999999;


void calcu()
{
	int sum_v = 0, sum_w = 0;
	for (int i = 0; i < N; i++)
	{
		if (tre[i].have == true)
		{
			sum_v += tre[i].value;
			sum_w += tre[i].weight;
		}
	}
	if (sum_v>VALUE)
	{
		WEIGHT = sum_w;
		VALUE = sum_v;
	}
	else if(sum_v==VALUE &&  WEIGHT>sum_w)
	{
		 WEIGHT = sum_w;
	}
}

void function(int n, int w)//0,W
{
	if (n >= N)	
	{
		calcu();
		//cout << VALUE << "," << WEIGHT << endl;
	}
	else if (tre[n].weight > w)	
	{
		tre[n].have=false;
		function(n + 1, w);
	}
	else
	{
		tre[n].have = true;	  
		function(n + 1, w - tre[n].weight);
		tre[n].have = false;   
		function(n + 1, w);
	}
}

int main()
{
	int W;
	for (int count = 1; cin >> W, W != 0; count++)
	{
		VALUE = 0;
		WEIGHT = 999999;
		cin >> N;
		for (int i = 0; i < N; i++)
		{
			scanf_s("%d,%d", &tre[i].value, &tre[i].weight);
		}
		function(0,W);
		cout << "case " << count << ":" << endl;
		cout << VALUE << endl;
		cout << WEIGHT << endl;
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
#define W_MAX 1000
#define N_MAX 1000

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

int main(){
	int c = 1;
	while (1){
		scanf("%d %d", &W, &N);
		if (W == 0)
			break;
		for (int i = 1; i <= N; ++i){
			char ch = ',';
			scanf("%d %s %d", &v[i], &ch, &w[i]);
		}
		for (int i = 1; i <= W; ++i)
			dp[0][i] = 0;
		for (int i = 1; i <= N; ++i){
			for (int j = 1; j <= W; ++j){
				if (j < w[i])
					dp[i][j] = dp[i - 1][j];
				else{
					dp[i][j] = std::max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
				}
			}
		}
		int minv = 1;
		for (int i = 0; i <= W; ++i){
			if (dp[N][minv] < dp[N][i])
				minv = i;
		}
		printf("%d %s \n", "Case", c, ":");
		printf("%d\n", dp[N][W]);
		printf("%d\n", minv);
		++c;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int tr[1100][2]={0},dp[1100][1100]={0};
int main(){
  for(int c=1;;c++){
    int w,n,ans = 0,q;
    cin >>w;
    if(w == 0) break;
    cin >>n;
    for(int i=0; i<1100; i++){
      tr[i][0] = tr[i][1] = 0;
      for(int j=0; j<1100; j++) dp[i][j] = 0;
    }
    for(int i=1; i<=n; i++) scanf("%d,%d",&tr[i][0],&tr[i][1]);
    for(int i=1; i<=n; i++){
      for(int j=0; j<=w; j++){
	if(j == 0) dp[i][tr[i][1]] = max(dp[i][tr[i][1]],tr[i][0]);
	if(dp[i-1][j]!=0){
	  if(j+tr[i][1]<=w) dp[i][j+tr[i][1]] = max(dp[i][j+tr[i][1]],dp[i-1][j]+tr[i][0]);
	  else dp[i][j] = max(dp[i][j],dp[i-1][j]);
	}
      }
    }
    for(int i=0; i<=w; i++){
      if(ans<dp[n][i]){
	ans = dp[n][i];
	q = i;
      }
    }
    cout <<"Case "<<c<<":"<<endl<<ans<<endl<<q<<endl;
  }
  return 0;
}

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

#define rep(i, n) for(int i = 0; i < n; i++)

using namespace std;

int main(){
	int c = 1;
	int w;
	while(cin >> w && w){
		int n;
		int tr[1024][2];
		// 価値を最大に
		// 入れた数 重さ合計 価値
		int dp[1024][1024];
		
		cin >> n;
		rep(i, n){
			scanf("%d, %d", &tr[i][0], &tr[i][1]);
		}

		rep(i, n){
			dp[1][tr[i][1]] = tr[i][0];
		}
		
		rep(i, n){
			for(int j = 0; j <= w; j++){
				if(dp[i][j] != 0){
					int value = dp[i][j] + tr[i][0];
					int we = j + tr[i][1];
					if(we <= w){	
						dp[i + 1][we] = max(dp[i+1][we], value);
					} else {
						dp[i + 1][j] = max(dp[i+1][j], dp[i][j]);
					}
				}
			}
		}
		
		int mv,mw;
		mv = 0; mw = 0;
		rep(i, n){
			for(int j = 0; j <= w; j++){
				if(dp[i][j] > mv){
					mv = dp[i][j];
					mw = j;
				}
			}
		}

		cout << "Case " << c << ':' << endl;
		cout << mv << endl;
		cout << mw - 1<< endl;

		c++;
	}
	return 0;
}

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

using namespace std;

int main() {
	int n, W;
	char dummy;
	int count = 1;

	while( true ) {

		cin >> W;
		if(W == 0) {
			break;
		}
		cin >> n;

		vector< int > item_val(n);
		vector< int > item_wei(n);
		vector< vector< int > > nap(n + 1, vector< int >(W + 1, 0));

		for(int i = 0; i < n; i++) {
			cin >> item_val[i] >> dummy >> item_wei[i];
		}

		for(int i = 0; i < n; i++) {
			for(int j = 0; j <= W; j++) {
				if(j + item_wei[i] <= W) {
					nap[i + 1][j + item_wei[i]] = nap[i][j] + item_val[i];
				}
			}
		}

		int max_value = 0;
		for(int i = 0; i <= n; i++) {
			for(int j = 0; j <= W; j++) {
				max_value = max(max_value, nap[i][j]);
			}
		}
		int max_weight = W;
		for(int i = 0; i <= n; i++) {
			for(int j = 0; j <= W; j++) {
				if(nap[i][j] == max_value) {
					max_weight = min(max_weight, j);
				}
			}
		}

		cout << "Case :" << count << endl;
		cout << max_value << "\n" << max_weight << endl;
		count++;

	}
}