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