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 <algorithm>
#include <utility>
#include <string>
#include <sstream>
using namespace std;

const int W_MAX = 1000;
const int N_MAX = 1000;

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


typedef pair<int,int> P;

P bfs(int i, int j)
{
	if(i == N)return P(0,0);
	if(w[i] > j){
		return bfs(i+1,j);
	}
	else{
		P p1,p2;
		p1 = bfs(i + 1,j);
		p2 = bfs(i + 1,j - w[i]);
		p2.first += v[i];p2.second += w[i];
		if(p1.first == p2.first){
			if(p1.second < p2.second){
				return p1;
			}
			else{
				return p2;
			}
		}
		else{
			return max(p1,p2);
		}
	}
}

int main()
{
	int c = 1;
	while(cin >> W,W){
		cin >> N;
		for(int i = 0; i < N; ++i){
			cin >> v[i]; cin.ignore(1,','); cin >> w[i];
		}
		cout << "Case " << c++ << ':' << endl;
		P p = bfs(0,W);
		cout << p.first << endl << p.second << endl;
	}
}

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

int main() {
    int W;
    int c = 0;
    while(++c, cin >> W, W) {
        int N;
        cin >> N;
        vector<vector<int>> dp(N+1, vector<int>(W+1, -1));
        dp[0][0] = 0;
        for(int i=0; i<N; ++i) {
            int v, w;
            scanf("%d,%d", &v, &w);
            for(int j=0; j<=W; ++j) {
                dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
                if(j+w <= W && dp[i][j] != -1) {
                    dp[i+1][j+w] = max(dp[i+1][j+w], dp[i][j] + v);
                }
            }
        }
        int w = 0;
        for(int i=0; i<=W; ++i) {
            if(dp[N][w] < dp[N][i]) {
                w = i;
            }
        }
        cout << "Case " << c << ':' << endl;
        cout << dp[N][w] << endl;
        cout << w << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstdio>
 
using namespace std;
 
int W , N ;
int dp[1024][1024];
int w[1024] , v[1024],O;
char cn;

int main ()
{
  int cont = 1;
  while(cin >> W >> N){
    
    for(int i = 0; i < N; i++)
      cin >> v[i] >> cn >> w[i] ;
    
    for(int i = N-1; i >= 0; i--)
      {
        for(int j = 0; j <= W; j++)
	  {
            if( j < w[i]) dp[i][j] = dp[i+1][j];
            else dp[i][j] = max( dp[i+1][j] , dp[i+1][j-w[i]]+v[i] );
	  }
      }
    for(int i = 0; i <=W; i++)
      if(dp[0][i]==dp[0][W]){
	O = i;
	break;
      }
    cout << "Case " << cont <<':' <<endl;
    cout << dp[0][W] << endl << O <<endl;
    //cout << << endl;
    /*  
    for(int i = 0; i < 10 ; i++)
      {       
	for(int j = 0; j <= 50; j++)
	  printf("%3d", dp[i][j]);
        puts("");
      }    
      }*/
    cont++;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

int W, N;
int v[1000], w[1000];
pair<int, int> dp[1100][1100];

pair<int, int> rec(int i, int j)
{
	pair<int, int> ans;

	if (dp[i][j].first != -1 && dp[i][j].second != -1)
	{
		return dp[i][j];
	}

	if (i == N)
	{
		ans = pair<int, int>(0, W - j);
	}
	else if (j < w[i])
	{
		ans = rec(i + 1, j);
	}
	else
	{
		pair<int, int> vw1 = rec(i + 1, j);
		pair<int, int> vw2 = rec(i + 1, j - w[i]);
		vw2.first += v[i];
		if (vw1.first > vw2.first)
		{
			ans = vw1;
		}
		else if (vw1.first < vw2.first)
		{
			ans = vw2;
		}
		else if (vw1.first == vw2.first)
		{
			if (vw1.second < vw2.second)
			{
				ans = vw1;
			}
			else
			{
				ans = vw2;
			}
		}
	}

	dp[i][j] = ans;

	return ans;
}

signed main()
{
	int cnt = 1;

	while (true)
	{
		cin >> W;
		if (W == 0)
		{
			break;
		}
		cin >> N;
		rep(i,N)
		{
			scanf("%d,%d", &v[i], &w[i]);
		}

		rep(i,1100)
		{
			rep(j,1100)
			{
				dp[i][j] = pair<int, int>(-1, -1);
			}
		}

		pair<int, int> ans = rec(0, W);

		printf("Case %d:\n", cnt);
		printf("%d\n", ans.first);
		printf("%d\n", ans.second);

		cnt++;
	}
}

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] != -1 && dp_rw[n][w] != -1) {
        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;

        // 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;
        for (int i = 0; i < 1000; ++i)
            for (int j = 0; j < 1000; ++j)
                dp_v[i][j] = dp_rw[i][j] = -1;
        rec_dp(0, W, retv, retw, W, N, v, w);

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

    return 0;
}