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 <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <utility>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int W,N;
const int MAX_N = 1001;
const int MAX_W = 1001;
int v[MAX_N];
int w[MAX_N];
vector<pii> t;
int dp[MAX_N][MAX_W];

void solve() {
	memset(dp, 0, sizeof(dp));
	repa(i,1,N) {
		repa(j,0,W) {
			if(j + w[i] < W) {
				rep(k,i) {
					dp[i][j + w[i]] = max(dp[i][j + w[k]], dp[k][j] + v[i]);
				}
			}
		}
	}

	int maxval = -1;
	int weight = 0;
	repa(i,0,W) {
		if(maxval < dp[N][i]) {
			maxval = dp[N][i];
			weight = i;
		}
	}
	cout <<	maxval << endl;
	cout << weight << endl;
}

int main() {
	int c = 0;
	while(cin>>W, W) {
		cin >> N;
		rep(i,N) {
			scanf("%d,%d", &v[i], &w[i]);
		}

		printf("Case %d:\n", ++c);
		solve();
	}
}

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

int main() {
  int W, n;
  for (int Case = 1; cin >> W, W; ++Case) {
    cin >> n;
    vector<int> v(n), w(n);
    for (int i = 0; i < n; ++i) {
      scanf("%d,%d", &v[i], &w[i]);
    }

    vector<int> d(W + 1, -1);
    d[0] = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = W - w[i]; j >= 0; --j) {
        if (d[j] == -1) continue;
        d[j + w[i]] = max(d[j + w[i]], d[j] + v[i]);
      }
    }

    int ans_v = 0, ans_w = 0;
    for (int i = 0; i <= W; ++i) {
      if (ans_v < d[i]) {
        ans_v = d[i];
        ans_w = i;
      }
    }

    cout << "Case " << Case << ":" << endl;
    cout << ans_v << endl;
    cout << ans_w << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int dp[1005][1005];

int main()
{
//	cut here before submit 
//	freopen ("testcase.A_Thief", "r", stdin );
	int cnt = 0;
	while (1 ){
		int W;
		scanf ("%d", &W );
		if (W == 0 ) break;
		int N;
		scanf ("%d", &N );
		vector<int> v (N, 0 ), w (N, 0 );
		rep (i, N ){
			string str;
			cin >> str;
			while (str.find(',') != string::npos ){
				str = str.replace(str.find(','), 1, " ");
			} // end while
			stringstream ss (str );
			ss >> v[i] >> w[i]; 
		} // end rep
/*
		rep (i, N ){
			cerr << "v: " << v[i] << " w: " << w[i] << endl;
		} // end rep
*/
		memset (dp, 0, sizeof (dp ) );
		dp[0][w[0]] = v[0];
		for (int i = 1; i < N; i++ ){
			dp[i][w[i]] = v[i];
			for (int j = 0; j <= W; j++ ){
				if (dp[i-1][j] != 0 ){
					dp[i][j] = max (dp[i][j], dp[i-1][j] );
					if (j + w[i] <= W ){
						dp[i][j + w[i]] = max (dp[i][j + w[i]], dp[i-1][j] + v[i] );
					} // end if
				} // end if
			} // end for
		} // end for
/*
		rep (i, N ){
			rep (j, W ){
				cerr << setw(3) << dp[i][j];
			} // end rep
			cerr << endl;
		} // end rep
*/
		int res_v = 0, res_w = 0;
		for (int j = 0; j <= W; j++ ){
			if (dp[N-1][j] > res_v ){
				res_v = dp[N-1][j];
				res_w = j;
			} // end if
		} // end for
		printf ("Case %d:\n", ++cnt );
		printf ("%d\n", res_v );
		printf ("%d\n", res_w );
	} // end loop

//	printf ("%d\n", res );
		
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/


void solve(int W,int N){
    vector<int> v(N),w(N);
    char c;
    rep(i,N) cin>>v[i]>>c>>w[i];

    vector<vector<int>> dp(N+10,vector<int>(W+10,0));
    for(int i=0;i<N;i++){
        // use v[i],w[i]
        for(int j=0;j<=W;j++){
            if(j+w[i]<=W){
                dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]],dp[i][j]+v[i]);
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }else{
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }
        }
    }

    int mx=0;
    for(int i=0;i<=N;i++) for(int j=0;j<=W;j++) mx = max(mx,dp[i][j]);
    assert(mx==dp[N][W]);

    for(int i=0;i<=W;i++) if(dp[N][i]==dp[N][W]){
        printf("%d\n%d\n",dp[N][W],i);
        return;
    }
    assert(false);
}

int main(){
    int w,n;
    int cnt=0;
    while(1){
        cin>>w;
        if(w==0) break;
        cin>>n;
        printf("case %d:\n",++cnt);
        solve(w,n);
    }
}