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 <cstdio>
#include <algorithm>
using namespace std;

int n, W;
int v[1200], w[1200], dp[1200];

void solve() {
  fill(dp, dp+1200, 0);
  for(int i = 0; i < n; i++) {
    for(int j = W; j >= 0; j--) {
      if(j-w[i] >= 0) dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
    }
  }
  cout << dp[W] << endl;

  for(int i = W; i >= 0; i--) {
    if(dp[W] != dp[i]) {
      cout << i+1 << endl;
      break;
    }
  }
}

int main() {
  int cnt = 1;
  while(cin >> W, W) {
    cin >> n;
    for(int i = 0; i < n; i++) {
      scanf("%d,%d", &v[i], &w[i]);
    }
    
    printf("Case %d:\n", cnt++);
    solve();
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 28)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int W, N;
int tres[1001];
int weig[1001];
int maxW, maxV;
int dp[1001][1001];

void dfs(int weight, int value, int index){
	if(weight > W)return;
	if(dp[weight][index]>value)return;
	dp[weight][index] = value;
	if(index == N){
		if(maxV < value){
			maxW = weight;
			maxV = value;
		}else if(maxV == value){
			if(maxW > weight){
				maxW = weight;
				maxV = value;
			}
		}
		return;
	}
	dfs(weight, value, index+1);
	dfs(weight+weig[index], value+tres[index], index+1);
}


int main(void){
	int cnt = 0;
	while(cin >> W, W){
		cnt++;
		cin >> N;
		maxW = 0;
		maxV = 0;
		fill_n(tres, 1001, 0);
		fill_n(weig, 1001, 0);
		fill_n(*dp, 1001*1001, -1);

		rep(i,N){
			int n, w;
			scanf("%d,%d", &n, & w);
			tres[i] = n;
			weig[i] = w;
		}

		dfs(0,0,0);
		cout << "Case " << cnt << ":" << endl;
		cout << maxV << endl << maxW << endl;
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
#define W_MAX 1000
#define N_MAX 1000

int W, N, dp[N_MAX + 1][W_MAX + 1], w[N_MAX + 1], v[N_MAX + 1];;

int main(){
	int c = 1;
	while (1){
		scanf("%d %d", &W, &N);
		if (W == 0)
			break;
		for (int i = 1; i <= N; ++i){
			scanf("%d %d", &v[i], &w[i]);
		}
		for (int i = 1; i <= W; ++i)
			dp[0][i] = 0;
		for (int i = 1; i <= N; ++i){
			for (int j = 1; j <= W; ++j){
				if (j < w[i])
					dp[i][j] = dp[i - 1][j];
				else{
					dp[i][j] = std::max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
				}
			}
		}
		int minv = 1;
		for (int i = 0; i <= W; ++i){
			if (dp[N][minv] < dp[N][i])
				minv = i;
		}
		printf("%d\n", "Case", c, ":");
		printf("%d\n", dp[N][W]);
		printf("%d\n", minv);
		++c;
	}
	return 0;
}

C++: // Volume0 - 0042: A Thief
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
//#define DEBUG
 
// ??????????????°??? 1000 ??\????????????????????????10000 ??\???
// ???????????????????????§??????10^7
class Solver {
private:
    int caseNum; // ???????????????????????????
    int w; // ?¢¨??????????????????????????????W(1000 ??\???)
    int n; // ???????????° N
    vector<int> vv; // vector of value
    vector<int> vw; // vector of weight
    int maxValue;  // ?¢¨???????????\???????????????????????????
    int maxWeight; // ????????¨??????????????????????????????
public:
    Solver():caseNum(0){}
    bool input();
    void calc();
    void output();
};
bool Solver::input() {
    ++caseNum;
    cin >> w;
    if(w == 0) return false;
    cin >> n;
    vv.clear();
    vw.clear();
    for(int i=0; i<n; i++) {
        int value, weight;
        char ch;
        cin >> value >> ch >> weight;
        vv.push_back(value);
        vw.push_back(weight);
    }
    return true;
}
void Solver::calc() {
    vector<int> memo(w+1,0);
    for(unsigned int i=0; i<vv.size(); i++) {
        vector<int> temp = memo;
        for(unsigned int j=0; j+vw[i]<=memo.size(); j++) {
            temp[j+vw[i]] = std::max(memo[j+vw[i]], memo[j]+vv[i]);
        }
        memo = temp;
    }
    maxValue = 0;
    maxWeight = 0;
    for(int i=memo.size()-1; i>=0; --i) {
        if(maxValue <= memo[i]) {
            maxValue = memo[i];
            maxWeight = i;
        }
    }
}
void Solver::output() {
    cout << "Case " << caseNum << ":" << endl;
    cout << maxValue << endl;
    cout << maxWeight << endl;
}
 
int main(int argc, char** argv) {
    Solver solver;
    while(solver.input()) {
        solver.calc();
        solver.output();
    }
    return 0;
}