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