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<string>
int dp[1000][1000];
using namespace std;
int main(){
	int t,n;
	int w[1000],v[1000];
	int k=0;
	char z;
	while(true){
	cin>>t;
	if(t==0)
		break;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<t;j++){
			dp[i][j]=-1;
		}
	}
	for(int i=0;i<n;i++){
		cin>>v[i]>>z>>w[i];
		dp[i][0]=0;
	}
	dp[0][w[0]]=v[0];
	for(int i=1;i<n;i++){
		for(int j=1;j<t;j++){
			if(j-w[i]<0){
				if(dp[i-1][j]==-1)
					dp[i][j]=-1;
				if(dp[i-1][j]!=-1)
					dp[i][j]=dp[i-1][j];
			}
			if(j-w[i]>=0){
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]==-1){
					if(dp[i-1][j]>=dp[i-1][j-w[i]]+v[i])
						dp[i][j]=dp[i-1][j];
					else
						dp[i][j]=dp[i-1][j-w[i]]+v[i];
				}
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]!=-1)
					dp[i][j]=dp[i-1][j-w[i]]+v[i];
				if(dp[i-1][j]!=-1&&dp[i-1][j-w[i]]==-1)
					dp[i][j]=dp[i-1][j];
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]==-1)
					dp[i][j]=-1;
			}
		}
	}
	int c=0;
	for(int j=0;j<t;j++){
		if(dp[n-1][j]>c)
			c=dp[n-1][j];
	}
	k++;
	for(int j=0;j<t;j++){
		if(c==dp[n-1][j]){
			cout<<"Case "<<k<<":"<<endl;
			cout<<c<<endl;
			cout<<j<<endl;
		}
	}
	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define SIZE(x) ((int)(x).size())
#define UQ(v) v.erase(unique(v,begin(),v,end()),v.end());
#define INF 11922960 
#define pb push_back 
int dxy4[5] = { 0,1,0,-1,0 };
int dxy8[9] = { 0,1,1,0,-1,1,-1,-1,0 };
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)




//vector< vector<int> > v (max1, vector<int>(max2) );
int main() {
	int n, W, count = 0;
	while (1) {
		count++;
		cin >> W >> n;
		if (!W) break;
		vector<vector<int>>dp(n+1000, vector<int>(W+10, 0));
		vector<int> v(n + 1, 0);
		vector<int> w(n + 1, 0);


		 char s;
		rep(i, n) { cin >> v[i+1]>> s>>w[i+1]; }

		rep(i, (n+1)) {
			if (!i)continue;
			//cout << i << "-------" << endl;
			rep(j, W-w[i]+1) {
				dp[i][w[i] + j] = max({ dp[i - 1][w[i] + j], dp[i - 1][j] + v[i]});
				//cout << dp[i][w[i]+j] << " ";
			}
			cout << endl;
		}
		int umax = dp[n][W];
		rep(i, W + 1) {
			if (dp[n][i] == umax) {
				cout << "Case: " << count << endl<< umax << endl << i << endl;
				break;
			}
		}
	}
int jk;
cin >> jk;
	return 0;
}

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

int W,N,value[1000],weight[1000],cost;
int dp[1001][10000],data=0,w;
int main(){

  while(cin >>W){
    if(W==0)break;
    cin >>N;
    data++;
    for(int i=0;i<N;i++) scanf("%d,%d",&value[i],&weight[i]);
   
    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]);
       
	dp[i+1][j+weight[i]] = max(dp[i+1][j+weight[i]],dp[i][j]+value[i]);
      }
    }
    cost=dp[N][W];
    w=-1;
   
	for(int j=0;j<=W;j++){
	  if(dp[N][j] == cost){
	    w=j;
	    break;
	  }	  
	  //      cout <<weight[i]<<endl;
    }
      cout <<"Case "<<data<<":\n"<<cost<<endl<<w<<endl;
  }

  return 0;
}

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

int main(){
    int W,N;
    int C = 0;
    while(cin >> W >> N && W){
        int dp[1010] = {};
        for(int i=0;i<N;i++){
            int a,b;
            char c;
            cin >> a >> c >> b;
            for(int j=W;j>=b;j--) dp[j] = max(dp[j], dp[j-b]+a);
        }
        int x = 0, y = 0;
        for(int i=0;i<=W;i++) if(x < dp[i]) x = dp[i], y = i;
        cout << "Case " << ++C << ":\n" << x << "\n" << y << "\n";
    }
}