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 <vector>
#include <map>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;

#define rep(i,n) for((i)=0;(i)<(int)n;(i)++)
#define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)

int W, N, v[1002], w[1002];
int dp[1002][1002];

int main() {
  int i, j, cases = 1, ans;
  char c;
  while (scanf("%d",&W),W) {
    scanf("%d",&N);
    rep(i,N) scanf("%d%c%d",&v[i],&c,&w[i]);
    memset(dp, -1, sizeof dp);

    for (i=0;i<=W;i++) dp[0][i] = 0;

    for (i=0;i<N;i++) {
      for (j=0;j<=W;j++) {
        if (j >= w[i]) {
          dp[i+1][j] = max(dp[i][j], v[i]+dp[i][j-w[i]]);
        } else {
          dp[i+1][j] = dp[i][j];
        }
      }
    }
    
    ans = W;
    for (i=0;i<=W;i++) if (dp[N][W] == dp[N][i]) {
      ans = i; break;
    }
    
    printf("Case %d:\n%d\n%d\n", cases, dp[N][ans], ans);
    cases++;
  }
  return 0;
}

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

int gcd(int a, int b){
    if(a<b) swap(a, b);
    if(b==0) return a;
    return gcd(b, a%b);
}

int W, N, cnt=1;
const int MAX_W = 1000;
const int MAX_N = 1000;
int dp[MAX_N+1][MAX_W+1];
int w[MAX_N]; int v[MAX_N];
int main() {
    while(cin >> W, W){
        cin >> N;
        for(int i=0; i<N; ++i){
            scanf("%d,%d", &v[i], &w[i]);
        }
        int sum_w=0;
        dp[0][0] = 0;
        for(int i=N-1; i>=0; --i){
            for(int j=0; j<=W; ++j){
               if(j<w[i]){
                   dp[i][j] = dp[i][j];
               }else{
                   dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]]+v[i]);
               }
            }
        }
        for(int i=N-1; i>=0; --i){
            for(int j=0; j<=W; ++j){
                if(dp[i][j]==dp[0][W]){
                    sum_w = j;
                    break;
                }
            }
            if(sum_w) break;
        }
        cout << "Case " << cnt << ":" << endl;
        cout << dp[0][W] << endl;
        cout << sum_w << endl;
        cnt++;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

int MAX = 1000*1000;

void solve(int n, int W, vector<int> v, vector<int> w) {
	int **dp = new int*[n];
	for (int i=0; i<n; i++) {
		dp[i] = new int[MAX+2];
		for (int j=0; j<=MAX+1; j++)
			dp[i][j] = 0;
	}

	int ans = 0;

	dp[0][w[0]] = v[0];
	for (int i=1; i<n; i++) {
		for (int j = 0; j+w[i]<=W; j++) {
			dp[i][j] = max(dp[i][j], dp[i-1][j]);
			dp[i][j+w[i]] = max(dp[i-1][j+w[i]], dp[i-1][j]+v[i]);
		}
	}

	int val = 0, weight = 0;
	for (int j=0; j<=W; j++) {
		if (val < dp[n-1][j]) {
			val = dp[n-1][j];
			weight = j;
		}
	}

	cout << val << endl << weight << endl;

	for (int i=0; i<n; i++)
		delete [] dp[i];
	delete [] dp;
}

int main() {
	

	int n, W;
	int ver = 1;

	cin >> W;
	while (W!=0) {
		cin >> n;

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

		cout << "Case " << ver << ":" << endl;
		solve(n, W, v, w);

		cin >> W;
		ver++;
	}
}

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 = vector<Edge>;

class Graph {
  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);
      }
    }
    assert(N * W <= 1e6);
    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++: Case 1:
220
49
Case 2:
220
49

C++: #include <math.h>
#include <utility>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int main(){

	int n,w;

	for ( int num = 1;; num++ ) {

		cin >> w;
		if ( w == 0 ) break;
		cin >> n;

		int input1,input2;
		char input3;
		vector<int> value;
		vector<int> weight;
		for ( int i = 0; i < n; i++ ) {
			cin >> input1 >> input3 >> input2;
			value.push_back(input1);
			weight.push_back(input2);
		}

		vector<int> check;
		check.push_back(0);
		for ( int i = 0; i < w; i++ ) {
			check.push_back(-1);
		}
		int value_sum = 0;
		int weight_sum = w+1;

		for ( int i = 0; i < n; i++ ) {
			for ( int j = w - weight[i]; j >= 0; j-- ) {
				if ( check[j] != -1 ) {
					check[ j+weight[i] ] = max( check[ j+weight[i] ], check[j] + value[i] );
					if ( check[ j+weight[i] ] > value_sum ) {
						value_sum = check[ j+weight[i] ];
						weight_sum = j+weight[i];
					}else if ( check[ j+weight[i] ] == value_sum ) {
						weight_sum = min( weight_sum, j+weight[i] );
					}
				}
			}
		}
		cout << "Case " << num << ":" << endl << value_sum << endl << weight_sum << endl;
	}
	return 0;
}