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 <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) { char c;for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> c >> 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 w, n;

// index, weight
int dp[1010][1010];
PArray treasure;

int main(void)
{
	ios::sync_with_stdio(false);
	int count = 1;
	while(cin >> w && w)
	{
		n = getInt();
		for (int i = 0; i <= n; i++)
		{
			memset(dp[i], 0, sizeof(dp[i]));
		}
		treasure.resize(n);
		readAll(treasure);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				int nw = j + treasure[i].second;
				if(nw > w) continue;
				dp[i + 1][nw] = max(dp[i + 1][nw], dp[i][j] + treasure[i].first);
			}
		}
		P ans = P(-1, INF);
		for (int i = 0; i <= w; i++)
		{
			if(dp[n][i] > ans.first)
			{
				ans = P(dp[n][i], i);
			}
		}

		cout << "Case " << count << ':' << endl;
		cout << ans.first << endl << ans.second << endl;
		count++;
	}
	return 0;
}

C++: #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std ;

int N , W ;
int v[1001] , w[1001] ;
int dp[1002][1002] ;
int ans1 , ans2 ;

void solve(){
	memset(dp,-1,sizeof(dp)) ;
	dp[0][0] = 0 ;
	ans1 = 0 ;
	ans2 = 0 ;
	
	for( int i=1 ; i<=N ; i++ ){
		for( int j=0 ; j<=W ; j++ ){
			if( dp[i-1][j] >= 0 ){
				if( dp[i][j] < dp[i-1][j] ) dp[i][j] = dp[i-1][j] ;
				if( j+w[i] < W ){
					dp[i][j+w[i]] = max( dp[i-1][j+w[i]] ,dp[i-1][j] + v[i] ) ;
				}
			}
		}
	}
	
	for( int i=0 ; i<W ; i++ ){
		if( ans1 < dp[N][i] ){
			ans1 = dp[N][i] ;
			ans2 = i ;
		}
	}
}

int main(){
	
	int t=0 ;
	
	while( cin >> W >> N , W ){
		for( int i=1 ; i<N ; i++ ){
			scanf("%d,%d" ,&v[i] ,&w[i] ) ;
		}t++ ;
		solve() ;
		
		cout << "Case " << t << ':' << endl ;
		cout << ans1 << endl ;
		cout << ans2 << endl ;
	}
	
return 0 ; 
}

C++: #include <bits/stdc++.h>
using namespace std;

#define  req(i,r,k) for(int i=k;i<r;i++)
#define  rep(i,r) req(i,r,0)

const long long int INF = 100000000000000;

long long int dp[1001][1001];

int main() {

    for(int a=1;1;a++){
        int w,n;
        scanf(" %d",&w);
        if(w==0) break;
        scanf(" %d",&n);
        pair<int,int> t[n];
        char c;
        rep(i,n){
            scanf(" %d%c%d",&t[i].first,&c,&t[i].second);
        }

        rep(i,w){
            dp[0][i] = 0;
            dp[i][0] = 0;
        }

        dp[0][0] = 0;
        rep(i,n+1) {
            rep(j,w+1) {
                if(j >= t[i].second) {
                    dp[i+1][j] = max(dp[i][j], dp[i][j - t[i].second] + t[i].first);
                }
                else{
                    dp[i+1][j] = dp[i][j];
                }
            }
        }
        long long int maxvalue=0,thenweight=0;

        rep(i,w+1){
            //cout << dp[i] << " " << i << " " << thenweight << endl;
            if(maxvalue < dp[n][i]){
                maxvalue = dp[n][i];
                thenweight = i;
            }
        }

        //printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight);
        cout << "Case " << a << ':' << endl;
        cout << maxvalue << endl;
        cout << thenweight << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;


int w;
int num;
int caseN;

int *dataV,*dataW;
int maxV,maxW;

void calc(int n,int sumV,int sumW){
	if(n==num){
		if(maxV<=sumV){
			maxV=sumV;
			maxW=sumW;
		}
		return;
	}
	if(sumW>w){
		return;
	}
	else{
		calc(n+1,sumV,sumW);
		calc(n+1,sumV+dataV[n],sumW+dataW[n]);
	}

}

int main(){

	char tmp;
	caseN=0;

	while(cin>>w){
		if(w==0){
			 break;
		}
		cin>>num;
		dataV = new int(num);
		dataW = new int(num);
		maxV=0;
		maxW=0;
		
		for(int i=0;i<num;i++){
			cin>>dataV[i];
			cin>>tmp;
			cin>>dataW[i];
		}
		calc(0,0,0);

		cout<<"Case "<<caseN+1<<":"<<endl;
		cout<<maxV<<endl;
		cout<<maxW<<endl;

		caseN++;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int, int>P;

int dp[1001][1001];
int v[1001], w[1001];
int main() {
	int W, N;
	int cnt = 1;
	while (scanf("%d", &W), W) {
		scanf("%d", &N);
		rep(i, N)scanf("%d,%d", &v[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]] + v[i]);
			}
		}
		int Max = 0, Min = 0;
		for (int i = 0; i <= W; i++) {
			if (Max < dp[N][i]) { Max = dp[N][i]; Min = i; }
		}
		printf("Case %d:\n%d\n%d\n", cnt, Max, Min);
		cnt++;
	}
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
	int dp[1001][1001];
	int n,W;
	int v[1000],w[1000];
	int num=0;
	while(cin>>W,W)
	{
		num++;
		cin>>n;
		memset(dp,sizeof(dp),0);
		for(int i = 0; i < n; ++i)
		{
			scanf("%d,%d",&v[i],&w[i]);
		}
		for(int i = 1; i <= n; ++i)
		{
			for(int j = 1; j <= W; ++j)
			{
				if(j<w[i-1]) dp[i][j]=dp[i-1][j];
				else dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i-1]]+v[i-1]);
			}
		}
		
		int mv=0,mw;
		for(int j = 0; j <= W ; ++j)
		{
			if(mv<dp[n][j]){ mv=dp[n][j]; mw=j;}
		}
		cout<<"Case "<<num<<":"<<endl;
		cout<<mv<<endl;
		cout<<mw<<endl;
	}
  return 0;
}