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<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1000;
const int MAXW = 10000;

int val[MAXN],weight[MAXN];
int dp[MAXN][MAXW];
// dp[i][w] .. i番目までを重さwで行った時の最大の価値
// dp[i+1][w] =
//   max(dp[i][w-weight[i+1]] + val[i+1] , dp[i][w])
int main(){
  int N,W,casenum = 1;
  while(scanf("%d",&W) && W != 0){
    //input
    fill(*dp,*(dp+MAXN),0);
    scanf("%d",&N);
    for(int i = 0;i < N;i++){
      scanf("%d,%d",val+i,weight+i);
    }
    ///
    
    ///solve
    for(int i = 0; i < N;i++){
      for(int w = 0;w < W+1;w++){
        int use = (w - weight[i] >= 0
                   ? dp[i][w - weight[i]] + val[i]
                   : 0);
        int unuse = dp[i][w];
        dp[i+1][w] = max(use,unuse);
      }
    }
    
    //
    printf("Case %d:\n",casenum);
    for(int w = 0;w <= W;w++){
      if(dp[N][w] == dp[N][W]){
        printf("%d\n%d\n",dp[N][w],w);
        break;
      }
    }
    casenum++;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;
#define rep2(x,from,to) for(int x = (from); x < (to); ++(x))
#define rep(x,to) rep2(x,0,to)
#define MAX_N 1000
#define MAX_W 1000

// dp[i][j]???i????????\?????????????????????????????????j??\?????????????????????????????¨??????????????????????????§????????¨??????
int dp[MAX_N + 1][MAX_W + 1];

int main() {
	int W;
	int count = 1;
	while(cin >> W && W) {
		int n;
		cin >> n;
		int v[MAX_N], w[MAX_N];
		rep(i,n) {
			string s;
			cin >> s;
			string t;
			auto it = s.begin();
			for(; *it != ','; ++it) {
				t += *it;
			}
			v[i] = atoi(t.c_str());
			t = "";
			++it;
			for(; it != s.end(); ++it) {
				t += *it;
			}
			w[i] = atoi(t.c_str());
		}
		rep(i, MAX_N + 1) {
			rep(j, MAX_N + 1) {
				dp[i][j] = 0;
			}
		}
		rep2(i,1,n+1) {
			rep(j,W+1) {
				if(j < w[i - 1]) dp[i][j] = dp[i - 1][j];
				else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
			}
		}
		int ret = W;
		while(1) {
			if(dp[n][ret] != dp[n][ret - 1]) break;
			ret--;
		}
		cout << "Case " << count << ":" << endl;
		cout << dp[n][W] << endl;
		cout << ret << endl;
		count++;
	}
	return 0;
}

C++: #include<cstdio>
int main(void){
	int ww;
	int n;
	int a[1001][1001];
	int w[1001];
	int p[1001];
	int max;
	int mm;
	for (int tt=1;;tt++){
		scanf("%d", &ww);
		if (ww == 0)break;
		scanf("%d", &n);
		for (int i = 0; i<1001; i++){
for(int j=0;j<1001;j++){
a[i][j]=0;
}
}
		for (int i = 1; i <= n; i++){
			scanf("%d,%d", &p[i], &w[i]);
for(int j=0;j<w[i];j++){
a[i][j]=a[i-1][j];
}
			for (int j = w[i]; j <= ww; j++){
				if (a[i - 1][j] > a[i - 1][j - w[i]] + p[i]){
					a[i][j] = a[i - 1][j];
				}
				else{
					a[i][j] = a[i - 1][j - w[i]] + p[i];
				}
			}
		}
		max = 0;
		mm = 0;
		for (int i = 0; i <= ww; i++){
			if (max < a[n][i]){
				max = a[n][i];
				mm = i;
			}
		}
		printf("Case %d:\n%d\n%d\n", tt, max, mm);
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;

int main(){
	int W,N,cou=0;
	int tr[1001],we[1001];
	while(cin >> W , W){
		cou++;
		cin >> N;
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		P dp[100][1001];
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				dp[i][j] = P(0,0);
			}
		}
		dp[0][0] = P(0,0);
		for(int i=1;i<=N;i++){
			dp[i][0] = P(0,0);
			for(int j=0;j<i;j++){
				if(dp[i-1][j].second+we[i] <= W){
					P p;
					p = P(dp[i-1][j].first+tr[i],dp[i-1][j].second+we[i]);
					if(p.first == dp[i-1][j].first){
						if(p.second < dp[i-1][j].second) dp[i][j+1] = p;
						else dp[i][j+1] = dp[i-1][j];
					}
					else if(p.first > dp[i-1][j].first){
						dp[i][j+1] = p;
					}
					else dp[i][j+1] = dp[i-1][j];
				}
				else dp[i][j+1] = dp[i-1][j];
			}
		}
		int ans1=0,ans2=0;
		for(int i=0;i<=N;i++){
			if(dp[N][i].first == ans1){
				if(dp[N][i].second < ans2) ans2 = dp[N][i].second;
			}
			else if(dp[N][i].first > ans1){
				ans1 = dp[N][i].first;
				ans2 = dp[N][i].second;
			}
		}

		cout << "Case " << cou << ":" << endl;
		cout << ans1 << endl;
		cout << ans2 << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
#include <cmath>
//#include <string>
//#include <sstream>
#include <iomanip>
//#include <complex>
#include <stdio.h>
//小数制度 	cout << fixed << setprecision(5) << tmp << endl;
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)

int dp[1010][1010];
int v[1010];
int w[1010];
int main(){
  int wei,n;
  int cnt = 1;
  while(cin >> wei,wei){
    cin >> n;
    REP(i,1010) REP(j,1010) dp[i][j] = 0;
    w[0]=0;v[0]=0;
    for(int i=1;i<n+1;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<=wei;j++){
        if(j-w[i]<0) dp[i+1][j] = dp[i][j];
        else  dp[i+1][j]=max(dp[i][j+1],dp[i][j-w[i]]+v[i]);
      }
    }
    /*for(int i=0;i<n+1;i++){
      for(int j=0;j<wei+1;j++){
        cout << dp[i][j] << " ";//l<<endl;
      }
      cout << endl;
    }*/
    int maxv=0;
    int ans=-1;
    for(int i=0;i<wei+1;i++){
      if(maxv < dp[n][i]){
        maxv = dp[n][i];
        ans = i;
      }
    }
    cout << "Case " << cnt << ":" <<endl;
    cout << maxv << endl << ans << endl;
    cnt++;
  }
}