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

C++: #include <iostream>
#include <climits>
using namespace std;
 
int main(void)
{
  int dp[1001][1001];  // [takara][weight]
  int wei[1001];       // [takara]
  int val[1001];       // [takara]
  int i, w;
  int n, tw;
  int datcnt;
  char c;
 
  cin >> tw;
  for(datcnt = 1; tw != 0; datcnt++){
    cin >> n;
    for(i = 1; i <= n; i++){
      cin >> val[i];
      cin >> c;
      cin >> wei[i];
    }
 
    // initArrayDP
    for(i = 0; i <= n; i++) {
      for(w = 0; w <= tw; w++) {
    dp[i][w] = 0;
      }
    }
    //cout << "--map";
    for(i = 1; i <= n; i++) {
      for(w = 1; w <= tw; w++) {
    int tem = w - wei[i];
    if(tem < 0) {
      dp[i][w] = dp[i-1][w];
    } else {
      dp[i][w] = max(dp[i-1][w], dp[i-1][tem] + val[i]);
    }
      }
    }
 
    //for(i = 1; i <= n; i++) {
    //  for(w = 1; w <= tw; w++) {
    //  printf("%5d", dp[i][w]);
    //  }
    //  printf("\n");
    //}
 
     
    // [takara]の最終列から価値最大かつ、そのなかで重さ最初を探す。
    int maxval  = INT_MIN;
    int lastwei = INT_MAX;
 
    for(w = 0; w <= tw; w++)
      maxval = max(dp[n][w], maxval);
 
    for(w = tw; w >= 0; w--)
      if(dp[n][w] == maxval) lastwei = min(lastwei, w);
 
    cout << "Case " << datcnt << ":" << endl;
    cout << maxval << endl << lastwei << endl;
     
    cin >> tw;
  }
  return 0;
}
//aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

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



int main(){
  int Case = 1;
  while(1){
    int w,n;
    int t[1001];
    int copy[1001];
    int takara[1000][2];
    for(int i=0;i<1001;i++){
      t[i]=0;
      copy[i]=0;
    }
                
    cin >> w;
    if ( w == 0)break;
    cin >> n;
    for(int i=0;i<n;i++){
      int kati,omosa;
      char conma;
      cin >> kati>>conma>>omosa;
      takara[i][0]=kati;
      takara[i][1]=omosa;
    }
                
    for(int i=1;i<=w;i++){
      if (i >= takara[0][1])copy[i] = takara[0][0]; 
    }
                
    for(int i=1;i<n;i++){
      for(int j=0;j<=w;j++){
        if (j <takara[i][1])t[j] = copy[j];
        else if (copy[j] < takara[i][0]+copy[j-takara[i][1]])t[j] = takara[i][0]+copy[j-takara[i][1]];
        else t[j] =copy[j]; 
      }
                
      for(int j=0;j<=w;j++){
        copy[j]=t[j];
      }
    }
                
    int max = t[w],maxweight = w;
    for(int i=w-1;i>=0 ;i--){
      //cout << t[i] <<" " << i << endl;                        
      if ( t[i] >= max ){
        max = t[i];
        maxweight = i;
      }
                
                
    }
                
    cout << "Case " << Case++ <<":" <<endl <<max <<  endl<< maxweight << endl;
                
                
                
                
  }


  return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;

int main(){
	int n,W;
	int v[1000],w[1000];
	for(int cnt = 0;;cnt++){
		cin >> W >> n;
		if(!W) break;
		int dp[1001][1001] = {},ma = 0,mi = 1e+9;
		for(int i = 0;i < n;i++){
			scanf("%d,%d",&v[i],&w[i]);
		}
		dp[0][0] = 0;
		for(int i = 1;i <= n;i++){
			for(int j = 0;j <= W;j++){
				if(j - w[i - 1] >= 0) dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - w[i - 1]] + v[i - 1]);
				else dp[i][j] = dp[i - 1][j];
				if(ma < dp[i][j]){
					ma = dp[i][j];
					mi = j;
				}else if(ma == dp[i][j]) mi = min(mi,j);
			}
		}
		printf("Case %d:\n%d\n%d\n",cnt + 1,ma,mi);
	}
	
	return 0;
}

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

int main()
{
  int W,N,v,w,i,j;
  int cnt=0,maxv,maxw;
  vector< vector<int> > dp;
  while(cin>>W>>N,W){
    vector< vector<int> > dp(N+2,vector<int>(W+1));
    for(i=1;i<=N;i++){
      scanf("%d,%d",&v,&w);
      for(j=0;j<=W;j++){
	if(j>=w && dp[i-1][j] < dp[i-1][j-w]+v)dp[i][j]=dp[i-1][j-w]+v;
	else dp[i][j]=dp[i-1][j];
      }
    }
    maxv=maxw=0;
    for(j=0;j<=W;j++){
      if(maxv<dp[N][j]){maxv=dp[N][j];maxw=j;}
    }
    printf("Case %d:\n%d\n%d\n",++cnt,maxv,maxw);
  }
}

C++: #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

int W,n,w[1001],v[1001];
int dp[1001];

int main(){
  int a = 0;
  while(cin >> W,W){
    cin >> n;
    for(int i=0;i<n;i++) scanf("%d,%d",&v[i],&w[i]);
    
    memset(dp,-1,sizeof(dp)); dp[0] = 0;
    for(int i = 0;i < n;i++){
      for(int j = W;j > 0;j--){
	if(j < w[i]) dp[j] = dp[j];
	else dp[j] = max(dp[j],dp[j - w[i]] + v[i]);
      }
    }
    
    int *maxi = max_element(dp,dp+W+1);
    cout << "Case " << ++a << ":" << endl;
    cout << *maxi << endl << maxi - dp << endl;
  }
}