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<memory.h>
#include<algorithm>
using namespace std;
int main()
{
	int W,n,c=0;
	int w,v;
	int dp[1001][1001];
	while (1){
		cin >> W;
		if (W == 0)break;
		c++;
		cin >> n;
		cin >> w >> n;
		for (int i = 1; i <= n; i++){
			for (int i2 = 0; i2 <= W; i2++){
				if (i2 < w)
					dp[i][i2] = dp[i - 1][i2];
				else
					dp[i][i2] = max(dp[i - 1][i2], dp[i - 1][w - i2] + v);
			}
		}
		int maxv=0, maxw;
		for (int i = 0; i <= W; i++){
			if (maxv < dp[n][i]){
				maxv = dp[n][i];
				maxw = i;
			}
		}
		cout << "Case" << c << ":" << endl;
		cout << maxv << endl << maxw << endl;
	}
	
}

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

using namespace std;

int N,W;

struct Treasurea
{

  int value;
  int weight;
  int flag;

  Treasurea()
  {
    value = 0;
    weight = 0;
    flag = false;
  }

  Treasurea& operator+=(const Treasurea &obj)
  {
    value += obj.value;
    weight += obj.weight;
  };

};

Treasurea knapsack(vector<Treasurea> &treasurea, int correntWeight,int correntValue, int kind)
{

  if(correntWeight < 0)
    {
      Treasurea temp;
      temp.weight = W - correntWeight + treasurea[kind - 1].weight;
      temp.value = correntValue - treasurea[kind - 1].value;
      return temp;
    }
  else if(kind == treasurea.size())
    {
      Treasurea temp;
      temp.weight = W - correntWeight;
      temp.value = correntValue;
      return temp;
    }

  Treasurea total;

  for(int i = 0;i < treasurea.size(); ++i)
    {

      if(treasurea[i].flag) continue;

      treasurea[i].flag = true;

      if((total.value < knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,kind + 1).value ) || (total.value == knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).value && total.weight > knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).weight))
         total = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

      treasurea[i].flag = false;

    }

  return total;

}

int main()
{

  int case_number = 0;
  vector<Treasurea> treasurea(N);
  Treasurea result;

  while(cin >> W && W != 0)
    {

      cin >> N;

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

          Treasurea temp;
          scanf("%d,%d",&temp.value,&temp.weight);
          treasurea.push_back(temp);

        }

      result = knapsack(treasurea,W,0,0);

      cout << "Case " << ++case_number << endl << result.value << endl << result.weight << endl;

      treasurea.erase(treasurea.begin(),treasurea.end());

    }

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

/*
風呂敷の耐えられる重さ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; }
}

void solve()
{
    int w,n;
    //vector<P> v;
    vector<data> v;
    cin >> w >> n;
    if(w == 0) { return; }
    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 << dp[n][w] << endl;
    cout << index << endl;
    return;
}

int main()
{
    int case_num;
    int i=1;
    while (1)
    {
        cout << "Case " << i << ":" << endl;
        solve();
        i++;
    }
}