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;
    cout << "Case " << case_num << ":" << endl;
    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 0;
}

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

C++: #include "math.h"
#include "stdio.h"
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>

using namespace std;


/** Problem0042 : A Thief
 Time Limit Exceeded **/

#define MAX_N 1100
#define MAX_W 1100

int W, N, tre[1100][2]={0}, solW, solN;
int dp[MAX_N][MAX_W];
int dpW[MAX_N][MAX_W];

pair<int, int> solve(int i, int j)
{
	if (dp[i][j] >= 0)
		return pair<int, int>(dp[i][j], dpW[i][j]);
	
	pair<int, int> res;
	if (i == N) {
		res = pair<int, int>(0, W-j);
	} else if (j < tre[i][1]) {
		res = solve(i+1, j);
	} else {
		//res.first = max(solve(i+1, j).first, solve(i+1, j-tre[i][1]).first+tre[i][0]);
		
		if (solve(i+1, j).first > solve(i+1, j-tre[i][1]).first+tre[i][0]) {
			res = solve(i+1, j);
		} else if (solve(i+1, j).first < solve(i+1, j-tre[i][1]).first+tre[i][0]) {
			res = pair<int, int>(solve(i+1, j-tre[i][1]).first+tre[i][0], solve(i+1, j-tre[i][1]).second);
		} else {
			res = pair<int, int>(solve(i+1, j).first, min(solve(i+1, j).second, solve(i+1, j-tre[i][1]).second));
		}
	}
	
	dp[i][j] = res.first;
	dpW[i][j] = res.second;
	return res;
}

int main()
{
	int set=0; char cam;
	while (1) {
		set++;
		
		cin >> W;
		solW = 0; solN = 0;
		if (W==0) break;
		cin >> N;
		
		memset(dp, -1, sizeof(dp));
		memset(dp, -1, sizeof(dp));
		
		for (int i=0; i<N; i++) {
			cin >> tre[i][0] >> cam >> tre[i][1];
		}
		
		pair<int, int> sol = solve(0, W);
		
		cout << "Case " << set << ":" << endl;
		cout << sol.first << endl;
		cout << sol.second << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>
#define rep(i,k,n) for(int i=k;i<n;i++)
using namespace std;
typedef pair<int,int> takara;
//cost,weight
class napsack{
private:
  vector<takara> pack;
  int lim;
public:
  napsack(){
  };
  void push(takara a){
    takara tmp;
    int flag;
    int forend=pack.size();
    rep(i,0,forend){
      tmp=make_pair(a.first+pack[i].first,a.second+pack[i].second);
      //cout<<"make"<<tmp.first<<" "<<tmp.second<<endl;  
      if(tmp.second>=lim) continue;
      flag=1;
     
      rep(j,0,forend)
	{
	if(pack[j].second>=tmp.second)
	  if(pack[j].first<=tmp.first){
	    if(flag){flag--;
	      pack[j]=tmp;}
	    else{ pack.erase(pack.begin()+j);
	      forend--;}
	    }
      }
  
      rep(j,0,pack.size())
	if(pack[j].first>=tmp.first) {flag=355;break;}
      if(flag!=355) pack.push_back(tmp);
    }
  }
  void test(){
    rep(i,0,pack.size()){
      cout<<pack[i].first<<" "<<pack[i].second<<endl;
    }
  }
  void max(int uuu){
    takara tmp=make_pair(0,0);
    rep(i,0,pack.size()){
      if(tmp.first==pack[i].first)
	if(tmp.second>pack[i].second)
	  tmp=pack[i];
      if(tmp.first<pack[i].first)
	tmp=pack[i];	
    }
        cout <<"case "<<uuu<<":"<<endl<<tmp.first<<endl<<tmp.second<<endl;
  }
  void reset(int i){
    lim=i;
    pack.resize(1,make_pair(0,0));
  }

};

 
int main(){
  int w,n,a,b,count;
  napsack sack;
  char th;  
  count=0;
  while(1){
    count++;
    cin >>w;
    if(w==0) break;
    cin >>n;
    sack.reset(w);
    rep(i,0,n){
      cin>>a>>th>>b;
      sack.push(make_pair(a,b));
      //sack.test();
    }
   
    sack.max(count);
  }
  return 0;
}