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 <vector>
#include <algorithm>
#include <cmath>
#include <utility>

#define ll (long long)
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)

using namespace std;


int main(void){
    
    /*(x,y) = (??????,??????)*/
    int max_w;
    int number;
    int answer = 0;
    int casecounter = 1;
    while(cin >> max_w,max_w)
    {
        int dp[1002][1002] = {0};
        int w[1002] = {0},v[1002] = {0};
        cin >> number;
        for(int i  =1; i<= number;i++)
            scanf("%d%*c%d",&v[i],&w[i]);
        
        for(int j = 1; j<= number;j++)
            for(int k = 0;k<=max_w;k++)
            {
              if(j == 1 && k>=w[j])
                  dp[j][k] = v[j];
              else if(k >= w[j])
                  dp[j][k] = max(dp[j-1][k],v[j]+dp[j-1][k-w[j]]);
     
                
            }
        int max = 0;
        for(int i = 1;i<=max_w;i++)
        {
            if(max < dp[number][i])
            {
                answer = i;
                max = dp[number][i];
            }
            
        }
        printf("Case %d:\n",casecounter);
        printf("%d\n",dp[number][answer]);
        printf("%d\n",answer);
        casecounter++;
    }
    return 0;
    
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

const int maxw = 1001;
const int maxn = 1001;
const int maxp = 10001;

int dp[maxn][maxw]={};
int w[maxn] = {};
int p[maxn] = {};
int W,N;

int main()
{
	int casenum = 0;
	
	while(cin >> W)
	{
		if(W==0)break;
		casenum++;
		cout << "Case " << casenum << ":" << endl;
		cin >> N;
		char t;
		rep(i,N) cin>>p[i] >> t >> w[i];

		memset(dp,0,sizeof(dp));
		rep(i,N)rep(j,W+1)
		{
			if(j-w[i]>=0) dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]] + p[i]);
			else dp[i+1][j] = dp[i][j];
		}
		int ansp = 0,answ = 0;
		for(int j=W;j!=0;j--)if(ansp< dp[N][j])
		{
			ansp = dp[N][j];
			answ = j;
		}
		
		cout << ansp << endl;
		cout << answ-1 << endl;
	}
	return 0;
}

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

//関数の宣言
int main();
int rec();
void slove();

//グローバル変数
int n,ww,c=0,omo=0,a=0;
int w[11111],v[11111];
int dp[11111][11111];//メモ化テーブル

int main(){ //入力

  while(1){
  scanf("%d",&ww);
  if(ww==0) break;
  c++;
  omo=0;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d,%d",&v[i],&w[i]);
      }
  slove();
  }
  return 0;
}

int rec(int i,int j){
  if(omo<w[i]){
    omo=w[i];
  }
  //既に調べたことがあるならその結果を再利用
  if(dp[i][j]>=0){
    return dp[i][j];
  }
  int res;

  if(i==n){
    res=0;
  }
  else if(j<w[i]){
    res=(i+1,j); 
  }
  else{
      res=max(rec(i+1,j),rec(i+1,j-w[i])+v[i]);
    }
    //結果をテーブルに記憶する
   return dp[i][j]=res;
}

void slove(){
  //まだ調べていないことを表す-1でメモ化テーブルを初期化
  memset(dp,-1,sizeof(dp));
  printf("Case %d:\n",c);
  printf("%d\n",rec(0,ww));
  printf("%d\n",omo);
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <ctype.h>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> dP;

const int INF = 1000000000;
const double EPS = 1e-10;

int main(){
	freopen("test.txt", "r", stdin);
	int casenum = 0;
	while(1){
		int W, n;
		cin >> W >> n;
		if(!W) break;

		vector<int> w(n+1);
		vector<int> v(n+1);
		FOR(i, 1, n+1){
			scanf("%d,%d", &v[i], &w[i]);
		}

		vector<vector<int> > table(n+1, vector<int>(W+1));

		FOR(i, 1, n+1){
			for(int j = 0; j <= W; j++){
				if(j >= w[i]){
					table[i][j] = max(table[i-1][j], table[i-1][j-w[i]]+v[i]);
				}else{
					table[i][j] = table[i-1][j];
				}
			}
		}


		int idx = 0;
		rep(i, W+1){
			if(table[n][idx] < table[n][i]){
				idx = i;
			}
		}

		cout << "Case " << ++casenum << ':' << endl;
		cout << table[n][idx] << endl;
		cout << idx << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>

using namespace std;

const int INF = 1 << 29;
typedef pair<int, int> P;

int n, w;
int dp[1001][1001];//お宝の番号、重さ
int main(void)
{
	int caseCount = 0;
	while(cin >> w && w)
	{
		cin >> n;
		vector<int> ps(n), ws(n);
		memset(dp, 0, sizeof(dp));
		char c;
		for (int i = 0; i < n; i++)
		{
			cin >> ps[i] >> c >> ws[i];
		}

		if(ws[0] <= w) dp[0][ws[0]] = ps[0];
		for (int i = 1; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				/*if(ws[i] <= j)
					dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - ws[i]] + ps[i]);*/
				if(j + ws[i] <= w)
					dp[i][j + ws[i]] = max(dp[i][j + ws[i]], max(dp[i - 1][j + ws[i]], dp[i - 1][j] + ps[i]));
				else dp[i][j] = max(dp[i - 1][j], dp[i][j]);
			}
		}

		cout << "Case " << ++caseCount << ":" << endl;
		int ansp = 0, answ = INF;
		for (int i = 0; i <= w; i++)
		{
			if(ansp < dp[n - 1][i])
			{
				ansp = dp[n - 1][i];
				answ = i;
			}
		}
		cout << ansp << endl << answ << endl;
	}
	return 0;
}

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

int dp[1001];

int main() {
    int n,w,t=1;
    while(scanf("%d", &w), w) {
        memset(dp, -1, sizeof(dp));
        scanf("%d", &n);
        dp[0] = 0;

        int v,x;
        for(int i=0; i<n; ++i) {
            scanf("%d,%d", &v,&x);
            for(int j=w; j>=0; --j) {
                if(dp[j] == -1 || j+x > w) continue;
                dp[j+x] = max(dp[j+x],dp[j]+v);
            }
        }

        int ans = 0,ai;
        for(int j=0; j<=w; ++j) {
            if(ans > dp[j]) ai = j;
            ans = max(ans, dp[j]);
        }
        printf("Case %d:\n%d\n%d\n", t++, ans, ai+1);
    }
}