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

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

void solve(){
    for(int i = 0; i < N; i++){
        for(int j = 0; j <= W; j++){
            dp[i+1][j] = 0;
        }
    }
    for(int i = 0; i < N; i++){
        dp[i+1][w[i]] = v[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 c,v = 0;
    for(int i = 0; i <= W; i++){
        if( dp[N][i] > v ){
            v = dp[N][i];
            c = i;
        }
    }
    printf("Case %d:\n%d\n%d\n", cnt, v, c);
}

int main(){
    cnt = 0;
    while( true ){
        cnt++;
        scanf("%d", &W);
        if( !W ) break;
        scanf("%d", &N);
        for(int i = 0; i < N; i++){
            scanf("%d,%d", v+i, w+i);
        }
        solve();
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

typedef pair<int, int> obj;

int main(){
  int N, W, weight, value;
  char ch;
  int case_num = 1;

  while(1){
    cin >> W;
    if(W == 0) break;
    cin >> N;

    //printf("W = %d N = %d\n", W, N);

    vector<obj> obj_list;
    int dp[2][1010];
    memset(dp, -1, sizeof(dp));
    
    for(int i = 0; i < N; i++){
      cin >> value >> ch >> weight;
      obj_list.push_back(obj(value, weight));
      //printf("value = %d weight = %d\n", value, weight);
    }

    dp[0][0] = 0;
    dp[1][0] = 0;

    for(int i = 0; i < N; i++){
      obj o = obj_list[i];

      for(int j = 0; j <= W; j++){
        if(dp[(i+1)%2][j] != -1){
          dp[i%2][j] = max(dp[i%2][j], dp[(i+1)%2][j]);
          if(j + o.second <= W){
            dp[i%2][j+o.second] = max(dp[i%2][j+o.second], dp[(i+1)%2][j] + o.first);
          }
        }
      }
    }
    int answer = 0;
    int answer_weight = 0;

    for(int i = 0; i <= W; i++){
      if(answer < dp[(N+1)%2][i]){
        answer = dp[(N+1)%2][i];
        answer_weight = i;
      }
      if(answer < dp[N%2][i]){
        answer = dp[N%2][i];
        answer_weight = i;
      }
    }

    cout << "Case " << case_num << ":" << endl;
    cout << answer << endl;
    cout << answer_weight << endl;

    case_num++;
  }

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

struct treasure_t{
    int value;
    int weight;
    treasure_t( const int v, const int w ){
        value = v;
        weight = w;
    }
};

vector<treasure_t> treasures;
int W, maxW, maxV;

void solve( int n, int w, int v ){
    if( w > W ) return;
    if( n==treasures.size() ){
        if( (v > maxV) || (v==maxV && w<maxW) ){
            maxV = v;
            maxW = w;
        }
        return;
    }
    solve(n+1,w+treasures[n].weight,v+treasures[n].value);
    solve(n+1,w,v);
}

int main(){
    int cnt = 1;
    while( cin >> W, W ){
        int n;
        cin >> n;
        treasures.clear();
        maxW = 0;
        maxV = 0;
        for( int i=0; i<n; i++ ){
            int v, w;
            scanf("%d,%d", &v,&w);
            treasure_t t(v,w);
            treasures.push_back(t);
        }
        solve(0,0,0);
        cout << "Case " << cnt << ":" << endl;
        cout << maxV << endl;
        cout << maxW << endl;
        cnt++;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)

int caseId = 1;

void solve42(int W, int N) {
    int dp[1001] = {}; // dp[i]: maxvalue under weight i
    REP(i,N) {
        int a, b;
        char c;
        cin >> a >> c >> b;
        for(int j = W; j >= b; j--) {
            dp[j] = max(dp[j], dp[j-b] + a);
        }
    }
    int value = 0, weight = 0;
    REP(i,W) {
        if (value < dp[i]) {
            value = dp[i];
            weight = i;
        }
    }
    cout << "Case " << caseId << ":" << endl << value << endl << weight << endl;
}

int main() {
    int W, N;
    while (cin >> W >> N, W) {
        solve42(W, N);
        caseId++;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int w,n;
pair<int,int> takara[1002];
int dp[1002][10002];
int saidai=0,banti=0;
char ch;
int now=1;
main(){
  while(1){
    cin>>w;
    if(w==0){
      break;
    }
    cin>>n;
    for(int i=1;i<=n;i++){
      cin>>takara[i].first>>ch>>takara[i].second;
    }
    saidai=0;
    banti=0;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=w;j++){
	if(i==0){
	  dp[i][j]=0;
	}
	else if(j-takara[i].second<0){
	  dp[i][j]=dp[i-1][j];
	}
	else{
	  dp[i][j]=max(dp[i-1][j],dp[i-1][j-takara[i].second]+takara[i].first);
	}
	if(i==n&&saidai<dp[i][j]){
	  saidai=dp[i][j];
	  banti=j;
	}
      }
    }
    cout<<"Case "<<now<<":"<<endl<<saidai<<endl<<banti<<endl;
    now++;
  }
}