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 <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>

using namespace std;

vector< vector<int> > dp;
vector<int> w,v;
int n,weight;

int rec(int i,int j){

	if(dp[i][j]>=0) return dp[i][j];

	int res;
	if(i==n) res=0;
	else if(j<w[i]) res=rec(i+1,j);
	else res=max(rec(i+1,j),rec(i+1,j-w[i])+v[i]);

	return dp[i][j]=res;
}

int main(){

	int count=1;
	cin>>weight;
	while(weight!=0){
		cin>>n;
		w.resize(n); v.resize(n);
		for(int i=0;i<n;i++) scanf("%d,%d",&v[i],&w[i]);

		dp = vector< vector<int> >(n+1);
		for(int i=0;i<n+1;i++) dp[i]=vector<int>(weight+1,-1);

		int ans_v=rec(0,weight);
		cout<<"Case "<<count<<":"<<endl<<ans_v<<endl;
	
		weight--;
		while(weight){
			dp = vector< vector<int> >(n+1);
			for(int i=0;i<n+1;i++) dp[i]=vector<int>(weight+1,-1);
			if(ans_v!=rec(0,weight)) break;
			weight--;
		}
		cout<<weight+1<<endl;

		count++;
		cin>>weight;

	}


	return 0;

}

C++: #include <bits/stdc++.h>

using namespace std;

#define MAX 1024

int main()
{
    int W, N, T = 1;
    while (cin >> W, W) {
        cin >> N;
        char c;
        vector<int> v(N), w(N);
        for (int i = 0; i < N; i++) {
            cin >> v[i] >> c >> w[i];
        }

        int dp[MAX][MAX] = {{}};
        for (int i = 0; i < N; i++) {
            for (int j = 0; j <= W; j++) {
                if (j - w[i] < 0) {
                    dp[i+1][j] = dp[i][j];
                } else {
                    dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]] + v[i]);
                }
            }
        }
        
        int max_v = *max_element(dp[N], dp[N] + W), min_w = -1;
        for (int i = W; i >= 0; i--) {
            if (dp[N][i] == max_v) {
                min_w = i;
            }
        }        
        cout << "Case " << T++ << ":" << endl;
        cout << max_v << endl;
        cout << min_w << endl;
    }
    return 0;
}

C++: // DP
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX_N 1001
#define MAX_W 1001
using namespace std;

int main(){
	int caseNum = 1;
	int ans;
	int W, n;
	int v[MAX_N];
	int w[MAX_N];
	int dp[MAX_N][MAX_W];
	while(cin >> W){
		if(W == 0){
			break;
		}
		cin >> n;
		for (int i = 0; i < n; i++) {
			scanf("%d,%d", &v[i], &w[i]);
		}
		
		memset(dp, 0, sizeof(dp));
		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] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
				}
			}
		}
		// answer
		cout << "Case " << caseNum++ << ":" << endl;
		for (int i = W; i >= 0; i--) {
			if(dp[n][W] != dp[n][i]) {
				ans = i+1;
				break;
			}
		}
		cout << dp[n][W] << endl;
		cout << ans << endl;
	}
	return 0;
}

C++: #include <cstdio>

using namespace std;

void rec(int n, int w, int &v, int &rw,
        const int W, const int N,
        const int lv[], const int lw[])
{
    v = rw = 0;
    if (n == N) {
        // not remain any treasure
        v = 0;
        rw = w;
    } else if (w < lw[n]) {
        // cannot pack treasure[n]
        rec(n + 1, w, v, rw, W, N, lv, lw);
    } else {
        int v1, v2, w1, w2;

        // through treasure[n]
        rec(n + 1, w        , v, rw, W, N, lv, lw);
        v1 = v;
        w1 = rw;
        // pack treasure[n]
        rec(n + 1, w - lw[n], v, rw, W, N, lv, lw);
        v2 = v + lv[n];
        w2 = rw - lw[n];

        // which is good
        if (v1 > v2) {
            v = v1;
            rw = w1;
        } else if (v1 < v2) {
            v = v2;
            rw = w2;
        } else {
            if (w1 >= w2) {
                v = v1;
                rw = w1;
            } else {
                v = v2;
                rw = w2;
            }
        }
    }
    // printf("  (%d, %d, %d, %d) -> (%d, %d)\n", n, w, v, rw, v, rw);
}

int dp_v[1000][1000];
int dp_rw[1000][1000];

void rec_dp(int n, int w, int &v, int &rw,
        const int W, const int N,
        const int lv[], const int lw[])
{
    // memo'd
    if (dp_v[n][w] != 0 && dp_rw[n][w] != 0) {
        v = dp_v[n][w];
        rw = dp_rw[n][w];
        return;
    }

    v = rw = 0;
    if (n == N) {
        // not remain any treasure
        v = 0;
        rw = w;
    } else if (w < lw[n]) {
        // cannot pack treasure[n]
        rec_dp(n + 1, w, v, rw, W, N, lv, lw);
    } else {
        int v1, v2, w1, w2;

        // through treasure[n]
        rec_dp(n + 1, w        , v, rw, W, N, lv, lw);
        v1 = v;
        w1 = rw;
        // pack treasure[n]
        rec_dp(n + 1, w - lw[n], v, rw, W, N, lv, lw);
        v2 = v + lv[n];
        w2 = rw - lw[n];

        // which is good
        if (v1 > v2) {
            v = v1;
            rw = w1;
        } else if (v1 < v2) {
            v = v2;
            rw = w2;
        } else {
            if (w1 >= w2) {
                v = v1;
                rw = w1;
            } else {
                v = v2;
                rw = w2;
            }
        }
    }
    // printf("  (%d, %d, %d, %d) -> (%d, %d)\n", n, w, v, rw, v, rw);
    dp_v[n][w] = v;
    dp_rw[n][w] = rw;
}

int main()
{
    for (int icase = 1;; ++icase) {
        int W, N;
        scanf("%d", &W);
        if (W == 0)
            break;
        scanf("%d", &N);

        int v[1000], w[1000];
        for (int i = 0; i < N; ++i)
            scanf("%d,%d", &v[i], &w[i]);

        int retv, retw;
        rec(0, W, retv, retw, W, N, v, w);

        printf("Case %d:\n%d\n%d\n", icase, retv, (W - retw) / 2);
    }

    return 0;
}

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

int T, N, V[1010], W[1010];

int main() {

	for ( int t = 1; scanf( "%d", &T ) && T; t++ ) {
	
		scanf( "%d", &N );
	
		for ( int i = 0; i < N; i++ ) { scanf( "%d,%d", &V[i], &W[i] ); }
	
		int A[1010][1010] = {{0}};
	
		for ( int i = 1; i < N; i++ ) {
		
			for ( int j = 0; j < T; j++ ) {
	
				if ( j < W[i] ) {
	
					A[i][j] = A[i-1][j];
	
				} else {
	
					A[i][j] = max( A[i-1][j], A[i-1][j-W[i-1]] + V[i-1] );
					
				}
	
			}
	
		}
		
		int best = -1, best_w = -1;
		
		for ( int j = 0; j <= T; j++ ) {
		
			if ( A[N][j] > best ) {
			
				best = A[N][j];
				best_w = j;
				
			}
		
		}
		
		printf( "Case %d:\n%d\n%d\n", t, best, best_w );
	
	}

}