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 <algorithm>
using namespace std;
int dp[1001][1001]={0};
int main()
{
    int c=0;
    while(1)
    {
        int W;
        cin>>W;if(!W)return 0;
        int n;
        cin>>n;
        int a[1001],w[1001];
        for(int i=1;i<=n;++i){
            cin>>a[i];
            cin.ignore();
            cin>>w[i];
        }
        for(int j=1;j<=n;++j){
            for(int i=0;i<=W;++i){
                if(i<w[j]){
                    dp[i][j]=dp[i][j-1];
                }else{
                    dp[i][j]=max(dp[i][j-1],dp[i-w[j]][j-1]+a[j]);
                }
            }
        }
        c++;
        cout<<"Case"<<" "<<c<<":"<<endl;
        cout<<dp[W][n]<<endl;
        bool flag=0;
        for(int j=1;j<=n;++j){
            for(int i=0;i<=W;++i){
                if(dp[i][j]==dp[W][n]){
                    cout<<i-1<<endl;
                    flag=1;
                    break;
                }
            }
            if(flag)break;
        }
    }
    return 0;
}

C++: #include<stdio.h>

int main()
{
	for(int c=1;;c++)
	{
		int w,n,kati[1000],weit[1000],katisum[1000][1000]={0},weitsum[1000][1000]={0},weitk=0,katik=0;
		scanf("%d",&w);
		if(w==0)
		{
			return 0;
		}
		scanf("%d",&n);
		
		for(int x=0;x<n;x++)
		{
			scanf("%d,%d",&kati[x],&weit[x]);
		}
		for(int y=0;y<n;y++)
		{
			for(int z=y;z<n;z++)
			{
				for(int r=y;r<=z;r++)
				{
					katisum[y][z]+=kati[r];
					weitsum[y][z]+=weit[r];
				}
			}
		}

		for(int y=0;y<n;y++)
		{
			for(int z=0;z<n;z++)
			{
				if(w>=weitsum[y][z]&&katik<katisum[y][z])
				{
					weitk=weitsum[y][z];
					katik=katisum[y][z];
				}
				
			}
		}
		for(int y=0;y<n;y++)
		{
			for(int z=0;z<n;z++)
			{
				if(katik==katisum[y][z]&&weitk>weitsum[y][z])
				{
					weitk=weitsum[y][z];
				}
			}
		}
		printf("Case %d\n%d\n%d\n",c,katik,weitk);
	}
}

C++: #include<stdio.h>
int i,j,l;
int w;
int n;
int kati,omosa;
long long int maxkati,maxomosa,casecnt=0;
long long int dp[1005][1005];//dp[お宝の総数][お宝の重さ]
int main(void){
	scanf("%d",&w);
	while(w!=0){
		for(i=0;i<=1000;i++){
			for(j=0;j<=1000;j++){
				dp[i][j]=0;
			}
		}
		scanf("%d",&n);
		scanf("%d,%d",&kati,&omosa);
		dp[0][omosa]=kati;
		for(j=1;j<=n;j++){
			dp[j][omosa]=dp[0][omosa];
		}
		for(i=1;i<n;i++){
			scanf("%d,%d",&kati,&omosa);
			for(j=i;j<=n;j++){
				if(dp[j][omosa]<kati){
					dp[j][omosa]=kati;
				}
			}
			for(j=1;j<=w;j++){
				if(dp[i-1][j]>0){
					if(dp[i][j+omosa]<dp[i-1][j]+kati){	
						dp[i][j+omosa]=dp[i-1][j]+kati;
					}
					for(l=i;l<=n;l++){
						if(dp[i-1][j+omosa]>dp[l+1][j+omosa]);
						else {
							dp[l][j+omosa]=dp[i][j+omosa];
						}
					}
				}
			}
			for(j=i;j<=w;j++){
				if(dp[i][j+omosa]>dp[j+1][j+omosa]){
					dp[j+1][j+omosa]=dp[i][j+omosa];
				}
			}
		}
		/*for(i=0;i<=n;i++){
			for(j=1;j<=w;j++){
				printf("%lld ",dp[i][j]);
			}
			printf("\n");
		}*/
		maxomosa=0;
		maxkati=0;
		for(j=0;j<=w;j++){
			if(maxkati==dp[n][j]);
			else if(maxkati<dp[n][j] ){
				maxkati=dp[n][j];
				maxomosa=j;
			}
		}
		casecnt++;
		printf("case %lld:\n",casecnt);
		printf("%lld\n",maxkati);
		printf("%lld\n",maxomosa);
		scanf("%d",&w);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

int MAX = 1000*1000;

void solve(int n, int W, vector<int> v, vector<int> w) {
	int **dp = new int*[n];
	for (int i=0; i<n; i++) {
		dp[i] = new int[MAX+2];
		for (int j=0; j<MAX+2; j++)
			dp[i][j] = 0;
	}

	dp[0][w[0]] = v[0];
	for (int i=1; i<n; i++) {
		for (int j = 0; j+w[i]<=W; j++) {
			dp[i][j] = max(dp[i][j], dp[i-1][j]);
			dp[i][j+w[i]] = max(dp[i][j+w[i]], dp[i-1][j]+v[i]);
		}
	}

	int val = -1, weight = 0;
	for (int j=0; j<=W; j++) {
		if (val < dp[n-1][j]) {
			val = dp[n-1][j];
			weight = j;
		}
	}

	cout << val << endl << weight << endl;

	for (int i=0; i<n; i++)
		delete [] dp[i];
	delete [] dp;
}

int main() {
	

	int n, W;
	int ver = 1;

	cin >> W;
	while (W!=0) {
		cin >> n;

		vector<int> v(n), w(n);
		for (int i=0; i<n; i++)
			scanf("%d,%d", &v[i], &w[i]);

		cout << "Case " << ver << ":" << endl;
		solve(n, W, v, w);

		cin >> W;
		ver++;
	}
}

C++: #include <stdio.h>
#include <string.h>

struct treasure {
	int w;
	int v;
	void operator+=(treasure t){
		*this = *this + t;
	}
	treasure operator+(treasure t){
		treasure tt;
		tt.v = this->v + t.v;
		tt.w = this->w + t.w;
		return tt;
	}
};

struct treasure t[1000];
struct treasure dp[1001][1001];
int n;

treasure max(treasure a, treasure b){
	if(a.v == b.v){
		return a.w <= b.w ? a : b;
	}else{
		return a.v >= b.v ? a : b;
	}
}

treasure calc(int i, int w){
	if(dp[i][w].v >=0){ return dp[i][w]; }
	struct treasure res;
	if(i == n){
		res.v = 0;
		res.w = 0;
	}else if(w < t[i].w){
		res += calc(i+1, w);
	}else{
		res = max(calc(i+1, w), calc(i+1, w-t[i].w) + t[i]);
	}
	return dp[i][w] = res;
}

int main(void){
	int w, c=1;
	struct treasure res;
	while(true){
		scanf("%d", &w);
		if(w == 0){ break; }
		scanf("%d", &n);
		for(int i=0; i<n; ++i){
			scanf("%d,%d", &(t[i].v), &(t[i].w));
		}
		memset(dp, -1, sizeof(dp));

		res = calc(0, w);
		printf("Case %d:\n", c++);
		printf("%d\n", res.v);
		printf("%d\n", res.w);
	}
	return 0;
}

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[1001][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+1].first){
						if(p.second > dp[i-1][j+1].second) dp[i][j+1] = p;
						else dp[i][j+1] = dp[i-1][j+1];
					}
					else if(p.first > dp[i-1][j+1].first){
						dp[i][j+1] = p;
					}
				}
			}
		}

		cout << "Case " << cou << ":" << endl;
		cout << dp[N][N].first << endl;
		cout << dp[N][N].second << endl;

	}
}