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 <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<P> v;
    vector<data> v;
    cin >> w;
    if(w == 0) { return -1; }

    cin >> n;
    int value, weight;
    //dp[重さ][考えたインデックス]
    //int dp[n+1][w+1];
    //dp[考えたインデックス][重さ]
    //インデックスは０から数えてnまで　=> [n+1]
    //重さは０から数えてwまで　=> [w+1]
    int dp[n+1][w+1];
    rep(i, n)
    {
        //カンマ区切りでの入力を受け付ける様に変更
        scanf("%d,%d" ,&value, &weight);
        data tmp = data(value, weight);
        v.push_back( tmp );
        //cout << "v == " << v[i].v <<endl;
    }

    //dp[何番目まで考えたか][重さ] = 最大値
    //dp init
    //rep(i,n+1)rep(j,w+1) dp[i][j] = 0;  
    rep(i,n+1)rep(j,w+1) dp[i][j] = 0;  


    rep(i,n+1)
    {
        rep(j,w+1)
        {
            if( i == 0) {continue;}
            //枠の中に入っていたら
            /*
            if(0 <= i-1 && i-1 <= n && 0 <= j-v[i].w && j-v[i].w <= w)
            {
            ////////////////////////////////////////////////////////////////////////////////////////////////////
                //cout << "dp = max(" << dp[i - 1][j] << "or" << dp[i-1][j - v[i].w] + v[i-1].v << endl;
                //dp[i][j] = max( dp[i - 1][j] , dp[i-1][ j - v[i].w ] + v[i].v );
                dp[i+1][j] = max (dp[i+1][j] , dp[i][j]);
                dp[i+1][j + v[i+1].v] = max (dp[i+1][j] , dp[i][j]);
            }
            */

            if(1 <= i && i < n && 0 <= j && j  <= w )
            {
            ////////////////////////////////////////////////////////////////////////////////////////////////////
                //cout << " (i , j) " << i << ", " << j <<endl;
                dp[i+1][j] = max (dp[i+1][j] , dp[i][j]);
                //cout << "max " <<  dp[i+1][j] << "or " << dp[i][j] << endl;
                if ( j+v[i+1].w <= w )
                {
                    //cout << "max " <<  dp[i+1][j+v[i+1].w] << "or " << dp[i][j] + v[i+1].v << 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);
                }
            }
        }
    }
    /*
    rep(i,n+1){
        cout << "[";
        rep(j,w+1){
            cout << dp[i][j] << " ";
        }
        cout << "]" << endl;
    }
    */
    int index;
    //int max_weight;
    rep(j,w){
         if(dp[n][j] < dp[n][j+1])
         {
             index = j+1;
         }
    }
    cout << "Case " << case_num << ":" << endl;
    cout << dp[n][w] << endl;
    cout << index << endl;
    return 0;
}

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