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 <cstring>
#include <algorithm>
#define MAX 1000
using namespace std;

int main(void){

  int n,W;
  int w[MAX+1];
  int v[MAX+1];
  int dp[MAX+1][MAX+1];
  char ten;
  int cnt = 1;

  while( cin >> W , W ){
    cin >> n;
    for( int i = 0 ; i < n ; i++ ){
      cin >> v[i] >> ten >> w[i];
    }

    for( int i = 0 ; i < MAX ; i++ ){
      for( int j = 0 ; j < MAX ; 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 " << cnt++ << ":" << endl;
    for( int i = W ; i >= 0 ; i-- ){
      if( dp[n][i] != dp[n][W] ){
	cout << dp[n][W] << endl << i+1 << endl;
	break;
      }
    }
  }  
  return 0;
}

C++: #include<iostream>
#include<memory.h>
#include<algorithm>
using namespace std;
int dp[1001][1001];
int main()
{
	int W, n, c = 0;
	int w, v;
	
	while (1){
		cin >> W;
		if (W == 0){ break; }
		c++;
		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> v >> w;
			for (int i2 = 0; i2 <= W; i2++){
				if (i2 < w)
					dp[i][i2] = dp[i - 1][i2];
				else
					dp[i][i2] = max(dp[i - 1][i2], dp[i - 1][i2 - w] + v);
			}
		}
		int maxv = 0, maxw;
		for (int i = 0; i <= W; i++){
			if (maxv < dp[n][i]){
				maxv = dp[n][i];
				maxw = i;
			}
		}
		cout << "Case" << c << ":" << endl;
		cout << maxv << endl << maxw << endl;
	}
	
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <complex>
#include <string>
#include <sstream>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>

using namespace std;

#define all(c) c.begin(),c.end()
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

typedef long long ll;
typedef complex<double> point;
typedef vector<int> vi;

template<class T>
inline int len(const T& t){
    return t.size();
}

template<typename T>
ostream& operator<<(ostream& os,const vector<T>& val){
    os << "[ ";
    for(typename vector<T>::const_iterator it=val.begin();
        it != val.end();++it){
        os << *it << " ";
    }
    os << "]";
    return os;
}

struct Treasure{
    int value,weight;
    Treasure():
        value(0),weight(0) {};
    Treasure(int _value,int _weight)
        : value(_value),weight(_weight) {}
};

int main(){
    for(int loop_count=1;true;loop_count++){
        int W,N;
        cin >> W;
        if(W == 0) break;
        cin >> N;
        vector<Treasure> v(N);
        rep(i,N){
            char c;
            cin >> v[i].value >> c >> v[i].weight;
        }
        vi value(W+1);
        rep(n,N){
            vi tmp(W+1);
            rep(i,W+1){
                tmp[i] = max(tmp[i],value[i]);
                int in = i+v[n].weight;
                if(in <= W){
                    tmp[in] = max(tmp[in],value[i]+v[n].value);
                }
            }
            value.swap(tmp);
        }
        int retw=0;
        int retv=0;
        rep(i,len(value)){
            if(value[i] > retv){
                retv = value[i];
                retw = i;
            }
        }
        cout << "Case " << loop_count << ":" << endl;
        cout << retv << endl << retw << endl;
    }
    return 0;
}

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

using namespace std;

struct itm{
	int val;
	int wht;
};

int main()
{
	int w, n, dp[1001], s, t, c=1;
	itm it[1000];
	
	while(cin >> w && w){
		cout << "Case " << c++ << ":" << endl;
		fill(dp, dp+1001, 0);
		cin >> n;
		for(int i = 0; i < n; i++){
			scanf("%d,%d",&it[i].val,&it[i].wht);
		}
		
		for(int i = 0; i < n; i++){
			for(int j = w; j >= it[i].wht; j--){
				if(j >= it[i].wht){
					dp[j] = max(dp[j], dp[j-it[i].wht]+it[i].val);
				}
			}
		}
		s = t = 0;
		for(int i = 0; i <= w; i++){
			if(dp[i] > s){
				s = dp[i];
				t = i;
			}
		}
		
		cout << s << endl << t << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<vector>
#include<list>
#include<stdio.h>
#include<math.h>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

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

C++: #include <bits/stdc++.h>

#define ll long long
#define INF INT_MAX

using namespace std;

int main() {
    int ww;
    int index = 1;
    while(scanf("%d",&ww) && ww){
        int n;
        cin >> n;
        int v[n],w[n];
        int dp[n+1][2000];
        bool go[n+1][2000];
        memset(dp,0,sizeof(dp));
        memset(go,false,sizeof(go));
        for(int i = 0;i < n;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        for(int i = n-1;i >= 0;i--){
            for(int j = 0;j <= ww;j++){
                if(j < w[i]){
                    dp[i][j] = dp[i+1][j];
                    go[i][j] = false;
                }else{
                    if(dp[i+1][j] < dp[i+1][j-w[i]]+v[i]){
                        dp[i][j] = dp[i+1][j-w[i]]+v[i];
                        go[i][j] = true;
                    }else{
                        dp[i][j] = dp[i+1][j];
                        go[i][j] = false;
                    }
                }
            }
        }
        cout << "Case " << index++ << ":" << endl;
        cout << dp[0][ww] << endl;
        int sumweight = 0;
        for(int i = 0;i < n;i++){
            if(go[i][ww]){
                sumweight += w[i];
                ww -= w[i];
            }
        }
        cout << sumweight << endl;
 
        // for(int j = 0;j <= n;j++){
        //     for(int i = 0;i <= ww ;i++){
        //         printf("%3d ",dp[j][i]);
        //     }
        //     cout << endl;
        // }
    }
	return 0;
}