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 <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w,n;
  int cnt = 1;
  char c;
  while(cin >> w){
    if(w == 0) break;
    cin >> n;
    vary(int,vs,n,0);
    vary(int,ws,n,0);
    int dp[1001][1001] ={};
//     vector<vector<int>> dp2(n+1,vector<int>(w+1,0));
    rep(i,n){
      cin >> vs[i] >> c >> ws[i];
    }
    REP(i,0,n){
      rep(j,w){
        dp[i+1][j] = max(dp[i][j], j-ws[i] >= 0? dp[i][j-ws[i]]+ vs[i] : 0);
      }
    }
    cout << "Case " + to_string(cnt) + ":"<<endl;
    rep(i,w+1){
      if(dp[n][i] == dp[n][w-1]){
        cout << dp[n][w-1]<<endl<<i<<endl;
        break;
      }
    }
    ++cnt;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

class Knapsack {
    struct item { 
        long long v, w; 
    };

    long long W;
    vector<item> items;
    long long lv;
    long long lv_w;

    pair<long long, long long> solve_rec(size_t k, long long v, long long w) {
        if (w + items[k].w > W) return solve_rec(k+1, v, w);
        long long cv = v, cw = w;
        for (size_t i = k; i < items.size(); ++i) {
            if (cw + items[i].w <= W) {
                cw += items[i].w;
                cv += items[i].v;
            }
        }
        if (lv < cv || (lv == cv && lv_w > cw)){
            lv = cv;
            lv_w = cw;
        }
        double fv = v, fw = w;
        for (size_t i = k; i < items.size(); ++i) {
            if (fw + items[i].w <= W) {
                fw += items[i].w;
                fv += items[i].v;
            } else {
                fv += items[i].v * (W - fw) / items[i].w;
                break;
            }
        }
        if (fv - lv < 1 || fv < lv) return make_pair(lv, lv_w);
        solve_rec(k+1, v+items[k].v, w+items[k].w);
        return solve_rec(k+1, v, w);
    }

    public:
    Knapsack() {}
    Knapsack(long long W_) : W(W_) {}
    void add_item(long long v, long long w) {
        items.push_back({v, w});
    }
    pair<long long, long long> solve() {
        sort(items.begin(), items.end(), [](const item &a, const item &b) { 
                return a.v * b.w > a.w * b.v;
                });
        lv = 0;
        return solve_rec(0, 0, 0);
    };
};

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int main(void){
    int W,N,v[1001],w[1001];
    int cas = 1;
    while(cin>>W){
        if(W==0) break;
        printf("Case %d:\n",cas++);
        Knapsack solver(W);
        cin>>N;
        REP(i,N){
            scanf("%d,%d",&v[i],&w[i]);
            solver.add_item(v[i], w[i]);
        }
        pair<long long, long long> p = solver.solve();
        printf("%lld\n%lld\n", p.first, p.second);
    }
    return 0;
}

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

int W, N;

struct T {
    int v, w;
    T(int value, int weight) {
        v = value, w = weight;
    }
    bool operator<(const T &o) const {
        return (w > o.w);
    }
};

vector<T> ts;

void init() {
    ts.clear();
    W = N = 0;
}

bool get_input() {
    init();
    cin >> W;
    if (W == 0) return false;
    cin >> N;
    int value, weight;
    char dummy;
    for (int i = 0; i < N; i++) {
        cin >> value >> dummy >> weight;
        ts.push_back(T(value, weight));
    }
    sort(ts.begin(), ts.end());
    return true;
}

T vmax(T t1, T t2) {
    if (t1.v == t2.v) {
        if (t1.w > t2.w) {
            return t2;
        } else {
            return t1;
        }
    } else if (t1.v > t2.v) {
        return t1;
    } else {
        return t2;
    }
}

T solve(T t, int ptr) {
    if (ptr == ts.size()) return t;
    if (ts[ptr].w + t.w > 50) 
        return solve(t, ptr+1);
    else
        return vmax(solve(t, ptr+1), solve(T(t.v + ts[ptr].v, t.w + ts[ptr].w), ptr+1));
}

void format(T t, int data_set_n) {
    cout << "Case " << data_set_n << ':' << endl;
    cout << t.v << endl;
    cout << t.w << endl;
}


int main() {
    int dn = 1;
    while (get_input()) {
        format(solve(T(0, 0), 0), dn++);
    }
    return 0;
}

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

#define MAX 1000

int dp[MAX + 1][MAX + 1];
int v[MAX], w[MAX];

int main() {
	int W, n, u = 0;
	char t;
	while (cin >> W) {
		if (W == 0) break;
		cin >> n;
		u++;

		dp[MAX + 1][MAX + 1] = { 0 };
		for (int i = 0; i < n; i++) {
			cin >> v[i] >> t >> w[i];
		}

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

		int mv = 0, mw;
		for (int i = 0; i <= W; i++) {
			if (dp[n][i] > mv) {
				mv = dp[n][i];
				mw = i;
			}
		}

		cout << "Case " << u << ":" << endl;
		cout << mv << endl;
		cout << mw << endl;
	}
	return 0;
}