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