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 <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { char c;for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> c >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

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

int w, n;

// index, weight
int dp[1010][1010];
PArray treasure;

int main(void)
{
	ios::sync_with_stdio(false);
	int count = 1;
	while(cin >> w && w)
	{
		n = getInt();
		for (int i = 0; i <= n; i++)
		{
			memset(dp[i], 0, sizeof(dp[i]));
		}
		treasure.resize(n);
		readAll(treasure);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				int nw = j + treasure[i].second;
				if(nw > w) continue;
				dp[i + 1][nw] = max(dp[i + 1][nw], dp[i][j] + treasure[i].first);
			}
		}
		P ans = P(-1, INF);
		for (int i = 0; i <= w; i++)
		{
			if(dp[n][i] > ans.first)
			{
				ans = P(dp[n][i], i);
			}
		}

		cout << "Case " << count << ':' << endl;
		cout << ans.first << endl << ans.second << endl;
		count++;
	}
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std ;

int N , W ;
int v[1001] , w[1001] ;
int dp[1002][1002] ;
int ans1 , ans2 ;

void solve(){
	memset(dp,-1,sizeof(dp)) ;
	dp[0][0] = 0 ;
	ans1 = 0 ;
	ans2 = 0 ;
	
	for( int i=1 ; i<=N ; i++ ){
		for( int j=0 ; j<=W ; j++ ){
			if( dp[i-1][j] >= 0 ){
				if( dp[i][j] < dp[i-1][j] ) dp[i][j] = dp[i-1][j] ;
				if( j+w[i] < W ){
					dp[i][j+w[i]] = max( dp[i-1][j+w[i]] ,dp[i-1][j] + v[i] ) ;
				}
			}
		}
	}
	
	for( int i=0 ; i<W ; i++ ){
		if( ans1 < dp[N][i] ){
			ans1 = dp[N][i] ;
			ans2 = i ;
		}
	}
}

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

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

#define  req(i,r,k) for(int i=k;i<r;i++)
#define  rep(i,r) req(i,r,0)

const long long int INF = 100000000000000;

long long int dp[1001][1001];

int main() {

    for(int a=1;1;a++){
        int w,n;
        scanf(" %d",&w);
        if(w==0) break;
        scanf(" %d",&n);
        pair<int,int> t[n];
        char c;
        rep(i,n){
            scanf(" %d%c%d",&t[i].first,&c,&t[i].second);
        }

        rep(i,w){
            dp[0][i] = 0;
            dp[i][0] = 0;
        }

        dp[0][0] = 0;
        rep(i,n+1) {
            rep(j,w+1) {
                if(j >= t[i].second) {
                    dp[i+1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first);
                }
                else{
                    dp[i+1][j] = dp[i][j];
                }
            }
        }
        long long int maxvalue=0,thenweight=0;

        rep(i,w+1){
            //cout << dp[i] << " " << i << " " << thenweight << endl;
            if(maxvalue < dp[n][i]){
                maxvalue = dp[n][i];
                thenweight = i;
            }
        }

        //printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight);
        cout << "Case " << a << ':' << endl;
        cout << maxvalue << endl;
        cout << thenweight << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;


int w;
int num;
int caseN;

int *dataV,*dataW;
int maxV,maxW;

void calc(int n,int sumV,int sumW){
	if(n==num){
		if(maxV<=sumV){
			maxV=sumV;
			maxW=sumW;
		}
		return;
	}
	if(sumW>w){
		return;
	}
	else{
		calc(n+1,sumV,sumW);
		calc(n+1,sumV+dataV[n],sumW+dataW[n]);
	}

}

int main(){

	char tmp;
	caseN=0;

	while(cin>>w){
		if(w==0){
			 break;
		}
		cin>>num;
		dataV = new int(num);
		dataW = new int(num);
		maxV=0;
		maxW=0;
		
		for(int i=0;i<num;i++){
			cin>>dataV[i];
			cin>>tmp;
			cin>>dataW[i];
		}
		calc(0,0,0);

		cout<<"Case "<<caseN+1<<":"<<endl;
		cout<<maxV<<endl;
		cout<<maxW<<endl;

		caseN++;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

int dp[1001][1001];
int v[1001], w[1001];
int main() {
	int W, N;
	int cnt = 1;
	while (scanf("%d", &W), W) {
		scanf("%d", &N);
		rep(i, N)scanf("%d,%d", &v[i], &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 Max = 0, Min = 0;
		for (int i = 0; i <= W; i++) {
			if (Max < dp[N][i]) { Max = dp[N][i]; Min = i; }
		}
		printf("Case %d:\n%d\n%d\n", cnt, Max, Min);
		cnt++;
	}
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
	int dp[1001][1001];
	int n,W;
	int v[1000],w[1000];
	int num=0;
	while(cin>>W,W)
	{
		num++;
		cin>>n;
		memset(dp,sizeof(dp),0);
		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(j<w[i-1]) dp[i][j]=dp[i-1][j];
				else dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i-1]]+v[i-1]);
			}
		}
		
		int mv=0,mw;
		for(int j = 0; j <= W ; ++j)
		{
			if(mv<dp[n][j]){ mv=dp[n][j]; mw=j;}
		}
		cout<<"Case "<<num<<":"<<endl;
		cout<<mv<<endl;
		cout<<mw<<endl;
	}
  return 0;
}