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 <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 <bits/stdc++.h>

#define SIZE 300005
#define MOD 1000000007LL
#define INF INT_MAX / 3
#define LLINF 1LL << 60
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define DOWN(i,b,a) for(int i=b;i>=a;i--)
#define SET(a,c) memset(a,c,sizeof a)
#define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FOREACH(i,c) for(auto (i) : (c))
#define BIT(i,j) ((i)>>(j))&1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end())
#define SQ(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef valarray<int> Array;
typedef pair<ll,ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i];if(i==a.size()-1)cout<<endl;else cout<<" ";}}
int gcd(int a,int b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);}
int lcm(int a,int b){return a==0||b==0?0:a*b/gcd(a,b);}

int W, N;
ll v[1010];
ll w[1010];
const int NMAX = 1000;
const int VMAX = 10000;
ll dp[NMAX*VMAX+10];

void solve()
{
  int NV = N*10000;
  fill_n(dp,NV,LLINF);
  dp[0] = 0;
  REP(i,N) DOWN(j,NV,v[i])
    dp[j] = min(dp[j],dp[j-v[i]]+w[i]);
  DOWN(i,NV,0) if(dp[i] <= W)
  {
    printf("%d\n%lld\n",i,dp[i]);
    return;
  }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  for(int i = 1; scanf("%d",&W), W; i++)
  {
    scanf("%d",&N);
    REP(i,N) scanf("%lld,%lld",&v[i],&w[i]);
    printf("Case %d:\n",i);
    solve();
  }
  return 0;
}

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

using namespace std;

int N,W;

struct Treasurea
{

  int value;
  int weight;
  int flag;

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

};

struct Memo
{

  Treasurea record;
  bool flag;

  Memo()
  {
    record.value = 0;
    record.weight = 0;
    record.flag = false;
    flag = false;
  }

};

Memo memo[1001][1001];

Treasurea temp;

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(memo[correntWeight][kind].flag) return memo[correntWeight][kind].record;
  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;

      //Treasurea temp = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

      temp = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

      if(total.value < temp.value || (total.value == temp.value && total.weight > temp.weight))
        total = temp;

      treasurea[i].flag = false;

    }

  memo[correntWeight][kind].record = total;
  memo[correntWeight][kind].flag = true;

  return total;

}

int main()
{

  int case_number = 0;

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

      cin >> N;

      vector<Treasurea> treasurea(N);
      Treasurea result;

      for(int i = 0; i < N; ++i) scanf("%d,%d",&treasurea[i].value,&treasurea[i].weight);

      memset(memo,0,sizeof(memo));

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

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

    }

  return 0;

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <cstdlib>
#include <stdio.h>
#define INF 100000000
#define INFLL (long long)1e14
#define Loops(i, n) for(int i = 0; i < (int)n; i++)
#define Loops1(i, n) for(int i = 1; i <= (int)n; i++)
int dp[2000][2000];
int w[2000];
int v[2000];
int N;
int W;
int count=1;
int kati=0,omosa=0;
int main(void){
	while(1){
		std::cin>>W;
		if(W==0){
			break;
		}
		std::cin>>N;
		Loops(i,N){
			int vi,wi;
			scanf("%d,%d",&vi,&wi);
			v[i]=vi;
			w[i]=wi;
//			std::cin>>w[i]>>v[i];
		}
		Loops(i,1000){
			Loops(j,1000){
				dp[i][j]=0;
			}
		}
		Loops(i,N){
			Loops(j,W+1){
				if(j-w[i]>=0){
					dp[i+1][j]=std::max(dp[i][j],dp[i][j-w[i]]+v[i]);
				}else {
					dp[i+1][j]=dp[i][j];				
				}
			}
		}
		Loops(i,W+1){
			if(kati<dp[N][i]){
				kati=dp[N][i];
				omosa=i;
			}
		}
		printf("Case %d:\n",count);
		std::cout<<kati<<std::endl;
		std::cout<<omosa<<std::endl;
		count++;
/*	Loops(i,N+1){
		Loops(j,W+1){
			std::cout<<dp[i][j]<<" ";
		}
		std::cout<<std::endl;
	}*/

	}
	return 0;
}