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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int dp[1010][1010][2];

int main() {
	int w,n,num=0;;
	while(w=in(),w){
		n=in();
		memset(dp,0,sizeof(dp));
		int a,b,i;
		for(i=0;i<n;i++){
			scanf("%d,%d",&a,&b);
			int k;
			for(k=0;k<b;k++){
				dp[i+1][k][0]=dp[i][k][0];
				dp[i+1][k][1]=dp[i][k][1];
			}
			for(k=b;k<=w;k++){
				if(dp[i][k][0]<dp[i][k-b][0]+a
				||dp[i][k][0]==dp[i][k-b][0]+a&&dp[i][k][1]>dp[i][k-b][1]+b){
					dp[i+1][k][0]=dp[i][k-b][0]+a;
					dp[i+1][k][1]=dp[i][k-b][1]+b;
				}else{
					dp[i+1][k][0]=dp[i][k][0];
					dp[i+1][k][1]=dp[i][k][1];
				}
			}
			//cout<<dp[w]<<endl;
		}
		cout<<"Case "<<++num<<":"<<endl;
		cout<<dp[n][w][0]<<endl;
		cout<<dp[n][w][1]<<endl;
	}
	return 0;
}

C++: const int MAX_N = 1000; // n????????§???
const int MAX_W = 5000; // W????????§???

// ??\???
int n, W;
int w[MAX_N], v[MAX_N];

// DP????????????
// dp[i][j]???i????????\?????????????????????????????????j??\?????????????????????????????¨??????????????????????????§????????¨??????
int dp[MAX_N + 1][MAX_W + 1];

void solve_dp2() {
  for (int j = 0; j <= W; j++) {
    dp[n][j] = 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[0][W] << endl;
}

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

const int INF = 1 << 27;
const int MAX_N = 1001;
const int MAX_W = 1001;
// ???????????????
int v[MAX_N], a[MAX_N];
int dp[MAX_N][MAX_W];

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int w, n;
	int cnt = 1;
	while(cin >> w && w != 0){
	cin >> n;
	
	for(int i = 0; i < n; ++i){
		char c;
		cin >> v[i] >> c >> a[i];
	}
	
	for(int i = 0; i < n; ++i){
		for(int j = 0; j <= w; ++j){
			if(j < a[i])
				dp[i + 1][j] = dp[i][j];
			else
				dp[i + 1][j] = max(dp[i][j], dp[i][j - a[i]] + v[i]);
		}
	}
	
	int m = INF;
	for(int i = 0; i < n; ++i){
		for(int j = 0; j <= w; ++j){
			if(dp[i + 1][j] == dp[n][w]){
				m = min(m, j);
			}
		}
	}
	
	cout << "Case " << cnt << ":" << endl;
	cout << dp[n][w] << endl;
	cout << m << endl;
	cnt++;
	}
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cctype>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

//#define deb_table(arr, w, h) rep(w_n, w){  rep(h_n, h) {cout << arr[w][h] << " "  } cout << endl; }
#define rep(i, n) for(int i = 0; i < n; i++)

// P(価値, 重さ)
typedef pair<int ,int> P;

class data
{
    public:
        int v;
        int w;
    public:
        data(int a, int b){ v=a; w=b;}
};

int case_num=1;
/*
風呂敷の耐えられる重さW(1000 以下)
お宝の数 N
お宝 1 の価値(整数), お宝 1 の重さ( ＜ W:整数)
お宝 2 の価値(整数), お宝 2 の重さ( ＜ W:整数)
.
.
.
お宝 N の価値(整数), お宝 N の重さ( ＜ W:整数)
*/

int max( int a, int b)
{
    if(a > b) { return a;}
    else { return b; }
}

//
int solve()
{
    int w,n;
    vector<data> v;
    cin >> w;
    //終わりのときは-1を返す
    if(w == 0) { return -1; }

    cin >> n;
    int value, weight;

    //int dp[n+1][w+1];
    //dp[考えたインデックス][重さ]
    //インデックスは０から数えてnまで　=> [n+1]
    //重さは０から数えてwまで        　=> [w+1]
    int dp[n+1][w+1];
    //dp init
    rep(i,n+1)rep(j,w+1) dp[i][j] = 0;  

    rep(i, n)
    {
        scanf("%d,%d" ,&value, &weight);
        v.push_back( data(value, weight) );
    }

    //dp[何番目まで考えたか][重さ] = 最大値

    rep(i,n)
    {
        rep(j,w+1)
        {
            //dp[i][j]から二つ分岐、分岐先ではmaxをとる
            if(0 <= i && i < n && 0 <= j && j  <= w )
            {
                //真下に落とす
                //printf("dp[%d][%d] = %d\n", i,j, dp[i][j]);
                //puts("or");
                //printf("dp[%d][%d] = %d\n", i+1,j, dp[i+1][j]);
                dp[i+1][j] = max(dp[i+1][j] , dp[i][j]);
               // if(i==2){ printf("dp[2][0] = %d", dp[2][0]); }
                if ( j+v[i].w <= w )
                {
                    //if(i == 1) { printf("dp[%d][%d]", i+1, j+v[i].w );  cout << dp[i+1][j+ v[i].w] << endl; }
                    //dp[i+1][j + v[i+1].w] = max(dp[i+1][j + v[i+1].w] , dp[i][j] + v[i+1].v);
                    dp[i+1][j + v[i].w] = max(dp[i+1][j + v[i].w] , dp[i][j] + v[i].v);
                }
            }
        }
    }
    /*
    rep(i,n+1){
        cout << "[";
        rep(j,w+1){
            cout << dp[i][j] << " ";
        }
        cout << "]" << endl;
    }
    */
    //maxを取る値の最小の重さを探す
    int least_weight;
    rep(j,w){
         if(dp[n][j] < dp[n][j+1])
         {
             least_weight= j+1;
         }
    }
   
    cout << "Case " << case_num << ":" << endl;
    cout << dp[n][w] << endl;
    cout << least_weight<< endl;
    return 0;
}

int main()
{
    while (1)
    {
        if(solve() == -1){ break;};
        case_num++;
    }
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;

class treasure
{
public:
int value, weight;
bool have;
};

treasure tre[1001];
int N,W, VALUE = 0, WEIGHT = 999999;

void calcu()
{
	int sum_v = 0, sum_w = 0;
	for (int i = 0; i < N; i++)
	{
		if (tre[i].have == true)
		{
			sum_v += tre[i].value;
			sum_w += tre[i].weight;
		}
	}
	if (sum_v>VALUE)
	{
		WEIGHT = sum_w;
		VALUE = sum_v;
	}
	else if(sum_v==VALUE &&  WEIGHT>sum_w)
	{
		 WEIGHT = sum_w;
	}
}

void function(int n, int w)//0,W
{
	if (n >= N)	  
	{
		calcu();
	}
	else if (tre[n].weight > w)	 
	{
		tre[n].have=false;
		function(n + 1, w);
	}
	else
	{
		tre[n].have = true;
		function(n + 1, w - tre[n].weight);
		tre[n].have = false;
		function(n + 1, w);
	}
}

int main()
{
	int W;
	for (int count = 1; cin >> W, W != 0; count++)
	{
		VALUE = 0;
		WEIGHT = 999999;
		cin >> N;
		for (int i = 0; i < N; i++)
		{
			scanf("%d,%d", &tre[i].value, &tre[i].weight);
		}
		function(0,W);
		cout << "case " << count << ":" << endl;
		cout << VALUE << endl;
		cout << WEIGHT << endl;
	}
	return 0;
}