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

using namespace std;

struct Treasure
{

  long value;
  long weight;
  bool flag;
};

bool flag;
long limit_weight;
long treasure_number;
Treasure result_treasure;

int AThief(vector<Treasure> &treasure,long &total_value,long &total_weight)
{

  if(limit_weight < total_weight){
    cout << "limit_weight" << endl;
    return 0;
  }
  else
    {

      for(int i = 0; i < treasure_number; i++)
        {

          if(treasure[i].flag)
            {

              total_value += treasure[i].value;
              total_weight += treasure[i].weight;
              treasure[i].flag = false;

              if(AThief(treasure,total_value,total_weight) == 0)
                {

                  total_value -= treasure[i].value;
                  total_weight -= treasure[i].weight;

                  if(result_treasure.value == total_value && result_treasure.weight > total_weight) result_treasure.weight = total_weight;
                  else if(result_treasure.value < total_value)
                    {
                      result_treasure.value = total_value;
                      result_treasure.weight = total_weight;
                    }
                  flag = false;
                }
              else
                {
                  total_value -= treasure[i].value;
                  total_weight -= treasure[i].weight;
                }

              treasure[i].flag = true;

            }

        }

    }

  //全部足しても耐えられる重さを越えなかった時
  if(flag)
    {
      result_treasure.value = total_value;
      result_treasure.weight = total_weight;
      flag = false;
    }


  return 1;

}


int main()
{

  int dummy;
  Treasure temp;
  int case_number = 0;
  long total_value = 0;
  long total_weight = 0;
  vector<Treasure> treasure;

  cin >> limit_weight;

  while(limit_weight!=0)
    {

      cin >> treasure_number;

      treasure.clear();

      for(int i = 0; i < treasure_number; i++)
        {

          scanf("%d,%d",&temp.value,&temp.weight);
          temp.flag = true;
          treasure.push_back(temp);

        }

      result_treasure.value = -1;
      result_treasure.weight = 2147483647;
      flag = true;
      dummy = AThief(treasure,total_value,total_weight);

      case_number++;

      cout << "Case " << case_number << ":" << endl;
      cout << result_treasure.value << endl;
      cout << result_treasure.weight << endl;

      cin >> limit_weight;

    }

}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

int main(){
    int N,W,cnt=1;
    int v[1001],w[1001];

    while(cin>>W){
        if(W==0) break;

        cin>>N;
        int dp[N+1][W+1];

        for(int i=0;i<=W;i++){
            dp[0][i]=0;
        }

        for(int i=0;i<N;i++){
            scanf("%d,%d",v+i,w+i);
        }

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

        cout<<"Case "<<cnt++<<":\n"<<dp[N][W]<<endl;

        int max_w=W;
        while(dp[N][W]==dp[N][max_w]) max_w--;

        cout<<max_w+1<<endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

vi v,w;
int n;
int dp[1010][1010];

int solve(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=solve(i+1,j);
	else res=max(solve(i+1,j),solve(i+1,j-w[i])+v[i]);
	return dp[i][j]=res;
}

int main(){
	for(int l=1;;l++){
		int W,x,y;
		v.clear(); w.clear();
		cin>>W;
		if(W==0) break;
		cin>>n;
		rep(i,0,n){
			scanf("%d,%d",&x,&y); v.pb(x); w.pb(y);
		}
		memset(dp,-1,sizeof(dp));
		int MAX=0,ans=0;
		rer(i,1,W){
			if(MAX<solve(0,i)){
				MAX=solve(0,i); ans=i;
			}
		}
		cout<<"Case "<<l<<":"<<endl; o(MAX); o(ans);
	}
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;

int cnt, W, N, w[1000], v[1000], dp[1000][1001];

void solve(){
    for(int i = 0; i < N; i++){
        dp[i+1][w[i]] = v[i];
        for(int j = w[i]+1; j <= W; j++){
            dp[i][j] = 0;
            if( dp[i][j-w[i]] ){
                dp[i+1][j] = dp[i][j-w[i]] + v[i];
            }
        }
    }
    int c,v = 0;
    for(int i = 0; i < W; i++){
        if( dp[N][i] > v ){
            v = dp[N][i];
            c = i;
        }
    }
    printf("Case %d:\n%d\n%d\n", cnt, v, c);
}

int main(){
    cnt = 0;
    while( true ){
        cnt++;
        scanf("%d", &W);
        if( !W ) break;
        scanf("%d", &N);
        for(int i = 0; i < N; i++){
            scanf("%d,%d", v+i, w+i);
        }
        solve();
    }
    return 0;
}