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

#define ll (long long)
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)

using namespace std;

/*thief??????*/

int main(void){
    
    /*(x,y) = (??????,??????)*/
    int dp[1001][1001] = {0};
    int w[1001] = {0},v[1001] = {0};
    int endurew;
    int number;
    int answer = 0;
    int casecounter = 1;
    while(cin >> endurew,endurew)
    {
        cin >> number;
        for(int i  =1; i<= number;i++)
            scanf("%d%*c%d",&v[i],&w[i]);
        
        for(int j = 1; j<= number;j++)
            for(int k = 0;k<=endurew;k++)
            {
              if(j == 1 && k>=w[j])
                  dp[j][k] = v[j];
              else if(k >= w[j])
                  dp[j][k] = v[j]+dp[j-1][k-w[j]];
              else
                  dp[j][k] = dp[j-1][k];
                
            }
        int max = 0;
        for(int i = 1;i<=endurew;i++)
        {
            if(max < dp[number][i])
            {
                answer = i;
                max = dp[number][i];
            }
            
        }
        cout << "Case " << casecounter<< ":" << endl << dp[number][answer]<< endl;
        cout << answer << endl;
        casecounter++;
    }
    return 0;
    
}

C++: #include "math.h"
#include "stdio.h"
#include <algorithm>
#include <string>
#include <map>
#include <iostream>

using namespace std;

/** Problem0042 : A Thief **/
int W, N, tre[1100][2]={0}, solW, solN;
void solve(int n, int i, int value)
{
	if (n > W || i>N)
		return;
	
	if (value > solW) {
		solW = value;
		solN = n;
	} else if (value == solW) {
		solN = min(n, solN);
	}
	

	
	solve(n+tre[i][1], i+1, value+tre[i][0]);
	solve(n, i+1, value);
}

int main()
{
	int set=0; char cam;
	while (1) {
		set++;
		
		cin >> W;
		solW = 0; solN = 0;
		if (W==0) break;
		cin >> N;
		for (int i=0; i<N; i++) {
			cin >> tre[i][0] >> cam >> tre[i][1];
		}
		solve(0, 0, 0);
		cout << "Case " << set << ":" << endl;
		cout << solW << endl;
		cout << solN << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<stdio.h>

int dp[1000][10000];
int W;
int n;
int v[1000], w[1000];
int now = 1;

int main()
{
	while( 1 ){
		std::cin >> W;
		if( !W ) break;
		std::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] = std::max( dp[i][j], dp[i][j-w[i]] + v[i] );
				}
			}
		}
		
		int res = W;
		for( int i = W; i >= 0; i-- ){
			if( dp[n][i] == dp[n][W] ) res = std::min( res, i );
			else break;
		}
		
		std::cout << "Case " << now << ":" << std::endl;
		std::cout << dp[n][W] << std::endl;
		std::cout << res << std::endl;
		
		now++;	
	}
	

 	return 0;
}

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

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	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];

	while(cin >> w){
		if(w == 0) break;
		int n;
		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;
		for(int i = 0; i <= w; i++){
			cost[i] = 0;
			best[i] = 0;
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<deque>
using namespace std;
typedef pair<int,int> P;

int W,N,v[1000],w[1000];
int I;

P dp[1000][1000];
bool done[1000][1000];
P solve(int i,int r){
    if(done[i][r]) return dp[i][r];
    P result;
    if(i==N) result=P(0,r);
    else {
        if(r-w[i]>=0){
            P p=solve(i+1,r-w[i]);
            p.first+=v[i];
            result=max(p,solve(i+1,r));
        }else{
            result=solve(i+1,r);
        }
    }
    done[i][r]=true;
    return dp[i][r]=result;
}

int main(){
    while(I++,scanf("%d%d",&W,&N),W){
        for(int i=0;i<N;i++)scanf("%d,%d",v+i,w+i);
        memset(done,0,sizeof(done));
        P p=solve(0,W);
        printf("Case %d:\n%d\n%d\n",I,p.first,W-p.second);
    }
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int weight, n, v, w, dp[1050];
int main(){
    int t = 0;
    while(cin >> weight, weight){
	fill(dp, dp + 1050, 0);
	t++;
	cin >> n;
	for(int i = 0;i < n;i++){
	    scanf("%d,%d", &v, &w);
	    for(int j = 1000;j >= 0;j--){
		if(j - w >= 0)
		    dp[j] = max(dp[j], dp[j - w] + v);
	    }
	}
	int now = weight;
	for(int i = weight;i >= 0;i--){
	    if(dp[i] >= dp[now])now = i;
	}
	cout << "Case " << t << ":" << endl;
	cout << dp[now] << endl;
	cout << now << endl;
    }
    return 0;
}

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

int dp[1010][1010];

int main() {
	int w;
	int x = 1;
	while (cin >> w, w) {
		int n;
		cin >> n;
		int v[1005], m[1005];
		for (int i = 0;i < n;i++) {	scanf("%d,%d", &v[i], &m[i]);}

		for (int j = 0;j <= w;j++)dp[n][j] = 0;


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

		int k=0, aw=0;
		for (int i = 0;i <= w;i++) {
			if (dp[0][i] > k) {
				k = dp[0][i];
				aw = i;
			}
		}
		cout << "Case " << x << ":" << endl;
		cout << k << endl << aw << endl;
		x++;
	}
	return 0;
}