Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

const MaxW = 1000
const MaxN = 1000

type Tuple struct {
	Value  int
	Weight int
}

func nextLine(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func solve(w int, n int, vws []Tuple) Tuple {
	var dp [MaxN + 1][MaxW + 1]int

	for i := 0; i < n; i++ {
		for j:= w; j > 0; j-- {
			if j < vws[i].Weight {
				dp[i + 1][j] = dp[i][j]
			} else {
				dp[i + 1][j] = int(math.Max(float64(dp[i][j]), float64(dp[i][j - vws[i].Weight] + vws[i].Value)))
			}
		}
	}

	maxValue := dp[n][w]
	minWeight := w

	for j := w - 1; j > 0; j-- {
		if maxValue == dp[n][j] {
			minWeight = j
		} else {
			break
		}
	}

	return Tuple{Value: maxValue, Weight: minWeight}
}

func main() {
	var sc = bufio.NewScanner(os.Stdin)
	index := 1
	for {
		var w, n int
		w, _ = strconv.Atoi(nextLine(sc))
		if w == 0 {
			return
		}
		n, _ = strconv.Atoi(nextLine(sc))

		vws := make([]Tuple, n, n)
		for i := 0; i < n; i++ {
			array := strings.Split(nextLine(sc), ",")
			v, _ := strconv.Atoi(array[0])
			w, _ := strconv.Atoi(array[1])
			vws[i] = Tuple{Value: v, Weight: w}
		}

		t := solve(w, n, vws)
		fmt.Printf("Case %d:\n", index)
		fmt.Println(t.Value)
		fmt.Println(t.Weight)
		index++
	}
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int dp[1000][1000];
	int count=1,i,j,W,w,n;
	while(1)
	{
		memset(dp,0,sizeof(dp));
		cin >> W;
		if(W==0)break;
		cin >> n;
		int v[n],w[n];
		for(i=0;i<n;i++)
		{
			scanf("%d,%d",&v[i],&w[i]);
		}
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=W;j++)
			{
				if(i==0)
					dp[i][j]=0;
				else if(w[i]>j)
					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(i=W;i>=0;i--)
		{
			if(dp[n][W]>dp[n][i])break;
		}
		cout << "Case " << count << ':' << endl << dp[n][W] << endl;
		cout << i+1 << endl;
		count++;
	}
	return 0;
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;

int N;
int W;
vector<int> w, v;
int main(void) {
    int count = 1;
    while (1) {
        cin >> W;
        if (W == 0) return 0;
        cin >> N;
        for (int i = 0; i < N; i++) {
            int tmp, tmp2;
            scanf("%d, %d", &tmp, &tmp2);
            v.push_back(tmp);
            w.push_back(tmp2);
        }
        // dp[i][j]はでjまでで重さがiまで選んでいるとき得られる最高の価値
        long long dp[N+1][W+1];
        for (int i = 0; i <= N; i++) {
            for (int j = 0; j <= W; j++) {
                dp[i][j] = 0;
            }
        }
        for (int i = 1; i < N; i++) {
            for (int j = 0; j <= W; j++) {
                if (j - w[i] >= 0) {
                    dp[i][j] = max(dp[i-1][j - w[i]] + v[i], dp[i][j]);
                }
            }
        }
        int topvalue = 0, indexweight = 0;
        for (int j = 0; j <= W; j++) {
            if (topvalue < dp[N-1][j]) {
                topvalue = dp[N-1][j];
                indexweight = j;
            }
        }
        cout << "Case " << count << ":" << endl;
        cout << topvalue << endl;
        cout << indexweight << endl;
        count++;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define M_N 1000
#define M_W 1000

typedef pair<int, int> ii; //?????????, ?????????

int W, N;
int v[M_N], w[M_N];
int dp[M_N + 1][M_W + 1][2];

//?????????j??\?????¶??????i????????\????????????????????????
ii dfs(int i, int j) {
	if (dp[i][j][0] != -1)
		return ii(
			dp[i][j][0],
			dp[i][j][1]
		);

	if (i == N) {
		//?????°??¶?????????????????¨???
		return ii(
			dp[i][j][0] = 0,
			dp[i][j][1] = 0
		);
	}
	else if (j < w[i]) {
		//i??????????????????????????¶?????§??\??????????????¨???
		return ii(
			dp[i][j][0] = dfs(i + 1, j).first,
			dp[i][j][1] = dfs(i + 1, j).second
		);
	}
	else {
		//???????????\???????????\??????????????§?????????????????????????????§??¨?????????
		int a = dfs(i + 1, j).first,
			b = dfs(i + 1, j - w[i]).first + v[i];
		if (a > b)
			return ii(
				dp[i][j][0] = a,
				dp[i][j][1] = dfs(i + 1, j).second
			);
		else
			return ii(
				dp[i][j][0] = b,
				dp[i][j][1] = dfs(i + 1, j - w[i]).second + w[i]
			);

	}
}

int main() {
	for (int h = 1; cin >> W, W; h++) {
		cin >> N;
		memset(dp, -1, sizeof(dp));
		for (int i = 0; i < N; i++) {
			char c;
			cin >> v[i] >> c >> w[i];
		}
		cout << "Case " << h << ":" << endl;
		cout << dfs(0, W).first << endl;
		cout << dfs(0, W).second << endl;
	}
	return 0;
}

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

using namespace std;

int main(void){
  int W,n,v[1000],w[1000],dp[1000][1000],t=1;

  while(cin >> W,W){
    cin >> n;

    for(int i=0;i<n;i++)
    scanf("%d,%d",&v[i],&w[i]);

    for(int i=0;i<1000;i++)
      for(int j=0;j<1000;j++)
	dp[i][j]=0;
    
    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]);

    cout << "Case " << t << ":" << endl;
    cout << dp[n][W] << endl;

    bool fg=false;
    for(int j=0;j<=W;j++){
      for(int i=0;i<=n;i++){
	if(dp[n][W]==dp[i][j]){
	  cout<< j << endl;
	  fg=true;
	    break;
	}
      }
      if(fg)break;
    }
    t++;
  }

  return 0;
}

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

int main(){
  int ww,n;
  char c;
  int v[1050], w[1050];
  int dp[1050];

  int datacase=1;
  while( cin>>ww && ww ){
    cin >> n;
    for( int i=0;i<n;i++ )
      cin >> v[i] >> c >> w[i];

    dp[0]=0;
    for( int i=1;i<=ww;i++ )
      dp[i] = -1;


    for( int j=0;j<n;j++ ){
      for( int i=ww-w[j];i>=0;--i){
      //      for( int i=0;i<=ww-w[j];i++ ){ // 間違い
	if( dp[i]>=0 ){
	  int k =i+w[j];
	  dp[k] = max( dp[k],dp[i]+v[j] );
	}
      }
    }

    int aw=0,av=0;
    for( int i=0;i<=ww;i++ ){
      //      cout << i << "\t" << dp[i] << endl;
      if( dp[i]>av ){
	av=dp[i]; aw=i;
      }
    }
    cout << "Case " << datacase++ << ":" << endl;
    cout << av << endl;
    cout << aw << endl;


  }
  return 0;
}