Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

const MaxW = 1000
const MaxN = 1000

type Tuple struct {
	Value  int
	Weight int
}

func nextLine(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func solve(w int, n int, vws []Tuple) Tuple {
	var dp [MaxN + 1][MaxW + 1]int

	for i := 0; i < n; i++ {
		for j:= w; j > 0; j-- {
			if j < vws[i].Weight {
				dp[i + 1][j] = dp[i][j]
			} else {
				dp[i + 1][j] = int(math.Max(float64(dp[i][j]), float64(dp[i][j - vws[i].Weight] + vws[i].Value)))
			}
		}
	}

	maxValue := dp[n][w]
	minWeight := w

	for j := w - 1; j > 0; j-- {
		if maxValue == dp[n][j] {
			minWeight = j
		} else {
			break
		}
	}

	return Tuple{Value: maxValue, Weight: minWeight}
}

func main() {
	var sc = bufio.NewScanner(os.Stdin)
	index := 1
	for {
		var w, n int
		w, _ = strconv.Atoi(nextLine(sc))
		if w == 0 {
			return
		}
		n, _ = strconv.Atoi(nextLine(sc))

		vws := make([]Tuple, n, n)
		for i := 0; i < n; i++ {
			array := strings.Split(nextLine(sc), ",")
			v, _ := strconv.Atoi(array[0])
			w, _ := strconv.Atoi(array[1])
			vws[i] = Tuple{Value: v, Weight: w}
		}

		t := solve(w, n, vws)
		fmt.Printf("Case %d:\n", index)
		fmt.Println(t.Value)
		fmt.Println(t.Weight)
		index++
	}
}

C++: #include <iostream>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct Item {
  int value;
  int weight;
};

int main () {
  for( int case_n = 1; ; case_n++  ) {
    int w, n;
    vector< Item >items;
    vector< vector<int> >dp;
    vector< vector<int> >dpw;
    cin >> w;
    if( !w ) { return EXIT_SUCCESS; }
    cin >> n;
    
    for( int i = 0; i < n; i++ ) {
      string line;
      cin >> line;
      replace( line.begin(), line.end(), ',', ' ' );
      istringstream iss( line );
      
      Item item;
      iss >> item.value >> item.weight;
      items.push_back( item );
    }
    for( int i = 0; i <= n; i++ ) {
      vector<int> dpline;
      vector<int> dpwline;
      for( int j = 0; j <= w; j++ ) {
        int opt, optw;
        if( i == 0 || j == 0 ) {
          opt = 0;
          optw = 0;
        } else if( j < items[ i - 1 ].weight ) {
          opt = dp[ i - 1 ][ j - 1 ];
          optw = dpw[ i - 1 ][ j - 1 ];
        } else { 
          if( 
            dp[ i - 1 ][ j - 1 ] <
            dp[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].value
          ) {
            opt  =  dp[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].value;
            optw = dpw[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].weight;
          } else if(
            dp[ i - 1 ][ j - 1 ] ==
            dp[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].value
          ) {
            opt  =  dp[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].value;
            optw = min(
              dpw[ i - 1 ][ j - 1 ],
              dpw[ i - 1 ][ j  - items[ i - 1 ].weight ] + items[ i - 1 ].weight
            );
          } else {
            opt = dp[ i - 1 ][ j - 1 ];
            optw = dpw[ i - 1 ][ j - 1 ];
          }
        }
        dpline.push_back( opt );
        dpwline.push_back( optw );
      }
      dp.push_back( dpline );
      dpw.push_back( dpwline );
    }
    cout << "Case " << case_n << ":"<<endl;
    cout << dp[ n ][ w ] << endl;
    cout << dpw[ n ][ w ] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#define max(a,b) {a > b ? a:b}
using namespace std;

//O(W*N)??§??????
#define MAX_W 1000
#define MAX_N 100
#define MAX_VALUE 10000
#define MAX_WEIGHT 1000

int dp[MAX_W+1];
vector<pair<int, int> > Item;
void q42() {
	int W, N;
	int cnt = 1;
	for (; cin >> W >> N;) {
		if (!W) break;

		for (int i = 0; i < N; i++) {
			int value, weight;
			char tmp;
			cin >> value >> tmp >> weight;
			Item.push_back(make_pair(value, weight));
		}
		for (int i = 0; i <= W; i++) {
			dp[i] = -1;
		}
		dp[0] = 0;

		for (int i = 0; i < N; i++) {
			int value = Item[i].first;
			int weight = Item[i].second;
			for (int j = W; j >= 0; j--) {
				if (j - weight < 0) continue;
				if (dp[j - weight] == -1) continue;
				dp[j] = max(dp[j], dp[j - weight] + value);
			}
		}

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

		cout << "Case " << cnt << ":" << endl;
		cout << ans_value << endl;
		cout << ans_weight << endl;
		cnt++;
	}
}

int main() {
	q42();
	return 0;
}

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

using namespace std;

struct Treasure
{

  long value;
  long weight;
  bool flag;
};

bool flag;
int limit_weight;
int treasure_number;
Treasure result_treasure;

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

  if(limit_weight < total_weight) 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;

    }

}