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 <cstdio>

#define MAX(A, B) (A) > (B) ? (A) : (B)

using namespace std;

int dp[1001][1001];
int w[1000], v[1000];

int main() {
	int N, W;
	int c = 0;
	memset(dp, 0, sizeof dp);
	while (cin >> W, W) {
		cin >> N;
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &v[i], &w[i]);
		}
		for (int i = 0; i < N; i++) {
			for (int j = W; j >= 0; j--) {
				if (j >= w[i])
					dp[i+1][j] = MAX(dp[i][j], dp[i][j-w[i]]+v[i]);
				else
					dp[i+1][j] = dp[i][j];
			}
		}
		while (dp[N][W] == dp[N][W - 1]) W--;
		cout << "Case " << ++c << ':' << endl;
		cout << dp[N][W] << endl << W << endl;;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
int main(){
    int w, n;
    int ct = 1;
    while(cin >> w >> n, w){
    int va[n], we[n];
    for(int i = 0; i < n; i++)  scanf("%d, %d",&va[i], &we[i]);
    int dp[1010][1010];
    for(int i = 0; i < 1010; i++)
        for(int j = 0; j < 1010; j++)
            dp[i][j] = 0;
    for(int i = 0; i <= n; i++)
        for(int j = 0; j <= w; j++){
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            if(j + we[i] <= w)
                dp[i + 1][j + we[i]] = max(dp[i + 1][j + we[i]], dp[i][j] + va[i]);
        }
    int macs = 0, sumwe = 1010;
    for(int i = 0; i <= n; i++)
        for(int j = 0; j <= w; j++)
            if(dp[i][j] == dp[n][w])
                sumwe = min(j, sumwe);
    cout << "Case " << ct << ":" << endl << dp[n][w] << endl << sumwe << endl;
    ct++;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define pb push_back
#define MAX 100005
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;



signed main(){
  int W,N;
  int c=1;
  while(cin>>W>>N,W){
    vector<int> v(N),w(N);
    char x;
    vector<vector<int> > dp(N+1,vector<int>(W+1));
    rep(i,N)cin>>v[i]>>x>>w[i];
    loop(i,1,N+1){
      rep(j,W+1){
        if(j-w[i-1]>=0){
          dp[i][j]=max(dp[i-1][j-w[i-1]]+v[i-1],dp[i][j]);
        }else{
          dp[i][j]=dp[i-1][j];
        }
      }
    }
    int val=dp[N][W],wei;
    rep(i,W){
      if(dp[N][i]==val)wei=i;
    }
    cout<<"Case "<<c<<':'<<endl;
    cout<<val<<endl;
    cout<<wei<<endl;
    c++;
  }

  return 0;
}

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

int pri[1010],wai[1010],dp[2][1010];
int main(){
	int W,N,cas=0;
	while(true){
		cas++;
		cin>>W;
		if(W==0) break;
		cin>>N;
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&pri[i],&wai[i]);
		}
		fill(dp[0],dp[0]+W+1,0);
		fill(dp[1],dp[1]+W+1,0);
		int in=0,ou=1;
		for(int i=1;i<=N;i++){
			for(int j=0;j<=W;j++) dp[ou][j]=dp[in][j];
			for(int j=0;j<=W;j++){
				if(j+wai[i]<=W){
					dp[ou][j+wai[i]]=max(dp[ou][j+wai[i]],dp[in][j]+pri[i]);
				}
			}
			swap(in,ou);
		}
		int res=0,cnt=0;
		for(int i=0;i<=W;i++){
			if(res<dp[in][i]){
				res=dp[in][i];
				cnt=i;
			}
		}
		cout<<"Case "<<cas<<":"<<endl<<res<<endl<<cnt<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>

using namespace std;

int main(void){
  int W,n,v[1000],w[1000],dp[1000][1000],t=1;

  while(cin >> W,W){
    cin >> n;

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

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

    cout << "Case " << t << ":" << endl;
    cout << dp[n][W] << endl;

    int m=10000000;
    for(int j=0;j<=W;j++)
      for(int i=0;i<=n;i++)
	if(dp[n][W]==dp[i][j]){
	  m=min(m,j);
	}
    cout << m << endl;
    t++;
  }

  return 0;
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

#define INF 1000000000
#define MOD 1000000007
#define EPS 1E-12

const int MAX_N = 1000;
const int MAX_W = 10000;
int dp[MAX_N+1][MAX_W+1];
int v[MAX_N];
int w[MAX_N];
int W, N;
int c;
int cnt;

int main()
{
  while((cin >> W) && W){
    cin >> N;
    rep(i, N) scanf("%d,%d", &v[i], &w[i]);

    rep(i, N) erep(j, 0, W) dp[i][j] = 0;
    rev(i, N) erep(j, 0, W){
      if(j < w[i]) dp[i][j] = dp[i+1][j];
      else dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]]+v[i]);
    }

    cout << "Case " << ++cnt << ":" << endl;
    cout << dp[0][W] << endl;
    rev(i, W) if(dp[0][i+1] > dp[0][i]){
      cout << i + 1 << endl;
      break;
    }
  }

  return 0;
}

C++: // 重さの総和W以下, 同率なら軽い方を出力

#include <iostream>
#include <cstring>

using namespace std;

const int N_MAX=1000;
const int W_MAX=10000;
int value[N_MAX], weight[N_MAX];

int memoW[W_MAX];
int NUM;
int VTOTAL=-1, WTOTAL=9999999;
int W;

int max(int a, int b);
int maximumValue(int v, int w, int n); // v:現在の価値, w:現重量 n番目の要素に対して

int main() {
  int caseN=0;
  int flag=0;
  while(flag==0) {
    VTOTAL=-1;
    WTOTAL=9999999;
    cin>>W;
    if(W==0) {
      flag=1;
      break;
    }
    cin>>NUM;
  caseN++;
    for(int i=0; i<NUM; i++) {
      cin>>value[i]; // 価値
      cin.ignore(); // 1文字読み飛ばし(",")
      cin>>weight[i]; // 重さ
    }
    memset(memoW, -1, sizeof(memoW));
    cout<<"Case "<<caseN<<":"<<endl;
    cout<<maximumValue(0, 0, 0)<<endl;
    cout<<WTOTAL<<endl;
  }
}

int max(int a, int b) {
  if(a>b) {
    return a;
  }
  else {
    return b;
  }
}

int maximumValue(int v, int w, int n) {
  if(n==NUM) { // 最深部
    if(v>VTOTAL) {
      VTOTAL=v;
      WTOTAL=w;
    }
    if(v==VTOTAL && w<WTOTAL) {
      WTOTAL=w;
    }
    return v;
  }
  else if(w+weight[n]<=W) {
    return max(maximumValue(v, w, n+1), maximumValue(v+value[n], w+weight[n], n+1));
  }
  else {
    return maximumValue(v, w, n+1);
  }
}