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<cstdio>
#include<cstring>
#include<algorithm>
#include<deque>
using namespace std;
typedef pair<int,int> P;

int W,N,v[1000],w[1000];
int I;

P dp[1001][1001];
bool done[1001][1001];
P solve(int i,int r){
    if(done[i][r]) return dp[i][r];
    P result;
    if(i==N) result=P(0,r);
    else {
        if(r-w[i]>=0){
            P p=solve(i+1,r-w[i]);
            p.first+=v[i];
            result=max(p,solve(i+1,r));
        }else{
            result=solve(i+1,r);
        }
    }
    done[i][r]=true;
    return dp[i][r]=result;
}

int main(){
    while(I++,scanf("%d%d",&W,&N),W){
        for(int i=0;i<N;i++)scanf("%d,%d",v+i,w+i);
        memset(done,0,sizeof(done));
        P p=solve(0,W);
        printf("Case %d:\n%d\n%d\n",I,p.first,W-p.second);
    }
}

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

int parse(string a){
int s=0;
for(char b:a){
	s*=10;
	s+=b-48;
}
return s;
}
vector<int>split(string a){
	vector<int>b;
	string c="";
	for(char d:a){
		if(d==','){b.push_back(parse(c));c="";}
		else c+=d;
	}
	b.push_back(parse(c));
	return b;
}
int main() {
	int u=1;
 int a,b;
 while(cin>>b,b){
 	cin>>a;
 vector<vector<int>>dp(a+1,vector<int>(b+1));
 for(int c=0;c<a;c++){
 	string q;
 	cin>>q;
 	int d,e;
 	d=split(q)[0];
 	e=split(q)[1];
 	for(int f=0;f<b+1;f++){
 		if(f<e)dp[c+1][f]=dp[c][f];
 		else dp[c+1][f]=max(dp[c][f],dp[c][f-e]+d);
 	}
 }
 cout<<"Case "<<u<<":"<<endl;
 int y=0,z=0;
 for(int x=0;x<b+1;x++){
 	if(dp[a][x]>y){y=dp[a][x];z=x;}
 }
 cout<<y<<endl<<z<<endl;
 u++;
 }
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#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()
  {
    record.value = 0;
    record.weight = 0;
    record.flag = false;
    flag = false;
  }

};

Memo memo[1001][1001];

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

  if(correntWeight < 0)
    {
      Treasurea temp;
      temp.weight = W - (correntWeight + treasurea[kind - 1].weight);
      temp.value = correntValue - treasurea[kind - 1].value;
      return temp;
    }
  else if(kind == treasurea.size())
    {
      Treasurea temp;
      temp.weight = W - correntWeight;
      temp.value = correntValue;
      return temp;
    }
  else if(memo[correntWeight][kind].flag) return memo[correntWeight][kind].record;

  Treasurea total;

  for(int i = 0;i < treasurea.size(); ++i)
    {

      if(treasurea[i].flag) continue;

      treasurea[i].flag = true;

      Treasurea temp = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

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

      treasurea[i].flag = false;

    }

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

  return total;

}

int main()
{

  int case_number = 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);

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

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

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

    }

  return 0;

}

C++: #include <iostream>
#include <string.h>
#include <vector>
#include <cstdio>

#define W 1010
#define N 1010

using namespace std;

int main(void)
{
	int w,n;
	int i1,i2;
	int dp[N][W];
	int v[N];
	int ww[N];
	int c=0;
	int maxx;
	int i;

	while(1)
	{
		cin>>w;
		if(w==0) break;
		cin>>n;

		for(i1=0;i1<n;i1++){
			scanf("%d,%d",v+i1,ww+i1);
		}
		memset(dp,0,sizeof(dp));

		for(i1=n-1;i1>=0;i1--){
			for(i2=0;i2<=w;i2++){

				if(ww[i1]>i2) dp[i1][i2]=dp[i1+1][i2];
				else {
					dp[i1][i2]=max(dp[i1+1][i2],dp[i1+1][i2-ww[i1]]+v[i1]);
				}
			}
		}
		c++;
		cout<<"Case "<<c<<":"<<endl;
		cout<<dp[0][w]<<endl;
		
		maxx=dp[0][0];
		for(i1=0;i1<=w;i1++){
			if(maxx<dp[0][i1]) {
				i=i1;
				maxx=dp[0][i1];
			}
		}

		cout<<i<<endl;
	}

	return 0;
}

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;
    if (N * W >= 1e5) exit(0);
    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;
    }
  }
}