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 FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0,  1, -1,  1, -1 };
static const int dy[] = { 0, -1, 0, 1 , 1,  1, -1, -1 };

int can_take, n, cnt = 0;
PII dp[ 1001 ][ 1001 ];
vector<PII> Item;
PII ans;

PII solve( int w, int now ) {
	if ( dp[ now ][ w ].first != -1 ) {
		return dp[ now ][ w ];
	}
	if ( now == n || w == can_take ) {
		return PII( 0, w );
	}
	PII res = PII( 0, 0 );
	if ( w + Item[ now ].second <= can_take ) {
		res = solve( w + Item[ now ].second, now + 1 );
		res.first += Item[ now ].first;		
	}

	PII d = solve( w, now + 1 );
	if ( res.first < d.first || ( res.first == d.first && res.second > d.second ) ) {
		res = d;
	}
	return dp[ now ][ w ] = res;
}

int main() {
	while ( ~scanf( "%d", &can_take ) && can_take ) {
		printf( "Case %d:\n", ++cnt );
		scanf( "%d", &n );
		Item.resize( n );
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d,%d", &Item[ i ].first, &Item[ i ].second );
		}
		for ( int i = 0; i < 1001; i++ ) {
			for ( int j = 0; j < 1001; j++ ) {
				dp[ i ][ j ] = PII( -1, -1 );
			}
		}
		ans = solve( 0, 0 );
		printf( "%d\n", ans.first );
		printf( "%d\n", ans.second );
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define MAX_V 1000
#define MAX_W 10000
int W,N;
int dp[MAX_V+1][MAX_W+1];
int w[MAX_W+1];
int v[MAX_V+1];
void solve(){
  int cnt = 0;
  vector<int> data(5,0);
  for(int i = N - 1 ; i >= 0 ; i --){
    
    for(int j = 0 ; j <= W ; j ++){
    data[i]++;
      if(j<w[i]){
	data[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]);

	if(dp[i+1][j] < dp[i+1][j-w[i]] + v[i]){
	  data[i]--;
	}
      }
    }
  }
  rep(i,5){
    if(data[i]==0){
      cnt+= w[i];
    }
  }
  printf("%d\n",dp[0][W]);
  cout<<cnt<<endl;
}

int main(void){
  int cnt = 0;
  while(cin>>W,W){
    cout<<"Case "<<++cnt<<":"<<endl;
    cin>>N;
    for(int i = 0 ; i < N ; i ++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    solve();
    
  }
  
}

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

int W,N;
int dp[1001][1001]; // 宝の数×重さ
struct Obj {
	int price;
	int weight;
} obj[1001];

int main(void){
	while (cin >> W, W) {
		// input
		cin >> N;
		char buf;
		for (int i = 0; i < N; i++) {
			cin >> obj[i].price >> buf >> obj[i].weight;
		}
		
		// init
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = 0;
			}
		}
		dp[0][obj[0].weight] = obj[0].price;

		// calc
		int wei;
		for (int i = 0; i < N - 1; i++) {
			for (int j = 0; j <= W; j++) {
				if (j != 0 && dp[i][j] == 0) {
					continue;
				}
				// 下
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
				// 右下
				if (wei = j + obj[i + 1].weight, wei <= W) {
					dp[i + 1][wei] = max(dp[i + 1][wei], dp[i][j] + obj[i + 1].price);
				}
			}
		}
		
		cout << endl;
		struct Obj res = {0, 0};
		for (int i = 0; i <= W; i++) {
			for(int j = 0; j < N; j++) {
				if (res.price < dp[j][i]) {
					res.price = dp[j][i];
					res.weight = i;
				}
			}
		}
		cout << endl;

		// output
		cout << res.price << endl << res.weight << endl;
	}

	return 0;
}

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

#define SIZE 1001
#define MAX_W 1001

int main(void) {
    int V[SIZE][MAX_W] = {};
    int v[SIZE];
    int w[SIZE];
    int W, N;
    int ans_v, ans_w;
    int n = 1;
    
    while(true) {
        cin >> W;
        if(W == 0)   break;
        
        cin >> N;
        for(int i = 0; i < N; ++i) {
            scanf("%d,%d", &v[i], &w[i]);
        }
        
        for(int i = 1; i <= N; ++i) {
            for(int j = 1; j <= W; ++j) {
                if(w[i - 1] > j) {
                    V[i][j] = V[i - 1][j];
                } else {
                    V[i][j] = max(V[i - 1][j], V[i - 1][j - w[i - 1]] + v[i - 1]);
                }
            }
        }
        
        ans_w = W;
        while(V[N][ans_w] == V[N][W]) {
            --ans_w;
        }
        ++ans_w;
        ans_v = V[N][ans_w];
        
        printf("Case %d:\n", n);
        cout << ans_v << endl << ans_w << endl;
        
        ++n;
    }
    
    return 0;
}

C++: #include <iostream>
#define MAX_N 1000
#define MAX_W 10000

using namespace std;

main(){
  int W, N;
  int v[MAX_N], w[MAX_N];
  int dp[MAX_N+1][MAX_W+1];

  for(int count=1;;count++){
    cin >> W;
    if(W==0) break;

    cin >> N;
    for(int i=0;i<N;i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    for(int i=0;i<=W;i++){
      dp[0][i]=0;
    }
    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 ans;
    for(int i=0;i<=W;i++){
      if(dp[N][W]==dp[N][i]){
	ans=i;
	break;
      }
    }
    printf("Case %d:\n", count);
    cout << dp[N][W] << endl;
    cout << ans << endl;
  }
  return 0;
}