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

int main()
{
	int w, n, i, j, cs, val[1001], wg[1001], dp[1001], inserted[1001];
	cs = 1;
	
	while( cin >> w , w)
	{
		memset( dp, 0, sizeof(dp));
		memset( val, 0, sizeof(val));
		memset( wg, 0, sizeof(wg));
		memset( inserted, 0, sizeof(inserted));
		
		inserted[0] = 1;
		
		cin >> n;
		
		for( i = 1; i <= n; i++)
		{	
			char c;
			cin >> val[i] >> c >> wg[i];
		}
		
		
		for( j = 1; j <= n; j++)
			for( i = 0; i <= w; i++)
				if( inserted[i] && i+wg[j] <= w && dp[i+wg[j]] < dp[i] + val[j] )
				{
						dp[i+wg[j]] = max( dp[i] + val[j], dp[i+wg[j]] );
						inserted[i+wg[j]] = 1;
						i = (i+wg[j]) + 1;
				}
		
		for( i = w; i >= 0; i--)
		{
			if( dp[i] > dp[i-1] )
			{
				cout << "Case " << cs << ":" << endl;
				cout << dp[i] << endl;
				cout << i << endl;
				cs++;
				break;
			}
		}
		
	}
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){

	int w,n,cnt=1;
	int dp[1006][1005],V[1001],W[1001];
	while(cin>>w,w){
		cin>>n;
		for(int i=0;i<n;i++)scanf("%d,%d",&V[i],&W[i]);
		
		for(int i=1;i<=n;i++){
			for(int j=0;j<=w;j++){
				if(j-W[i-1]>=0)dp[i][j]=max(dp[i-1][j],dp[i-1][j-W[i-1]]+V[i-1]);
			}
		}
		int tmp=w;
		for(int i=w;i>=0;i--)if(dp[n][i]==dp[n][w])tmp=i;
		cout<<"Case "<<cnt<<":"<<endl<<dp[n][w]<<endl<<tmp<<endl;
		cnt++;
		for(int i=0;i<1001;i++)for(int j=0;j<=1000;j++)dp[i][j]=0,V[i]=W[i]=0;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <math.h>
#include <map>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <bitset>

using namespace std;

int main() {
    int size;
    int case_count = 1;
    
    
    while(cin>>size){
        //size==0なら終了
        if(size==0){
            break;
        }
        
        //データの入力
        int num;
        cin>>num;
        int val[num];
        int wei[num];
        for(int i=0; i<num; i++)scanf("%d,%d",&val[i],&wei[i]);
        
        //計算
        int dp[size+1];
        int dp_temp[size+1];
        int dp_val[size+1];
        for(int i=0; i<=size; i++){
            dp[i]=0;
            dp_val[i]=0;
        }
        dp[0]=1;

        for(int i=0; i<num; i++){
            memcpy(dp_temp,dp,sizeof(dp));
            for(int j=0; j<=size; j++){
                if(dp[j]==1){
                    int next_wei=j+wei[i];
                    int next_val=dp_val[j]+val[i];
                    if(next_wei<=size){
                        if(dp_val[next_wei]<next_val){
                            dp_temp[next_wei]=1;
                            dp_val[next_wei]=next_val;
                        }
                    }
                }
            }
            memcpy(dp,dp_temp,sizeof(dp_temp));
        }
        int max_val=0;
        int min_wei=0;
        for(int i=0; i<=size; i++){
            if(max_val<dp_val[i]){
                max_val=dp_val[i];
                min_wei=i;
            }
        }
        cout<<"Case "<<case_count<<":"<<endl;
        cout<<max_val<<endl;
        cout<<min_wei<<endl;
        case_count++;
        
    }
    
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  int cas=0;
  int anskati=0;
  int ansomo;
  int w,n;
  int kati[1111],omo[1111];
  int memo[1111][1111];
  while(1){
    cas++;
    anskati=0;
    cin>>w;
    if(w==0) break;
    cin>>n;
    for(int i=0;i<=w;i++){
      memo[0][i]=0;
    }
    for(int i=0;i<n;i++){
      char a;
      cin>>kati[i]>>a>>omo[i];
    }
    cout<<"Case "<<cas<<':'<<endl;
    
    for(int i=1;i<=n;i++){
      for(int j=1;j<=w;j++){
	if(j<omo[i-1]){
	  memo[i][j]=memo[i-1][j];
	}else{
	  memo[i][j]=max(memo[i-1][j],memo[i-1][j-omo[i-1]]+kati[i-1]);
	}
      }
    }
    for(int i=0;i<=w;i++){
      if(memo[n][i]>anskati){
	anskati=memo[n][i];
	ansomo=i;
      }
    }
    cout<<anskati<<endl;
    cout<<ansomo<<endl;
    
  }
}

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

C++: #include <iostream>
#define MAX_N 1000
#define MAX_W 10000

using namespace std;

main(){
  int W, n;
  int v[MAX_N], w[MAX_N];
  int dp[2][MAX_W+1];

  for(int count=1;;count++){
    cin >> W;
    if(W==0) break;

    cin >> n;
    for(int i=0;i<n;i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    for(int i=0;i<=W;i++){
      dp[0][i]=0;
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<=W;j++){
	if(j<w[i]){
	  dp[(i+1)&1][j]=dp[i&1][j];
	}else{
	  dp[(i+1)&1][j]=max(dp[i&1][j], dp[i&1][j-w[i]]+v[i]);
	}
      }
    }
    int ans;
    for(int i=0;i<=W;i++){
      if(dp[n&1][W]==dp[n&1][i]){
	ans=i;
	break;
      }
    }
    printf("Case %d:\n", count);
    cout << dp[n&1][W] << endl;
    cout << ans << endl;
  }
  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=0;i<N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		int dp[1010];
		for(int i=0;i<=W;i++){
			dp[i]= 0;
		}
		for(int i=0;i<N;i++){
			for(int j=W;j>=we[i];j--){
				dp[j] = max(dp[i],dp[j-we[i]]+tr[i]);
			}
		}
		int ret = 0;
		for(int i=0;i<=W;i++){
			if(dp[i] > dp[ret]) ret = i;
		}
		cout << "Case " << cou << ":" << endl;
		cout << dp[ret] << endl;
		cout << ret << endl;
	}
}

C++: //A Thief
#include <cstdio>
#include <iostream>
#include <string.h>
#include <cstdlib>

using namespace std;

int dp[2000][2000];
int n;
int w[2000], v[20000];
int W;
int weight;

int rec(int i, int j)
{
	if(dp[i][j] >= 0){
		return dp[i][j];
	}
	
	int res;
	if(i == n){
		res = 0;
	}
	else if(j < w[i]){
		res = rec(i + 1, j);
	}
	else{
		if(rec(i + 1, j) < rec(i + 1, j - w[i]) + v[i]){
			res = rec(i + 1, j - w[i]) + v[i];
			weight += w[i];
		}
		else{
			res = rec(i + 1, j);
			weight -= w[i];
		}
	}
	return dp[i][j] = res;
}

void solve()
{
	memset(dp, -1, sizeof(dp));
	cout << rec(0, W) << endl;
}

int main()
{
	int count = 1;
	while(scanf("%d", &W) != EOF){
		if(W == 0){ break; }
		char c;
		char temp[10000];
		cin >> n;
		for(int i = 0; i < n; i++){
			scanf("%d,%d", &v[i], &w[i]);
		}
		weight = 0;
		cout << "Case " << count << ":" << endl;
		solve();
		cout << weight << endl;
		count++;
	}
	
	return 0;
}