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>
using namespace std;

#define SAFE_DELETE( x ){ if( x ){ delete x; x = NULL; } }

struct tCell{
	tCell() : value( 0 ), weight( 0 ), unitV( 0.0 ), prev( NULL ), next( NULL ){}

	int value, weight;
	double unitV;
	tCell *prev, *next;
};

class MyArray{
public:
	MyArray() : mSize( 0 ), head( NULL ), tail( NULL ){
		head = new tCell;
		tail = new tCell;
		head->next = tail;
		tail->prev = head;
	}
	~MyArray(){
		init();
		SAFE_DELETE( tail );
		SAFE_DELETE( head );
	}

	void init(){
		for ( int i = mSize; i > 0; --i ){
			tCell* cell = tail->prev;
			cell->prev->next = cell->next;
			cell->next->prev = cell->prev;
			SAFE_DELETE( cell );
		}
		mSize = 0;
	}
	void pushBack( int value, int weight ){
		++mSize;
		tCell* cell = new tCell;
		cell->value = value;
		cell->weight = weight;
		cell->unitV = static_cast< double >( value ) / weight;
		cell->prev = tail->prev;
		cell->next = tail;
		cell->prev->next = cell;
		cell->next->prev = cell;
	}
	int size(){ return mSize; }

	tCell operator[]( int index ){
		tCell* cell = head->next;
		for ( int i = 0; i < index; ++i ){
			cell = cell->next;
		}
		return *cell;
	}

private:
	int mSize;
	tCell *head, *tail;
};

int main(){

	MyArray treList;
	int time = 1, c;
	int v = 0, w = 0;
	char d;

	while ( cin >> c ){
		if ( !c ) break;
		int n;
		treList.init();
		cin >> n;
		for ( int i = 0; i < n; ++i ){
			cin >> v >> d >> w;
			treList.pushBack( v, w );
		}

		int maxV = 0, maxW = 0;
		int* table = new int[ ( n+1 ) * ( c+1 ) ];
		for ( int i = 0; i < n+1; ++i ){
			if ( i != 0 ){
			v = treList[ i - 1 ].value;
			w = treList[ i - 1 ].weight;
			}
			for ( int j = 0; j < c+1; ++j ){
				if ( i == 0 || j == 0 ){
					table[ i * ( c+1 ) + j ] = 0;
					continue;
				}
				table[ i * ( c+1 ) + j ] = table[ ( i-1 ) * ( c+1 ) + j ];
				if ( ( j - w >= 0 ) && ( table[ ( i-1 ) * ( c+1 ) + ( j-w ) ] + v > table[ ( i-1 ) * ( c+1 ) + j ] ) ){
					table[ i * ( c+1 ) + j ] = table[ ( i-1 ) * ( c+1 ) + ( j-w ) ] + v;
					if ( table[ i * ( c+1 ) + j ] > maxV ){
						maxV = table[ i * ( c+1 ) + j ];
						maxW = j;
					}else if ( table[ i * ( c+1 ) + j ] == maxV && i < maxW ){
						maxW = j;
					}
				}
			}
		}

		cout << "Case " << time << ":" << endl;
		cout << maxV << endl << maxW << endl;
		++time;

		delete[] table;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int n, w, value[1000], weight[1000], memo[1000][1000];
int dp(int val, int wei);

int main(){
  int data = 1;
  while(cin >> w, w){
    memset(memo, 0, sizeof(memo));
    cin >> n;
    for(int i = 0; i < n; i++){
      scanf("%d,%d", &value[i], &weight[i]);
    }

    int wgtsum = 0;
    int maxi = dp(0, w);
    for(int i = n-1; i >= 0 && !maxi; i++){
      for(int j = n-1; j >= 0 && !maxi; j++){
	if(memo[i][j] == maxi) wgtsum = i;
      }
    }
      
    cout << "Case " << data << ":" << endl;
    cout << maxi << endl;
    cout << w - wgtsum - 1 << endl;

  }
}

int dp(int val, int wei){
  if(memo[wei][val]) return memo[wei][val];
  if(val >= n) return 0;
  if(wei < weight[val]) return memo[wei][val] = dp(val+1, wei);
  else return memo[wei][val] = max(dp(val+1, wei-weight[val])+value[val], dp(val+1, wei));
}

C++: #include <bits/stdc++.h>
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
constexpr int INF = 1000000000;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
using namespace std;

using Weight = int;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(const int s = 0, const int d = 0, const Weight w = 0) : src(s), dst(d), weight(w) {}
};

using Edges = std::vector<Edge>;

class Graph {
  std::vector<Edges> g;

public:
  Graph(const int size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  const Edges &operator[](const int i) const { return g[i]; }
  void addArc(const int src, const int dst, const Weight w = 1) { g[src].emplace_back(src, dst, w); }
  void addEdge(const int node1, const int node2, const Weight w = 1) {
    addArc(node1, node2, w);
    addArc(node2, node1, w);
  }
};

vector<Weight> dijkstra(const Graph &g, const int src) {
  using state = pair<Weight, int>;
  priority_queue<state, vector<state>, greater<state>> q;
  vector<Weight> dist(g.size(), INF);
  dist[src] = 0;
  q.emplace(0, src);
  while (q.size()) {
    Weight d;
    int v;
    tie(d, v) = q.top();
    q.pop();
    if (dist[v] < d) continue;
    for (auto &e : g[v]) {
      if (cmin(dist[e.dst], dist[v] + e.weight)) q.emplace(dist[e.dst], e.dst);
    }
  }
  return dist;
}

int main() {
  int caseIndex = 0;
  int W;
  while (cin >> W, W) {
    cout << "Case " << ++caseIndex << ':' << endl;
    int N;
    cin >> N;
    Graph g((N + 1) * (W + 1));
    rep(i, W) g.addArc(i, i + 1, 0);
    rep(i, N) {
      int v, w;
      char gomi;
      cin >> v >> gomi >> w;
      rep(j, W + 1) {
        g.addArc(i * (W + 1) + j, (i + 1) * (W + 1) + j, 0);
        if (j + w <= W) g.addArc(i * (W + 1) + j, (i + 1) * (W + 1) + j + w, -v);
      }
    }
    vector<Weight> dist(g.size(), 0);
    // vector<Weight> dist = dijkstra(g, 0);
    int maxi = dist.back();
    cout << -maxi << endl;
    rep(i, W + 1) if (maxi == dist[N * (W + 1) + i]) {
      cout << i << endl;
      break;
    }
  }
}