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<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXW = 1000, MAXN = 1000;

int dp[MAXN + 1][MAXW + 10];
int w[MAXN + 1];
int v[MAXN + 1];

int main()
{
    int wl, n, casen = 1;
    char t;
    while(cin >> wl && wl)
    {
        cin >> n; REP(n) cin >> v[i] >> t >> w[i];

        memset(dp, 0, sizeof(dp));
        
        REP(n)rep(j, wl+1 - w[i])
            dp[i+1][j + w[i]] = max(dp[i][j + w[i]], dp[i][j] + v[i]);
        
        int answ = 0, ansv = -1;
        REP(wl+1)if(ansv < dp[n][i]){ ansv = dp[n][i]; answ = i;}
        cout << "Case " << casen << ":\n" << ansv << "\n" << answ << endl;
        casen++;
    }

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { char c;for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> c >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int w, n;

// index, weight
int dp[1010][1010];
PArray treasure;

int main(void)
{
	ios::sync_with_stdio(false);
	int count = 1;
	while(cin >> w && w)
	{
		n = getInt();
		for (int i = 0; i <= n; i++)
		{
			memset(dp[i], 0, sizeof(dp[i]));
		}
		treasure.resize(n);
		readAll(treasure);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				int nw = j + treasure[i].second;
				if(nw > w) continue;
				dp[i + 1][nw] = max(dp[i + 1][nw], dp[i][j] + treasure[i].first);
			}
		}
		P ans = P(-1, INF);
		for (int i = 0; i <= w; i++)
		{
			if(dp[n][i] > ans.first)
			{
				ans = P(dp[n][i], i);
			}
		}

		cout << "Case " << count << ':' << endl;
		cout << ans.first << endl << ans.second << endl;
		count++;
	}
	return 0;
}

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];
    //dp init
    rep(i,n+1)rep(j,w+1) dp[i][j] = 0;  
    rep(i, n)
    {
        //カンマ区切りでの入力を受け付ける様に変更
        scanf("%d,%d" ,&value, &weight);
        data tmp = data(value, weight);
        v.push_back( tmp );
        //cout << "v == " << v[i].v <<endl;
    }

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

    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(0 <= 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;
    //cout <<  dp[n][index] << endl;
    return 0;
}

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