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