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>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w,n;
  int cnt = 1;
  char c;
  while(cin >> w){
    if(w == 0) break;
    cin >> n;
    vary(int,vs,n,0);
    vary(int,ws,n,0);
    vector<vector<pii>> dp(n+1,vector<pii>(w+1,make_pair(0,0)));
//     vector<vector<int>> dp2(n+1,vector<int>(w+1,0));
    rep(i,n){
      cin >> vs[i] >> c >> ws[i];
    }
    REP(i,0,n){
      rep(j,w){
        dp[i+1][j] = max(dp[i][j], make_pair((j-ws[i] >= 0)? dp[i][j-ws[i]].fi + vs[i] : 0,j));
      }
    }
    cout << "Case " + to_string(cnt) + ":"<<endl;
    rep(i,w+1){
      if(dp[n][i] == dp[n][w-1]){
        cout << dp[n][w-1].fi<<endl<<i<<endl;
        break;
      }
    }
    ++cnt;
  }
  return 0;
}

C++: 
#include <iostream>
#include <algorithm>
#include <vector>

#define min(a,b) {a > b ? b:a}
#define max(a,b) {a > b ? a:b}
using namespace std;

#define MAX_W 1000
#define MAX_N 1000

int dp[MAX_W*10+1];
void q42() {
	int W, N;
	int cnt = 1;
	for (; cin >> W;) {
		if (!W) break;

		cin >> N;
		vector<pair<int, int> > Item;
		for (int i = 0; i < N; i++) {
			int value, weight;
			char tmp;
			cin >> value >> tmp >> weight;
			Item.push_back(make_pair(value, weight));
		}

		for (int i = 0; i <= W; i++) {
			dp[i] = -1;
		}
		dp[0] = 0;

		for (int i = 0; i < N; i++) {
			int value = Item[i].first;
			int weight = Item[i].second;
			for (int j = W; j >= 0; j--) {
				if (j - weight < 0) continue;
				if (dp[j - weight] == -1) continue;
				dp[j] = max(dp[j], dp[j - weight] + value);
			}
		}

		int ans_value = 0, ans_weight = 0;
		for (int i = 0; i <= W; i++) {
			cout << dp[i] << endl;
			if (ans_value < dp[i]) {
				ans_value = dp[i];
				ans_weight = i;
			}
		}

		cout << "Case " << cnt++ << ":" <<"\n";
		cout << ans_value << "\n";
		cout << ans_weight << "\n";
	}
}

int main() {
	q42();
	return 0;
}

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

using namespace std;

int W, N;
int *v, *w;
int dp[1001][1001];

int main() {
	int no = 1;
	while (1) {
		scanf("%d", &W);
		if (W == 0)
			break;
		scanf("%d", &N);
		v = new int[N];
		w = new int[N];
		for (int i = 0; i < N; i++)
			scanf("%d,%d", &v[i], &w[i]);

		memset(dp, 0, sizeof(dp));
		for (int i = N - 1; i >= 0; i--) {
			for (int j = 0; j <= W; j++) {
				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]);
			}
		}
		printf("Case %d:\n", no++);
		printf("%d\n", dp[0][W]);
		for (int j = 0; j <= W; j++) {
			if (dp[0][j] == dp[0][W]) {
				printf("%d\n", j);
				break;
			}
		}

		delete[] v;
		delete[] w;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<deque>
#include<algorithm>
#include<functional>
using namespace std;
int n,w;
pair<int,int> tre[1005];

int dp[1005][1005];//現在調べてる宝ID,重さの総和
int solve(){

  for(int i=0;i<n;i++){
    for(int j=0;j<=w;j++){
      if(j<tre[i].second) dp[i+1][j]=dp[i][j];
      else dp[i+1][j]= max(dp[i][j],dp[i][j-tre[i].second] + tre[i].first);
    }
  }

  int temp=dp[n][w];
  int ret=w;
  for(int i=w;i>0;i--){
    if(temp <= dp[n][i]) ret=i;
    else break;
  }
  return ret;

}

main(){
  int cas=1;
  while(1){
    cin >> w;
    if(!w) break;
    cin >> n;
    for(int i=0;i<n;i++) scanf("%d,%d",&tre[i].first,&tre[i].second);
    int res=solve();
    printf("Case %d:\n",cas);
    printf("%d\n%d\n",dp[n][w],res);
    cas++;
  }

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>

#define rep(a, b, c) for (int a = b; a < (c); a++)
#define repe(a, b, c) for (int a = b; a <= (c); a++)
#define repr(a, b, c) for (int a = b; a > (c); a--)
#define repre(a, b, c) for (int a = b; a >= (c); a--)

using namespace std;

int main() {
    for (int c = 1; ; c++) {
        int W, N;
        cin >> W;
        if (W == 0) break;
        cin >> N;
        vector< vector<int> > dp(N + 1, vector<int>(W + 1, 0));
        int v, w;
        char ch;
        rep(i, 0, N) {
            cin >> v >> ch >> w;
            repe(j, 0, W - w) {
                dp[i + 1][j + w] = max(dp[i][j + w], dp[i][j] + v);
            }
        }
        int ans_w = 0, ans_v = 0;
        repre(i, W, 0) {
            if (ans_v <= dp[N][i]) {
                ans_w = i;
                ans_v = dp[N][i];
            }
        }
        cout << "Case " << c << ":" << endl;
        cout << ans_v << endl;
        cout << ans_w << endl;
    }
    return 0;
}