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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int N, W, v[1002], w[1002]; // v[i],w[i] := iツ氾板姪堋づ個品ツ閉ィツづ個可ソツ値ツ、ツ重ツつウ
int dp[1002][1002]; // dp[i][j] := iツ氾板姪堋づ個品ツ閉ィツづ慊づ考ツ猟カツつオツつスツ篠楪づ個、ツ重ツつウjツづ可つィツつッツづゥツ催妥・ツづ個可ソツ値
void solve() {
  memset(dp, 0, sizeof dp);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j + w[i] <= W)
        dp[i+1][j+w[i]] = max(dp[i][j+w[i]], max(dp[i][j]+v[i], dp[i+1][j+w[i]]));
      else
        dp[i+1][j+w[i]] = max(dp[i][j+w[i]], dp[i+1][j+w[i]]);
    }
  }
  // int *itr = max_element(dp[N], dp[N]+W+1);
  // printf("%d\n%d\n", *itr, itr - dp[N]);
  int ans = -1, itr = -1;
  for (int i = 0; i <= W; ++i) if (ans < dp[N][i]) ans = dp[N][i], itr = i;
  printf("%d\n%d\n", ans, itr);
}

int main() {
  int z = 1;
  while (scanf("%d",&W),W) {
    scanf("%d",&N);
    rep(i,N) scanf("%d,%d",v+i,w+i);
    printf("Case %d:\n", z);
    solve();
    ++z;
  }
  return 0; 
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;
 
#define ll long long
#define vvi vector< vector<int> >
#define vi vector<int>
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
#define pii pair<int,int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int dp[1500][1500];
int v[1500],w[1500];
int h;
int n;

int solve(int i,int j){
  if(dp[i][j] != -1) return dp[i][j];

  int hoge;

  if(i == n) hoge = 0;

  else if(j < w[i]) hoge = solve(i+1, j);

  else hoge =  max(solve(i+1, j) , solve(i+1 , j - w[i]) + v[i]);

  return dp[i][j] = hoge;

}

int main(){

  while(1){
	cin >> h;
	if(h == 0) break;

	cin >> n;

	REP(i,n){
	  cin >> v[i]>> w[i];
	}

	REP(i,1500) REP(j,1500) dp[i][j] = -1;

	cout << solve(0,h) << endl;

  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) r.begin(),r.end()

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,a,b) for(int i=(a); i<=(b); i++)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
const int IMAX=((1<<30)-1)*2+1;
const double EPS=1e-9;
//int mod=1000000007;

vector<pii> v(1010,pii(0,0));
vector<vi> dp(1010,vi(1010,-1));
int N,W;

int ap=0,aw=0;

void init(){
	for(int i=0; i<N+1; i++){
		for(int j=0; j<W+1; j++){
			dp[i][j]=-1;
		}
		v[i]=mp(0,0);
	}
	ap=0;
	aw=0;
}

int calc(int n,int w){
	if(dp[n][w]!=-1)return dp[n][w];
	int ret;
	int t=w;
	if(n==N)ret=0;
	else if(w+v[n].second>W)ret=calc(n+1,w);
	//else ret=max(calc(n+1,w),calc(n+1,w+v[n].second)+v[n].first);
	else{
		int a=calc(n+1,w),b=calc(n+1,w+v[n].second)+v[n].first;
		if(b>a)t+=v[n].second;
		ret=max(a,b);
		//if(a==0&&b==10)cout<<n<<" "<<ret<<" "<<t<<endl;
	}
	// if(ret>=ap){
	// 	ap=ret;
	// 	aw=w;
	// 	cout<<ap<<" "<<aw<<endl;
	// }
	if(ret>=ap){
		ap=ret;
		if(aw<=w)aw=t;
	}
	//cout<<ret<<" "<<n<<" "<<t<<endl;
	return dp[n][w]=ret;
}



int main(){
	int k=0;
	char c;
	while(++k){
		cin>>W;
		//cout<<"    W        "<<W<<endl;
		if(W==0)break;
		cin>>N;
		init();
		for(int i=0; i<N; i++){
			cin>>v[i].first>>c>>v[i].second;
		}
		calc(0,0);
		cout<<"Case "<<k<<":"<<endl;
		cout<<ap<<endl;
		cout<<aw<<endl;
		//scout<<v[4].first<<endl;;
		// for(int i=0; i<N+1; i++){
		// 	for(int j=0; j<W+1; j++){
		// 		if(dp[i][j]==220)cout<<i<<" "<<j<<endl;
		// 	}
		// }
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX_w = 1000;
const int MAX_N = 1000;

int w, N;
int dp[MAX_N+1][MAX_w];
int V[MAX_N];
int W[MAX_N];

void solve() {
  for (int i = N-1; i >= 0; i--) {
    for (int j = 0; j <= w; j += 1) {
      if (j < W[i]) {
        dp[i][j] = dp[i+1][j];
      } else {
        dp[i][j] = max(dp[i+1][j], dp[i+1][j-W[i]] + V[i]);
      }
    }
  }
}

int main (int argc, char const* argv[]) {
  char com;
  int tv, tw, z = 1, max_value;
  while (true) {
    cin >> w;
    if (w == 0) break;
    cin >> N;
    memset(dp, 0, sizeof dp);
    memset(V, 0, sizeof V);
    memset(W, 0, sizeof W);
    for (int i = 0; i < N; i += 1) {
      cin >> V[i] >> com >> W[i];
    }
    solve();
    cout << "Case " << z << ":" << endl << dp[0][w] << endl;
    max_value = dp[0][w];
    for (int i = w;; i--) {
      if (max_value != dp[0][i]) {
        cout << i+1 <<endl;
        break;
      }
    }
    z++;
  }
  return 0;
}

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

class treasure
{
public:
int value, weight;
bool have;
};

treasure tre[1000];
int N,W, VALUE = 0, WEIGHT = 999999;


void calcu()
{
	int sum_v = 0, sum_w = 0;
	for (int i = 0; i < N; i++)
	{
		if (tre[i].have == true)
		{
			sum_v += tre[i].value;
			sum_w += tre[i].weight;
		}
	}
	if (sum_v>VALUE)
	{
		WEIGHT = sum_w;
		VALUE = sum_v;
	}
	else if(sum_v==VALUE &&  WEIGHT>sum_w)
	{
		 WEIGHT = sum_w;
	}
}

void function(int n, int w)//0,W
{
	if (n >= N)	
	{
		calcu();
		//cout << VALUE << "," << WEIGHT << endl;
	}
	else if (tre[n].weight > w)	
	{
		tre[n].have=false;
		function(n + 1, w);
	}
	else
	{
		tre[n].have = true;	  
		function(n + 1, w - tre[n].weight);
		tre[n].have = false;   
		function(n + 1, w);
	}
}

int main()
{
	int W;
	for (int count = 1; cin >> W, W != 0; count++)
	{
		VALUE = 0;
		WEIGHT = 999999;
		cin >> N;
		for (int i = 0; i < N; i++)
		{
			scanf("%d,%d", &tre[i].value, &tre[i].weight);
		}
		function(0,W);
		cout << "case " << count << ":" << endl;
		cout << VALUE << endl;
		cout << WEIGHT << endl;
	}
	return 0;
}