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 <cstdio>
#include <vector>

using namespace std;

int main()
{
  int index = 1;
  while (true) {
    int W;
    scanf("%d ", &W);
    if (W == 0) {
      break;
    }
    int N;
    scanf("%d ", &N);
    vector<int> dp(W + 1, -1);
    for (int i = 0; i < N; i++) {
      int value, weight;
      scanf("%d,%d ", &value, &weight);
      //printf("%d,%d\n", value, weight);

      for (int j = W; j >= 0; j--) {
	if (dp[j] >= 0 && j + weight <= W) {
	  dp[weight + j] = max(dp[weight + j], dp[j] + value);
	}
      }
      dp[weight] = max(dp[weight], value);
      /*for (int i = 0; i <= W; i++) {
	if (dp[i] >= 0) {
	  printf("dp:%d, %d\n", i, dp[i]);
	}
      }
      printf("\n");*/
    }
    int max_weight = 0, max_value = -1;
    for (int i = 0; i <= W; i++) {
      if (max_value < dp[i]) {
	max_weight = i;
	max_value = dp[i];
      }
    }
    printf("Case %d:\n%d\n%d\n", index++, max_value, max_weight);
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
int main()
{
	for (int i = 1;; i++)
	{
		int W;
		std::cin >> W;
		if (W == 0)
		{
			break;
		}
		int N;
		std::cin >> N;
		std::vector<std::vector<int>>table(N + 1, std::vector<int>(W + 1, -1));
		table[0][0] = 0;
		for (int n = 0; n < N; n++)
		{
			int vi, wi;
			char foo;
			std::cin >> vi >> foo >> wi;
			for (int w = 0; w <= W; w++)
			{
				if (table[n][w] != -1)
				{
					if (table[n + 1][w] < table[n][w])
					{
						table[n + 1][w] = table[n][w];
					}
					if (w + wi <= W)
					{
						table[n + 1][w + wi] = table[n][w] + vi;
					}
				}
			}
		}
		int max_value = 0;
		int weight;
		for (int w = 0; w <= W; w++)
		{
			if (table[N][w] > max_value)
			{
				max_value = table[N][w];
				weight = w;
			}
		}
		std::cout << "Case 1:" << std::endl;
		std::cout << max_value << std::endl;
		std::cout << weight << std::endl;
	}
}

C++: #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int W, n, dp[1024];

int main() {
    for (int _q = 0;; _q++) {
        scanf("%d", &W);
        if (W == 0) return 0;
        scanf("%d", &n);
        memset(dp, 0, sizeof(dp));
        rep (i, n) {
            int v, w;
            scanf("%d,%d", &v, &w);
            for (int i = W-w; i >= 0; i--) {
                dp[i+w] = max(dp[i+w], dp[i]+v);
            }
        }
        int ansv = 0, answ = 0;
        rep (i, W+1) if (ansv < dp[i]) {
            ansv = dp[i];
            answ = i;
        }
        printf("Case %d:\n", _q+1);
        printf("%d\n", ansv);
        printf("%d\n", answ);
    }
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int W,N,w[1001],v[1001],ansW,ansV;
int memo[1001][1001];
void func(int i,int j,int _v){
    //printf(" %d,%d,%d\n",i,j,_v);
    if(memo[i][j]>_v){
        return;
    }
    memo[i][j] = _v;
    if(i==N){
        if(ansV<_v){
            ansV = _v;
            ansW = j;
        }
        else if(ansV==_v && ansW>j){
            ansV = _v;
            ansW = j;
        }
        return;
    }
    //ans2=max(ans2,j);
    if(j+w[i]<=W) func(i+1,j+w[i],_v+v[i]);
    func(i+1,j,_v);
    //ans2=max(ans2,j);
}
int main(){
    int tc=1;
    while(cin>>W,W){
        fill(*memo,*memo+1001*1001,0);
        ansW=ansV=0;
        cin>>N;
        for(int i=0;i<N;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        func(0,0,0);
        cout<<"Case "<<tc<<":"<<endl<<ansV<<endl<<ansW<<endl;;
        tc++;
    }
}

C++: #include <cstdio>

using namespace std;

void nap(int max,int i){
	int memo[1001]={0};
	int n,v,w;//n:個数 v:価値 w:重さ
	int ansV=0;
	int ansW=0;
	
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d,%d",&v,&w);
		for(int j=max;j>=w;j--){
			if(j==w || memo[j-w]>0){
				if(memo[j]<memo[j-w]+v){
					memo[j]=memo[j-w]+v;
				}
			}
		}
	}
	
	for(int i=0;i<=max;i++){
		if(memo[i]>ansV){
			ansV=memo[i];
			ansW=i;
		}
	}
	printf("Case %d:\n",i);
	printf("%d\n%d\n",ansV,ansW);
}

int main(){
	int w,i=1;
	scanf("%d",&w);
	while(w!=0){
		nap(w,i);
		i++;
		scanf("%d",&w);
	}
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int dp[1001],N,v[100],w[100],W;
signed main(){
    for(int l=1;;l++){
        fill(dp,dp+1001,0);
        /*fill(v,v+100,0);
        fill(w,w+100,0);*/
        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<N;i++){
            for(int j=W;j>=w[i];j--){
                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
            }
        }
        for(int i=W;i>=0;i--){
            if(dp[W]!=dp[i]){
                cout<<"Case "<<l<<":"<<endl<<dp[W]<<endl<<i+1<<endl;
                break;
            }
        }
    }
    return 0;
}

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

#define TEST 1
typedef std::pair<int,int> pi;

struct Pred
{
    bool operator()(const pi& f,const pi& s)//<
    {
        return f.first < s.first;
    }
};
pi rec_check(const std::vector<pi> &data,int start,int remw)
{
    if(start >=data.size() )
        return pi(0,0);
    if(data[start].second > remw)
        return rec_check(data,start+1,remw);
    pi div0= rec_check(data,start + 1,remw - data[start].second);
    div0.first+=data[start].first;
    div0.second+=data[start].second;
    pi div1 = rec_check(data,start + 1,remw);
    if(div0.first > div1.first )
        return div0;
    if(div0.first < div1.first)
        return div1;
    if(div0.second <div1.second)
        return div0;
    return div1;
    
}
int main(void)
{
    int ccount=1;
    int w,n;
    using namespace std;
    vector<pair<int,int> > data;
    while(scanf("%d%d",&w,&n)==2 && w!=0)
    {
        data.assign(n,std::pair<int,int>(0,0));
        for(int i=0;i<n;i++)
        {
            scanf("%d%*[,]%d",&data[i].first ,&data[i].second );
        }
        Pred pred;
        sort(data.begin(),data.end(),pred);
        printf("Case %d:",ccount);
#if TEST
        for(int i=0;i<n;i++)
        {
            printf("val=%d,wei=%d\n",data[i].first,data[i].second);
        }
#endif
        pi result=rec_check(data,0,w);
        printf("%d\n%d\n",result.first,result.second);
        data.clear();
        ccount++;
    }
    return 0;
}