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>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MAX_N 5001
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<string, ll> PS;
typedef vector<ll> V;

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;


int co = 1;
char k;
int W, N, v[10000], w[10000];

ll dp[1010][10000];//i???????????§???????????§?????????j??\?????¨?????????????????????????????§???

int main(){
	while (cin >> W&&W){
		cin >> N;
		REP(i, N)cin >> v[i] >> k >> w[i];
		REP(i, N){
			REP(j, W){
				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];
				}
			}
		}
		int max = -1, ans = -1;
		REP(i, W + 1){
			if (max < dp[N][i]){
				max = dp[N][i];
				ans = i;
			}
		}
		printf("Case %d:\n", co++);
		cout << max << endl;
		cout << ans << endl;
		REP(i, N + 1)REP(j, W)dp[i][j] = 0;
	}
}

C++: #include<stdio.h>
int dp[1001][1001];
int main(void)
{
	int i,j,k,l;
	int w,n;
	int max=0;
	int a[1001],b[1001];
	
	scanf("%d",&w);
	l=1;
	while(w!=0){
		scanf("%d",&n);
		for(i=0;i<=n;i++){
			for(j=0;j<=1001;j++){
				dp[i][j]=-1;
			}
		}
		for(i=0;i<n;i++){
			scanf("%d,%d",&a[i],&b[i]);
		}
		for(i=0;i<=n;i++){
			dp[i][0]=0;	
			for(j=0;j<=w;j++){
				if(dp[i][j]!=-1){
					if(dp[i][j]+a[i]>=dp[i+1][j+b[i]]){
						dp[i+1][j+b[i]]=dp[i][j]+a[i];
					}
					if(dp[i+1][j]<dp[i][j]){
						dp[i+1][j]=dp[i][j];
					}
				}
			}
		}
		for(j=0;j<=w;j++){
			if(dp[n][j]>max){
				max=dp[n][j];
				k=j;
			}
		}
		printf("Case %d:\n",l);
		printf("%d\n%d\n",max,k);
		scanf("%d",&w);
		l++;
	}
	return 0;
}

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

pair<int, int> hoge(vector< pair<int, int> > &c, int i, int w, int aa, int bb)
{
    if (i  == int(c.size()) || w <= aa) {
        return make_pair(aa, bb);
    }

    pair<int, int> c1, c2;
    c1 = hoge(c, i+1, w, aa + c[i].first, bb + c[i].second);
    c2 = hoge(c, i+1, w, aa, bb);
    return (w <= c1.first)? c2 : (c1.second < c2.second)? c2 : c1;
}

int main(int argc, char const* argv[])
{
    int ww, nn;
    for(int i = 0;; ++i) {
        vector< pair<int, int> > container;
        cin >> ww;
        if (ww == 0)
            break;
        cin >> nn;
        for (int i = 0; i < nn; ++i) {
            int v, w;
            char c;
            cin >> v >> c >> w;
            container.push_back(make_pair(w, v));
        }
        pair<int, int> r = hoge(container, 0, ww, 0, 0);
        cout << "Case " << i+1 << ":" << endl;
        cout << r.second << endl;
        cout << r.first  << endl;
    }
    
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define INF (1<<21)
int dp[1001][1001];
pair<int,int> solve(vector<int> value , vector<int> weight , int W , int N){
	for(int i=0;i<1001;i++)
		for(int j=0;j<1001;j++)
			dp[i][j] = 0;
	for(int num=1;num<=N;num++){
		for(int w=0;w <= W;w++){
			if(w < weight[num]){
				dp[num][w] = max(dp[num][w],dp[num-1][w]);
			}else{
				dp[num][w] = max( dp[num][w] , dp[num-1][w-weight[num]] + value[num]);
			}
		}
	}
	int ans1 = *max_element(dp[N],dp[N]+W+1);
	int ans2 = find(dp[N],dp[N]+W+1,ans1)-dp[N];
	return make_pair(ans1,ans2);
}

int main(){
	int n,w,CaseNum=0;
	while(cin >> w && w){
		cout << "Case " << ++CaseNum << ":" << endl;
		cin >> n;
		vector<int> value(1+n) , weight(1+n);
		for(int i=1;i<=n;i++){
			scanf("%d,%d\n",&value[i],&weight[i]);
		}
		pair<int,int> ans = solve(value,weight,w,n);
		cout << ans.first << endl << ans.second << endl;
	}
}

C++: //http:judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0042

#include <iostream>
#include <fstream>

#define fin cin

using namespace std;
 
int main(void){

    //ifstream fin("input2.txt");

    int Case = 1;
    int maxWeight;
 
    //??????????????????
    while(fin >> maxWeight, maxWeight) {

        int n, sumWeight = 0;
        int w[1000], v[1000];
        int dp[1001] = {0};
        fin >> n;
        for(int i = 0; i < n; i++){

            //value
            string str_in;
            getline(fin, str_in, ',');
            v[i] = stoi(str_in);

            //weight
            getline(fin, str_in);
            w[i] = stoi(str_in);

        }

        //?????????
        for(int i = 1; i <= maxWeight; i++)
            dp[i] = -10;
            
        //DynamicProgramming! not memoka!!!
        for(int i = 0; i < n; i++) {
            for(int j = maxWeight - w[i]; j >= 0; j--) {

                int sub = j + w[i];
                if(0 <= dp[j] && sub <= maxWeight) {

                    dp[sub] = max( dp[j] + v[i], dp[sub] );

                }
            }
        }
 
        for(int i = 0; i <= maxWeight; i++)
            if(dp[i] > dp[sumWeight]) sumWeight = i;
 
        cout << "Case " << Case++ << ":" << endl;
        cout << dp[sumWeight] << endl;
        cout << sumWeight << endl;
    }
 
    return 0;
}