Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	Case := 1
	for {
		scanner.Scan()
		W, _ := strconv.Atoi(scanner.Text())
		if W == 0 {
			break
		}
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		w := make([]int, n+1)
		v := make([]int, n+1)
		for i := 1; i <= n; i++ {
			scanner.Scan()
			info := strings.Split(scanner.Text(), ",")
			v[i], _ = strconv.Atoi(info[0])
			w[i], _ = strconv.Atoi(info[1])
		}
		// fmt.Println(v)
		// fmt.Println(w)
		dp := make([][]int, n+1)
		for i := range dp {
			dp[i] = make([]int, W+1)
		}
		maxV := -(1 << 21)
		minW := 1 << 21
		for i := 1; i <= n; i++ {
			for j := 1; j <= W; j++ {
				// fmt.Println(dp)
				if j-w[i] >= 0 {
					dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i])
					if maxV < dp[i][j] {
						maxV = dp[i][j]
						minW = j
					} else if maxV == dp[i][j] {
						minW = min(minW, j)
					}
				} else {
					dp[i][j] = dp[i-1][j]
				}
			}
		}
		fmt.Printf("Case %d:\n", Case)
		Case += 1
		fmt.Println(maxV)
		fmt.Println(minW)
	}
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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;
}