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 <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int n, w;
const int M_W = 1001;
const int M_N = 1001;
P dp[M_N][M_W];
PArray arr;

P dfs(int idx, int weight)
{
	if(idx == n) return P(0, weight);
	if(dp[idx][weight].first) return dp[idx][weight];

	P v1 = P(-1, weight + arr[idx].second);
	if(v1.second <= w)
	{
		P ret = dfs(idx + 1, weight + arr[idx].second);
		v1.first = ret.first + arr[idx].first;
		v1.second = ret.second;
	}
	P v2 = dfs(idx + 1, weight);
	P ans;
	if(v1.first == v2.first)
	{
		ans.first = v1.first;
		ans.second = min(v1.second, v2.second);
	}
	else ans = max(v1, v2);
	return ans;
}

int main(void)
{
	ios::sync_with_stdio(false);
	int counter = 0;
	while(cin >> w && w)
	{
		counter++;
		cin >> n;
		arr.resize(n);
		char c;
		for (int i = 0; i < n; i++)
		{
			cin >> arr[i].first >> c >> arr[i].second;
		}
		memset(dp, 0, sizeof(dp));
		P ans = dfs(0, 0);
		
		cout << "Case " << counter << ":" << endl;
		cout << ans.first << endl;
		cout << ans.second << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[1001][1001],wait[1001],value[1001],dpp[1001][10000];
void Initialization()
{
    for(int i=0;i<1001;i++)for(int j=0;j<1001;j++)dp[i][j]=-1;
    for(int i=0;i<1001;i++)for(int j=0;j<10000;j++)dpp[i][j]=-1;
}
int power(int n)
{
    int num=1;
    for(int i=1;i<=n;i++)num*=10;
    return num;
}
int serch(int i,int j,int n)
{
    int num;
    if(dp[i][j]>-1)return dp[i][j];
    else if(i==n)num=0;
    else if(j<wait[i])num=serch(i+1,j,n);
    else num=max(serch(i+1,j,n),serch(i+1,j-wait[i],n)+value[i]);
    return dp[i][j]=num;
}
int wserch(int i,int va,int ser,int n,int w)
{
    int num;
    if(dpp[i][va]>-1)return dpp[i][va];
    else if(i==n)num=w;
    else if(ser<va+value[i])num=wserch(i+1,va,ser,n,w);
    else if(ser==va+value[i])num=wait[i];
    else num=min(wserch(i+1,va,ser,n,w),wserch(i+1,va+value[i],ser,n,w)+wait[i]);
    return dpp[i][va]=num;
}
int main()
{
    int n,w,point,num=1,wai,wa;
    string str;
    while(cin>>w&&w!=0){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>str;
            value[i]=0;wait[i]=0;
            for(int j=0;;j++){
                if(str[j]==','){
                    for(int k=0;k<j;k++)value[i]+=power(j-k-1)*(str[k]-'0');
                    point=j;
                }
                if(str[j]=='\0'){
                    for(int k=point+1;k<j;k++)wait[i]+=power(j-k-1)*(str[k]-'0');
                    break;
                }
            }
        }
        Initialization();
        point=serch(0,w,n);
        wai=w;
        for(int i=0;i<n;i++){
            if(value[i]<point){
                wa=wserch(i+1,value[i],point,n,w)+wait[i];
                if(wa<wai)wai=wa;
            }
            else if(value[i]==point&&wai>wait[i])wai=wait[i];
        }
        cout<<"Case "<<num<<":\n"<<point<<endl<<wai<<endl;
        num++;
    }
}

C++: #include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <algorithm>
#include <numeric>
#include <iostream>

using namespace std;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

int W,N;
int c[1000],w[1000];
int t[10000+10];

int main()
{
	for (int i = 1; ; i++){
		cin>>W;
		if(W == 0) break;
		if(i != 1) puts("");
		cin>>N;
		for(int j =0 ; j < N; j++) scanf("%d,%d",c+j,w+j);
		printf("Case%d:\n",i);
		memset(t,-1,sizeof(t));
		t[0] = 0;
		for (int i = 0; i < N; i++){
			for (int j = W - w[i]; j >= 0; j--){
				if(t[j] == -1) continue;
				t[j+w[i]] = max(t[j+w[i]],t[j] + c[i]);
			}
		}
		int ans_id = max_element(t,t+W+1) - t;
		printf("%d\n%d",t[ans_id],ans_id);
	}
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
int dp[1010][1010] = {{0}};
int W,n;
int w[1010] = {0},p[1010] = {0};
int cnt = 0;
int wm = 0,nc;
int main()
{
    while(1){
        cin >> W;
        if(W == 0) break;
        cnt++;
        cin >> n;
        for(int i = 0; i < n; i++) scanf("%d,%d",&p[i],&w[i]);
        for(int i = 0; i < n; i++){
            for(int j = 0; j <= W; j++){
                if(j < w[i]) dp[i+1][j] = dp[i][j];
                else dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]]+p[i]);
            }
        }
        nc = W;
        while(nc >= 0){
            if(dp[n][nc] == dp[n][W])
                wm = nc;
            nc--;
        }
        cout << "Case " << cnt << ':' << endl << dp[n][W] << endl << wm << endl;
    }
	return 0;
}