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 <map>

using namespace std;

typedef pair<unsigned long long int, unsigned long long int> P;

P plus_(P a, P b) {
    return P(a.first+b.first, a.second+b.second);
}

int main() {
    int w, n;
    int value[1000], weight[1000];
    P dp[2][1000];
    for (int case_num=1; true; case_num++) {
        cin >> w;
        if (w == 0) {
            return 0;
        }
        cin >> n;
        for (int i=0; i<n; i++) {
            cin >> value[i];
            cin.ignore(1);
            cin >> weight[i];
        }
        for (int i=0; i<=w; i++) {
            dp[0][i] = P(0, 0);
        }
        for (int i=1; i<=n; i++) {
            for (int j=0; j<=w; j++) {
                if (j < weight[i-1]) {
                    dp[i%2][j] = dp[(i-1)%2][j];
                }
                else {
                    P a = plus_(dp[(i-1)%2][j-weight[i-1]], P(value[i-1], -weight[i-1]));
                    P b = dp[(i-2)%2][j];
                    dp[i%2][j] = max(a, b);
                }
            }
        }
        cout << "Case " << case_num << ":" << endl;
        cout << dp[n%2][w].first << endl;
        cout << -dp[n%2][w].second << endl;
    }
}

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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXW = 1000, MAXN = 1000;

int dp[MAXN + 1][MAXW + 10];
int w[MAXN + 1];
int v[MAXN + 1];

int main()
{
    int wl, n, casen = 1;
    char t;
    while(cin >> wl && wl)
    {
        cin >> n; REP(n) cin >> v[i] >> t >> w[i];

        memset(dp, 0, sizeof(dp));
        
        REP(n)rep(j, wl+1 - w[i])
            dp[i+1][j + w[i]] = max(dp[i][j + w[i]], dp[i][j] + v[i]);
        
        int answ = 0, ansv = -1;
        REP(wl + 1)if(ansv < dp[n][i]){ ansv = dp[n][i]; answ = i;}
        cout << "Case " << casen << ":"<< endl;
        cout << ansv << endl;
        cout << answ << endl;
        casen++;
    }

    return 0;
}

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

struct pre{ int v, w; };

int max(int a, int b){
	return (a > b) a : b;
}

int main(){
	const int MAX_N = 1000, MAX_W = 1000;
	int N, W;
	pre dat[MAX_N];
	int dp[MAX_N][MAX_W + 1];
	int p;
	
	for(int n = 1;; n++){
		scanf("%d", &W);
		if(W == 0) break;
		scanf("%d", &N);
		for(int i = 0; i < N; i++) scanf("%d,%d", &dat[i].v, &dat[i].w);
		
		for(int i = 0; i < MAX_N; i++){
			for(int j = 0; j < MAX_W; j++){
				dp[i][j] = 0;
			}
		}
		
		for(int i = dat[0].w; i <= W; i++) dp[0][i] = dat[0].v;
		for(int i = 1; i < N; i++){
			for(int j = 0; j <= W; j++){
				dp[i][j] = max((dat[i].w <= j)? dp[i - 1][j - dat[i].w] + dat[i].v : 0, dp[i - 1][j]);
			}
		}
		
		for(int i = 0; i <= W; i++){
			if(dp[N - 1][i] == dp[N - 1][W]){
				printf("Case %d:\n%d\n%d\n", n, dp[N - 1][W], i);
				break;
			}
		}
	}
	
	return 0;
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int W;
  int caseIndex = 0;
  while (cin >> W, W) {
    caseIndex++;
    int N;
    cin >> N;
    vi v(N), w(N);
    char gomi;
    rep(i, N) cin >> v[i] >> gomi >> w[i];
    vvi dp(N + 1, vi(W + 1));
    rep(i, N) rep(j, W + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j >= w[i]) cmax(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
    }
    int maxi = dp[N][W];
    rep(i, W + 1) if (dp[N][i] == maxi) {
      cout << "Case " << caseIndex << ":" << endl;
      cout << maxi << endl;
      cout << i << endl;
      break;
    }
  }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iomanip>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define INF 100000000
#define _INF -100000000
#define INFLL (long long)1e14
#define _INFLL (long long)-1e14
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
struct dat { double x, y, arg; };
#pragma warning disable 4997

void knapsack(int n, int m, vi v, vi w) {
	vvi dp(n + 1, vi(m + 1, 0));
	Loop1(i, n) {
		Loop1(j, m) {
			if (j - w[i] < 0) dp[i][j] = dp[i - 1][j];
			else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
		}
	}
	int buf = dp[n][m];
	Loopr(j, m) {
		if (dp[n][j] < buf) {
			cout << dp[n][j + 1] << endl;
			cout << j + 1 << endl;
			break;
		}
	}
	return;
}

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