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;
#define latte WORLD

int N,W,tv[1000],tw[1000];
int memo[1000][1001],ansv,answ;

int latte(int n,int v,int w){
    if(w>W) return 0;
    if(n==N){
        if(ansv<v || (ansv==v && answ>w)){
            ansv = v; answ = w;
        }
        return v;
    }
    if(memo[n][w]!=-1) return memo[n][w];
    int ans=0;
    memo[n][w] = max(latte(n+1,v+tv[n],w+tw[n]),latte(n+1,v,w));
    return memo[n][w];
}

int main(){
    for(int ds=1;cin>>W,W;ds++){
        cin>>N;
        for(int i=0;i<N;i++) scanf("%d,%d",&tv[i],&tw[i]);
        fill_n((int*)memo,1000*1001,-1); ansv = answ = 0;
        latte(0,0,0);
        printf("Case %d:\n%d\n%d\n",ds,ansv,answ);
    }
}

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

#define MAX_N 1000
#define MAX_W 10000

int dp[MAX_W][MAX_N], W, N, value[MAX_N], w[MAX_N];

int main(){
	int count = 0;

	while(true){
		++count;
		
		if(w == 0)
			break;

		scanf("%d%d", &W, &N);

		for(int i = 0; i < N; ++i){
			for(int j = 0; j <= W; ++j)
				dp[j][i] = 0;
		}//initialize

		for(int i = 0; i < N; ++i)
			scanf("%d,%d", &value[i], &w[i]);
		//input

		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
					std::max(dp[i + 1][j], dp[i + 1][j - w[i]] - value[i]);
			}
		}
		printf("count %d:%d?\n", count, dp[0][W]);
	}
	return 0;
}

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

using namespace std;

const int MAX_w = 1000;
const int MAX_v = 10000;

int dp[MAX_w + 1][MAX_v + 1];


int n, wet ;
int w[MAX_w], v[MAX_v];

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
int rec(int i, int j) {
  int res;
  if(dp[i][j] != -1){
  	
	return dp[i][j];
  }

  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(
        rec(i + 1, j),
        rec(i + 1, j - w[i]) + v[i]
    );
  }

  return res;
}

void solve(int ca) {
  // 0番目以降で容量W以下の場合の結果を表示する
  int wmin;
  
  memset(dp, -1, sizeof(dp));
  
  cout << "Case" << " " << ca << ":" <<endl; 
  
  int ans =  rec(0, wet) ;
  cout << ans << endl;
  
  for (int w = wet -1 ; w>=0 ; w--){
  	if( ans != rec(0,w)){
		 wmin = w+1;
		 break;
  	}
  }
  
  cout << wmin << endl;  
  
}


int main(){
	
	char x;
	int ca = 0;
	
	while(1){
			
		ca++;
		cin >> wet;
		if(wet == 0 ) break;
		cin >> n;
		
		for(int i = 0 ;  i<n ; i++){
			
			cin >> v[i] >> x >> w[i];
		}
		
		
		 solve(ca);
		
	}
	
	return 0;
	
}

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 };

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

void solve() {

  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] = (short)max((int)dp[i][j], dp[i][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[n][W] << endl;
  int i;
  for (i = W; i >= 0; i--) {
    if (dp[n][W] != dp[n][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 <algorithm>
#include <stdio.h>

using namespace std;

#define WMAX 1000
#define NMAX 1000
int main(void)
{
    int W, n;
    int cyc = 0;
    while (cin >> W && W && cin >> n) {
        cyc++;
        int dp[WMAX+1][NMAX+1] = {};
        for (int i = 0; i < n; i++) {
            int v, w;
            scanf("%d,%d", &v, &w);
            for (int j = 0; j < W+1; j++) {
                dp[j][i+1] = max(dp[j][i], (j-w >= 0 ? dp[j-w][i] + v : 0));
            }
        }
        for (int i = 0; i < n + 1; i++) {
            for (int j = 0; j < W+1; j++) {
                cout << dp[j][i] << " ";
            }
            cout << endl;
        }
        cout << "Case " << cyc << ":" << endl;
        for (int j = 0; j < W+1; j++) {
            if (dp[j][n] == dp[W][n]) {
                cout << dp[j][n] << endl << j << endl;
                break;
            }
        }
    }
}

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

int dp[1000];
int main(){
	int w, n, ans_w = 0, ans_val = 0;
	for (int dataset = 1; cin >> w, w; dataset++){
		int t_w, t_val;
		cin >> n;
		for (int i = 0; i < w; i++) dp[i] = -1;
		dp[0] = 0;
		for (int i = 0; i < 5000000000; i++){}
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &t_val, &t_w);
			for (int j = w - 1; j >= 0; j--){
				//printf("%d ", dp[j]);
				if (dp[j] == -1 || j + t_w >= w || dp[j + t_w] > dp[j] + t_val)
					continue;
				dp[j + t_w] = dp[j] + t_val;
			}
			//puts("");
		}
		for (int i = 0; i < w; i++){
			//printf("%d ", dp[i]);
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		//puts("");
		printf("Case %d:\n%d\n%d\n", dataset, ans_val, ans_w);
	}
	return 0;
}