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 <vector>
#include <algorithm>
using namespace std;
#define MAX 1001
typedef pair<int,int> P;

int dp[MAX][MAX],t,w,m;

void judge(vector<P> v){
    
    for(int i = 1 ; i <= m ; i++){
        for(int j = 1 ; j <= w ; j++){
            int k = j - v[i-1].first;
            if(k < 0){
                dp[i][j] = dp[i-1][j];
            }
            else{
                dp[i][j] = max(dp[i-1][j],dp[i-1][k]+v[i-1].second);
            }
        }
    }
    int ans_W=0,ans_C;
    for(int i = 0 ; i <= w ; i++){
        ans_W = max(ans_W,dp[m][i]);
    }
    for(int i = w ; i >= 0 ; i--){
        if(ans_W==dp[m][i]) ans_C = i;
    }
    
    cout << ans_W << endl;
    cout << ans_C << endl;
}

void print(){
    cout << "Case " << t++ << ":" << endl;
}

int main(){
    int val,wei;
    string ch;
    
    t = 1;
    while(cin >> w,w){
        cin >> m;
        vector<P> v;
        
        fill(dp[0],dp[MAX-1],0);
        
        print();
        for(int i = 1 ; i <= m ; i++){
            cin >> val >> ch >> wei;
            v.push_back(P(wei,val));
        }
        sort(v.begin(),v.end());
        judge(v);
    }
    
    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 <complex>

#include <stack>

#include <queue>

#include <numeric>

#include <cstring>



#define rep(i, n) for(int i = 0; i < (n); i++)

#define pb push_back



using namespace std;



typedef long long ll;

typedef pair<int, int> P;

typedef vector<int> vi;

typedef vector<vi> vvi;



const int INF = 1 << 29;

const double EPS = 1e-9;

const int MAX_N = 1000;

const int MAX_W = 1000;



int dp[MAX_N][MAX_W];

int dpw[MAX_N][MAX_W];

//int dpw[MAX_N + 1];



int main(){

	int n, cap, ans_c = 0;

	int v[MAX_N], w[MAX_N];

	

	while(1){

		memset(dp, 0 , sizeof(dp));

		memset(dpw, 0, sizeof(dpw));

		scanf("%d", &cap);

		if(cap == 0) break;

		scanf("%d", &n);



//		int dpw[n];

//		memset(dpw, 0, sizeof(dpw));



		rep(i, n){

			scanf("%d,%d\n",&v[i],&w[i]);

		}



		for(int i = 0; i < n; i++){

			for(int j = 0; j <= cap; j++){

				if(j < w[i]){

					dp[i+1][j] = dp[i][j];

					dpw[i+1][j] = dpw[i][j];

//					dpw[i+1] = dpw[i];

				}else{

					dp[i+1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);

					if(dp[i+1][j] == dp[i][j - w[i]] +v[i]){

						dpw[i+1][j] = dpw[i][j-w[i]] + w[i];

//						dpw[i+1] = dpw[i] + w[i];

					}else{

						dpw[i+1][j] = dpw[i][j];

//						dpw[i+1] = dpw[i];

					}

				}

			}

		}

		ans_c++;

		printf("Case %d:\n%d\n%d\n", ans_c , dp[n][cap], dpw[n][cap]);

//		rep(i, n) cout << dpw[i] << endl;

	}

}

C++: //============================================================================
// Name        : AOJ0042.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

#define MAX_N 1000
#define MAX_M 10000

int n, W;
int w[MAX_N] = { 0 }, v[MAX_N] = { 0 };

int dp[MAX_N + 1][MAX_M + 1] = { 0 };

void solve() {

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

      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= W; j++) {
      //printf("%5d", dp[i][j]);
    }
    //cout << endl;
  }
  //cout << endl;
  cout << dp[0][W] << endl;
  int i;
  for (i = W; i >= 0; i--) {
    if (dp[0][W] != dp[0][i]) break;
  }
  cout << i + 1 << endl;

}

int main() {

  int kase = 1;
  char str[11];
  cin >> W;

  while (W != 0) {
    cin >> n;

    for (int i = 0; i < n; i++) {
      cin >> str;

      int j = 0;

      v[i] = 0;
      w[i] = 0;

      while (str[j] != ',') {
        v[i] = v[i] * 10;
        v[i] = v[i] + (str[j] - '0');
        j++;
      }

      j++;

      while (str[j] != '\0') {
        w[i] = w[i] * 10;
        w[i] = w[i] + (str[j] - '0');
        j++;
      }
    }
    /*
     for (int i = 0; i < n; i++) {
     cout << v[i] << endl;
     cout << w[i] << endl;
     }
     */
    cout << "Case " << kase << ":" << endl;
    solve();

    kase++;
    cin >> W;
  }

  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <utility>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int W,N;
const int MAX_N = 1001;
const int MAX_W = 1001;
int v[MAX_N];
int w[MAX_N];
int dp[MAX_N][MAX_W];

void solve() {
	memset(dp, 0, sizeof(dp));
	repa(i,1,N) {
		repa(j,0,W) {
			if(j - w[i] >= 0) {
				dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]);
			} else {
				dp[i][j] = dp[i-1][j];
			}
		}
	}

	int maxval = -1;
	int weight = 0;
	repa(i,0,W) {
		if(maxval < dp[N][i]) {
			maxval = dp[N][i];
			weight = i;
		}
	}
	cout <<	maxval << endl;
	cout << weight << endl;
}

int main() {
	int c = 0;
	while(cin>>W, W) {
		cin >> N;
		repa(i,1,N) {
			scanf("%d,%d", &v[i], &w[i]);
		}

		printf("Case %d:\n", ++c);
		solve();
	}
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>

#define INF 1e6

using namespace std;

int N,W;
int w,v;
int dp[1001];

int main(void){
	int tem=1;
	int vmax=0,wmax;
	while(true){
		cin >> W >> N;
		if(!W)
			break;
		memset(dp,-1,sizeof(dp));
		dp[0]=0;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&v,&w);
			for(int i=0;i+w<=W;i++)
				if(dp[i]>=0){
					dp[i+w]=max(dp[i+w],dp[i]+v);
					i+=w;
				}
		}
		for(int i=0; i<=W; i++){
			if(dp[i]>vmax){
			vmax=dp[i];
			wmax=i;
			}
		}
		cout << "Case "<<tem<<':'<<endl;
		cout <<  vmax << endl << wmax <<endl;
		tem++;
	}
	return 0;
}