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 <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <bitset>
using namespace std;

class info
{
public:
	int value;
	vector <bool> visited;

	info()
	{
		value = 0;
		visited = vector <bool>( 1001, false );
	}
};

int main( void )
{
	int case_no = 1;
	int w_max;
	while ( cin >> w_max && w_max )
	{
		cout << "Case " << case_no << ":" << endl;

		// Â
		int n;
		cin >> n;

		// îñ
		vector <int> v( n );
		vector <int> w( n );

		// üÍ
		for ( int i = 0; i < n; i++ )
		{
			string in;
			cin >> in;
			replace( in.begin(), in.end(), ',', ' ' );

			int vi, wi;
			istringstream is( in );
			is >> vi >> wi;

			v[i] = vi;
			w[i] = wi;
		}

		// sort
		for ( int i = 0; i < n-1; i++ )
		{
			for ( int j = i+1; j < n; j++ )
			{
				if ( w[i] > w[j] )
				{
					swap( w[i], w[j] );
					swap( v[i], v[j] );
				}
				else if ( w[i] == w[j] )
				{
					if ( v[i] < v[j] )
					{
						swap( w[i], w[j] );
						swap( v[i], v[j] );
					}
				}
			}
		}

		vector <info> dp( w_max + 1 );

		for ( int i = 0; i < n; i++ )
		{
			int& wi = w[i];
			int& vi = v[i];

			if ( dp[wi].value < vi )
			{
				dp[wi].value = vi;
				dp[wi].visited[i] = true;
			}
		}

		for ( int i = 0; i <= w_max; i++ )
		{
			if ( dp[i].value <= 0 )
				continue;

			for ( int j = 0; j < n; j++ )
			{
				int& wj = w[j];
				int& vj = v[j];

				if ( dp[i].visited[j] )
					continue;

				if ( i + wj > w_max )
					continue;

				if ( dp[i+wj].value == 0 )
					dp[i+wj].value		= dp[i].value + vj;
				else
				{
					dp[i+wj].value = max( dp[i+wj].value, dp[i].value + vj );
				}
				dp[i+wj].visited	= dp[i].visited;
				dp[i+wj].visited[j]	= true;
			}
		}

		int maxw = 0;
		int maxv = 0;
		for ( int i = 0; i <= w_max; i++ )
		{
			if ( dp[i].value <= 0 )
				continue;

			if ( maxv < dp[i].value )
			{
				maxw = i;
				maxv = dp[i].value;
			}
		}

		cout << maxv << endl;
		cout << maxw << endl;

		case_no ++;
	}

	return 0;
}

C++: #include <iostream>
#include <utility>
#include <vector>
#include <cstring>
using namespace std;
const int MAX_N = 1000,MAX_M = 10000;
int n;
typedef pair<int,int> P;
P ilist[MAX_M];
P memo[MAX_N + 1][MAX_M + 1];

P comp(const P &a,const P &b){
	if(a.first < b.first) return b;
	return a;
}

P dp(int now,int lim){
	if(memo[now][lim].first > -1){
		return memo[now][lim];
	}
	P res;
	if(now == n) return make_pair(0,0);
	else if(lim < ilist[now].second){
		res = dp(now + 1,lim);
	}
	else{
		res = dp(now + 1,lim - ilist[now].second);
		res.second += ilist[now].second;
		res.first += ilist[now].first;
		res = comp(dp(now + 1,lim),res);
	}
	return memo[now][lim] = res;
}

int main(){
	int lim,cnt = 0,w,v;
	P item;
	char buf;
	while(cin >> lim,lim){
		for(int i = 0;i <= MAX_N;++i){
			for(int j = 0;j <= MAX_M;++j){
				memo[i][j] = make_pair(-1,-1);
			}
		}
		++cnt;
		cin >> n;
		for(int i = 0;i < n;++i){
			cin >> v >> buf >> w;
			ilist[i] = make_pair(v,w);
		}
		item = dp(0,lim);
		cout << "Case " << cnt << ":" << endl;
		cout << item.first << endl << item.second << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define MAX 1024

int main()
{
    int W, N, T = 1;
    while (cin >> W, W) {
        cin >> N;
        char c;
        vector<int> v(N), w(N);
        for (int i = 0; i < N; i++) {
            cin >> v[i] >> c >> w[i];
        }

        int dp[MAX][MAX] = {{}};
        for (int i = 0; i < N; i++) {
            for (int j = 0; j <= W; j++) {
                if (j - w[i] < 0) {
                    dp[i+1][j] = dp[i][j];
                } else {
                    dp[i+1][j] = max(dp[i+1][j], dp[i][j-w[i]] + v[i]);
                }
            }
        }
        
        int max_v = *max_element(dp[N], dp[N] + W), min_w = -1;
        for (int i = W; i >= 0; i--) {
            if (dp[N][i] == max_v) {
                min_w = i;
            }
        }        
        cout << "Case " << T++ << ":" << endl;
        cout << max_v << endl;
        cout << min_w << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int w, cut = 1;
    while(cin >> w, w){
        int n;
        cin >> n;
        vector<int> dp(w+1, -INF<int>);
        dp[0] = 0;
        for (int i = 0; i < n; ++i) {
            int a, b;
            scanf("%d,%d", &a, &b);
            for (int j = w-b; j >= 0; --j) {
                dp[j+b] = max(dp[j+b], dp[j]+a);
            }
        }
        auto ans = max_element(dp.begin(),dp.end());
        printf("Case %d:\n%d\n%ld\n", cut++, *ans, ans-dp.begin());
    }
    return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int sum_w[1001][1001];
int sum_v[1001][1001];
void vmax(int i,int j,int v[],int w[]){
	if(i==0){
		if(w[i]<=j){
			sum_v[i][j]+=v[i];
			sum_w[i][j]+=w[i];
		}
	}else if(j>=w[i]){
		if(sum_v[i-1][j]<sum_v[i-1][j-w[i]]+v[i]){
			sum_v[i][j]=sum_v[i-1][j-w[i]]+v[i];
			sum_w[i][j]=sum_w[i-1][j-w[i]]+w[i];
		}else{
			sum_v[i][j]=sum_v[i-1][j];
			sum_w[i][j]=sum_w[i-1][j];
		}
	}else{
		sum_v[i][j]=sum_v[i-1][j];
		sum_w[i][j]=sum_w[i-1][j];
	}
}
int main(){
    int count=0;
    while(1){
        int i,j,lim=0,N=0,v[1001],w[1001],k=0,l=0;
        count++;
		for(i=0;i<1001;i++){
			for(j=0;j<1001;j++){
				sum_v[i][j]=0;
				sum_w[i][j]=0;
			}
		}
		cin>>lim;
        if(lim==0)break;
        cin>>N;
        for(i=0;i<N;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        for(i=0;i<N;i++){
            for(j=0;j<=lim;j++){
                vmax(i,j,v,w);
            }
        }
        for(i=0;i<=lim;i++){
            if(k<sum_v[N-1][i]){
                k=sum_v[N-1][i];
                l=i;
            }
        }
        cout<<"Case "<<count<<":"<<endl<<sum_v[N-1][l]<<endl<<sum_w[N-1][l]<<endl;
    }
    return 0;
}