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<cstdio>
using namespace std;

int main(void){
  int n,mw,cnt = 1;
  while(cin>>mw,mw!=0){
    cin>>n;
    printf("Case %d:\n",cnt++);
  int w[100],v[100];
  int dp[100][10000];
  int dp1[100][10000];
  for(int i = 0 ; i < n ; i ++)
    scanf("%d,%d",&v[i],&w[i]);
  for(int j = w[0] ; j < mw + 1 ; j ++)
    dp[0][j] = v[0],dp1[0][j] = w[0];
  for(int i = 1 ; i < n ; i ++){
    for(int j = 0 ; j < w[i] ; j ++){
      dp[i][j] = dp[i-1][j];
      dp1[i][j] = dp1[i-1][j];
    }
    for(int j = w[i] ; j < mw + 1 ; j ++){
      dp[i][j] = max(dp[i-1][j] , dp[i-1][j-w[i]] + v[i]);    
      if(dp[i-1][j] < dp[i-1][j-w[i]] + v[i]){
	// update
	dp1[i][j] = dp1[i-1][j-w[i]] + w[i];
      }else{
	dp1[i][j] = dp1[i-1][j];
      }
    }
  }
  int res = -1,resw = -1;
  for(int i = 0 ; i < mw+1 ; i ++){
    if(res < dp[n-1][i]){
      res = dp[n-1][i];
      resw = dp1[n-1][i];
    }
  }
  /*
  for(int i = 0 ; i < n  ; i ++){
    for(int j = 0 ; j < mw + 1; j ++)
      cout<<dp1[i][j]<<" ";
    cout<<endl;
  }
  //*/
  cout<<res<<endl<<resw<<endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <math.h>
#include <map>
#include <queue>
#include <string>
using namespace std;

int W,N,v[1000],w[1000],dp[1001][1001];
char c;

int main(){
	int cn=0;
	while(cin>>W){
		if(W==0)return 0;
		cn++;
		cin>>N;
		for(int i=0;i<N;i++)cin>>v[i]>>c>>w[i];
		for(int i=0;i<N;i++){
			for(int j=0;j<=W;j++){
				if(j<w[i])dp[i+1][j]=dp[i][j];
				else dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
			}
		}
		int V=dp[N][W],w2=W;
		while(dp[N][w2]==V&&w2>0)w2--;
		cout<<"Case "<<cn<<":"<<endl<<dp[N][W]<<endl<<w2<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define iota(i,n,b,s) for(int i=int(b);i!=int((b)+(s)*(n));i+=(s))
#define range(i,n,m) iota(i,(((n)>(m))?((n)-(m)):((m)-(n))),(n),((n)>(m)?-1:1))
#define rep(i,n) iota(i,(n),0,1)
#define loop for(;;)

#define INF (1e9)
#define EPS (1e-9)
#define cons(a,b) (make_pair(a,b))
#define car(a) (a.first)
#define cdr(a) (a.second)
#define cadr(a) (car(cdr(a)))
#define cddr(a) (cdr(cdr(a)))
#define all(a) a.begin(), a.end()
#define trace(var) cerr<<">>> "<<#var<<" = "<<var<<endl;

typedef long long Integer;
typedef double Real;
typedef vector<int> vi;
typedef vector<string> vs;
typedef map<string,int> Dictionary;
const Real PI = acos(-1);

template<class S, class T>
ostream& operator<<(ostream& os, pair<S,T> p) {
  os << '(' << car(p) << ", " << cdr(p) << ')';
  return os;
}

template<class T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << v[0];
  for (int i=1, len=v.size(); i<len; ++i) os << ' ' << v[i];
  return os;
}

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

int main() {

  int case_index = 1;
  loop {
    int C, n; cin >> C >> n;
    if (!C) break;
    cout << "Case " << case_index << endl;
    ++case_index;

    vi cs(n);
    vi xs(n);
    char comma;
    rep (i, n) cin >> xs[i] >> comma >> cs[i];

    int dp[C+1];
    rep (c, C+1) dp[c] = 0;

    rep (i, n) {
      for (int c = C - cs[i]; c >= 0; --c) {
        int c2 = c + cs[i];
        if (dp[c2] < dp[c] + xs[i]) {
          dp[c2] = dp[c] + xs[i];
        }
      }
    }

    int mx = 0,
        mc = -1;
    rep (c, C + 1) {
      if (mx < dp[c]) {
        mx = dp[c];
        mc = c;
      }
    }
    cout << mx << endl;
    cout << mc << endl;
  }

  return 0;
}

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

struct treasure{
	int value;
	int weight;
};
bool comp(treasure t1, treasure t2){
	if(t1.weight <= t2.weight) return true;
	else return false;
}

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	while(cin >> w){
		int n;
		int cost[maxw + 1];
		int best[maxw + 1];
		for(int i = 0; i <= maxw; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		vector<treasure> treasures;
		cin >> n;
		for(int i = 0; i < n; i++){
			treasure t;
			char c;
			cin >> t.value >> c >> t.weight;
			treasures.push_back(t);
		}
		sort(treasures.begin(), treasures.end(), comp);
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= treasures[j].weight)
					if(cost[k] < cost[k - treasures[j].weight] + treasures[j].value)
					{
						cost[k] = cost[k - treasures[j].weight] + treasures[j].value;
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;(cost[w] == cost[w-1]) && (w > 0); w--);
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - treasures[best[l]].weight >= 0;){
			if(l - treasures[best[l]].weight >= 0){
				total += treasures[best[l]].weight;
				l -= treasures[best[l]].weight;
			}
		}
		cout << total << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;

int main(){
	int W,N,cou=0;
	int tr[1001],we[1001];
	while(cin >> W , W){
		cou++;
		cin >> N;
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		P dp[100][1001];
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				dp[i][j] = P(0,0);
			}
		}
		dp[0][0] = P(0,0);
		for(int i=1;i<=N;i++){
			dp[i][0] = P(0,0);
			for(int j=0;j<i;j++){
				if(dp[i-1][j].second+we[i] <= W){
					P p;
					p = P(dp[i-1][j].first+tr[i],dp[i-1][j].second+we[i]);
					if(p.first == dp[i-1][j+1].first){
						if(p.second > dp[i-1][j+1].second) dp[i][j+1] = p;
						else dp[i][j+1] = dp[i-1][j+1];
					}
					else if(p.first > dp[i-1][j+1].first){
						dp[i][j+1] = p;
					}
				}
			}
		}

		cout << "Case " << cou << ":" << endl;
		cout << dp[N][N].first << endl;
		cout << dp[N][N].second << endl;

	}
}