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 <stdio.h>
#include <stdlib.h>
#include <string.h>

int dp[1002][1002];

int main(void)
{
	int w;
	int n;
	int cnt;
	int max;
	
	cnt = 1;
	while (1){
		int value[1000];
		int weight[1000];
		
		scanf("%d", &w);
		
		if (w == 0){
			break;
		}
		
		scanf("%d", &n);
		
		memset(dp, -1, sizeof(dp));
		
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &value[i], &weight[i]);
			dp[i][0] = 0;
		}
		
		for (int i = 1; i <= n; i++){
			for (int j = 0; j <= w; j++){
				if (dp[i - 1][j] != -1 && j + weight[i - 1] <= w){
					dp[i][j + weight[i - 1]] = dp[i - 1][j] + value[i - 1];
				}
				if (dp[i - 1][j] != -1){
					dp[i][j] = dp[i - 1][j];
				}
			}
		}
		
		max = 0;
		for (int i = 1; i <= w; i++){
			if (dp[n][i] > dp[n][max]){
				max = i;
			}
		}
		
		printf("Case%2d:\n%d\n%d\n", cnt++, dp[n][max], max);
	}
	
	return (0);
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>

using namespace std;

int N,W;

struct Treasurea
{

	int value;
	int weight;
	int flag;

	Treasurea()
	{

		value = 0;
		weight = 0;
		flag = false;

	}

};

struct Memo
{

	Treasurea record;
	bool flag;

	Memo()
	{

		flag = false;

	}

};

Memo memo[1001][1001];

Treasurea knapsack(vector<Treasurea> &treasurea, int correntWeight, int correntValue, int step)
{

	if(treasurea.size() == step)
	{

		Treasurea temp;
		temp.value = correntValue;
		temp.weight = W - correntWeight;
		return temp;

	}
	else if(memo[correntWeight][step].flag) return memo[correntWeight][step].record;


	Treasurea total;

	for(int i = 0; i < N; ++i)
	{

		if(treasurea[i].flag) continue;
			treasurea[i].flag = true;
		
		Treasurea temp;

		if(correntWeight - treasurea[i].weight >= 0)  temp = knapsack(treasurea,correntWeight - treasurea[i].weight, correntValue + treasurea[i].value, step + 1);
		else
		{
			temp.value = correntValue;
			temp.weight = W - correntWeight;
		}

		if( (total.value < temp.value) || (total.value == temp.value && total.weight > temp.weight))	total = temp;

		treasurea[i].flag = false;

	}

	memo[correntWeight][step].record = total; 
	memo[correntWeight][step].flag = true;

	return total;

}

int main()
{

	int case_count = 0;
	
	while(cin >> W && W != 0)
	{
		
		cin >> N;

		vector<Treasurea> treasurea(N);
		Treasurea result;

		for(int i = 0; i < N; ++i)	scanf("%d,%d",&treasurea[i].value,&treasurea[i].weight);			

		result = knapsack(treasurea,W,0,0);

		memset(memo,0,sizeof(memo));

		cout << "Case " << ++case_count << ":" << endl << result.value << endl << result.weight << endl;

		treasurea.clear();

	}

}

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

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

int main(){
  int W,N;
  int dp[1001];
  int num = 1;
  while(scanf("%d",&W),W){
    scanf("%d",&N);
    memset(dp,0,sizeof(dp));
    
    for(int i=0;i<N;i++){
      int v,w;
      scanf("%d,%d",&v,&w);
      for(int j=W;j>=0;j--){
	if(j+w <= W)dp[j+w] = max(dp[j+w], dp[j]+v);
      }
    }
    cout << "Case " << num << ":\n"; 
    cout << dp[W] << endl << (lower_bound(dp,dp+W+1,dp[W])-dp) << endl;
  }
}