Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	Case := 1
	for {
		scanner.Scan()
		W, _ := strconv.Atoi(scanner.Text())
		if W == 0 {
			break
		}
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		w := make([]int, n+1)
		v := make([]int, n+1)
		for i := 1; i <= n; i++ {
			scanner.Scan()
			info := strings.Split(scanner.Text(), ",")
			v[i], _ = strconv.Atoi(info[0])
			w[i], _ = strconv.Atoi(info[1])
		}
		// fmt.Println(v)
		// fmt.Println(w)
		dp := make([][]int, n+1)
		for i := range dp {
			dp[i] = make([]int, W+1)
		}
		maxV := -(1 << 21)
		minW := 1 << 21
		for i := 1; i <= n; i++ {
			for j := 1; j <= W; j++ {
				// fmt.Println(dp)
				if j-w[i] >= 0 {
					dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i])
					if maxV < dp[i][j] {
						maxV = dp[i][j]
						minW = j
					} else if maxV == dp[i][j] {
						minW = min(minW, j)
					}
				} else {
					dp[i][j] = dp[i-1][j]
				}
			}
		}
		fmt.Printf("Case %d:\n", Case)
		Case += 1
		fmt.Println(maxV)
		fmt.Println(minW)
	}
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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;
}