Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	Case := 1
	for {
		scanner.Scan()
		W, _ := strconv.Atoi(scanner.Text())
		if W == 0 {
			break
		}
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		w := make([]int, n+1)
		v := make([]int, n+1)
		for i := 1; i <= n; i++ {
			scanner.Scan()
			info := strings.Split(scanner.Text(), ",")
			v[i], _ = strconv.Atoi(info[0])
			w[i], _ = strconv.Atoi(info[1])
		}
		// fmt.Println(v)
		// fmt.Println(w)
		dp := make([][]int, n+1)
		for i := range dp {
			dp[i] = make([]int, W+1)
		}
		maxV := -(1 << 21)
		minW := 1 << 21
		for i := 1; i <= n; i++ {
			for j := 1; j <= W; j++ {
				// fmt.Println(dp)
				if j-w[i] >= 0 {
					dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i])
					if maxV < dp[i][j] {
						maxV = dp[i][j]
						minW = j
					} else if maxV == dp[i][j] {
						minW = min(minW, j)
					}
				} else {
					dp[i][j] = dp[i-1][j]
				}
			}
		}
		fmt.Printf("Case %d:\n", Case)
		Case += 1
		fmt.Println(maxV)
		fmt.Println(minW)
	}
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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;
    }
  }
}