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 <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
 
using namespace std;
 
#define pb(n)	push_back(n)
#define mp(n,m) make_pair(n,m)
#define fi 	first
#define se 	second
#define all(r) r.begin(),r.end()

#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,a,b) for(int i=(a); i<=(b); i++)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
const int IMAX=((1<<30)-1)*2+1;
const double EPS=1e-9;
//int mod=1000000007;

vector<pii> v(1010,pii(0,0));
vector<vi> dp(1010,vi(1010,-1));
int N,W;

int ap=0,aw=0;

void init(){
	for(int i=0; i<N+1; i++){
		for(int j=0; j<W+1; j++){
			dp[i][j]=-1;
		}
		v[i]=mp(0,0);
	}
	ap=0;
	aw=0;
}

int calc(int n,int w){
	if(dp[n][w]!=-1)return dp[n][w];
	int ret;
	int t=w;
	if(n==N)ret=0;
	else if(w+v[n].second>W)ret=calc(n+1,w);
	//else ret=max(calc(n+1,w),calc(n+1,w+v[n].second)+v[n].first);
	else{
		int a=calc(n+1,w),b=calc(n+1,w+v[n].second)+v[n].first;
		if(b>a)t+=v[n].second;
		ret=max(a,b);
		//if(a==0&&b==10)cout<<n<<" "<<ret<<" "<<t<<endl;
	}
	// if(ret>=ap){
	// 	ap=ret;
	// 	aw=w;
	// 	cout<<ap<<" "<<aw<<endl;
	// }
	if(ret>=ap){
		ap=ret;
		aw=t;
	}
	//cout<<ret<<" "<<n<<" "<<t<<endl;
	return dp[n][w]=ret;
}



int main(){
	int k=0;
	char c;
	while(++k){
		cin>>W;
		//cout<<"    W        "<<W<<endl;
		if(W==0)break;
		cin>>N;
		init();
		for(int i=0; i<N; i++){
			cin>>v[i].first>>c>>v[i].second;
		}
		calc(0,0);
		cout<<"Case "<<k<<":"<<endl;
		cout<<ap<<endl;
		cout<<aw<<endl;
		//scout<<v[4].first<<endl;;
		// for(int i=0; i<N+1; i++){
		// 	for(int j=0; j<W+1; j++){
		// 		if(dp[i][j]==220)cout<<i<<" "<<j<<endl;
		// 	}
		// }
	}
}

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

using namespace std;

int dp[1001][1001];

int main(){
  int W, N, v[1024], w[1024], c = 0, res = 0;
  while(scanf("%d", &W) && W){
    c++;
    scanf("%d", &N);
    for(int i = 0; i < N; ++i) scanf("%d,%d", v + i, w + i);
    res = 0;
    memset(dp, 0, sizeof(dp));
    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]);
      }
    }
    for(int j = 0; j < W; ++j) if(dp[N][j] < dp[N][W]) res++;
    printf("Case %d:\n%d\n%d\n", c, dp[N][W], res);
  }
  return 0;
}

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

int dp[1001][1001];
int N, W;
int v[1001], w[1001];

int solve(int i, int j) {
	if (dp[i][j] != -1)return dp[i][j];

	int res;
	if (i == N)res = 0;
	else if (j < w[i])res = solve(i + 1, j);
	else {
		res = max(
			solve(i + 1, j),
			solve(i + 1, j - w[i]) + v[i]
		);
	}
	return dp[i][j] = res;
}

int main() {
	int now = 1;
	while (1) {
		cin >> W;
		if (W == 0)break;
		cin >> N;
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", v + i, w + i);
		}

		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = -1;
			}
		}
		solve(0, W);
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				cout << i << ":" << j<<"=" << dp[i][j] << endl;
			}
		}
		int value = -1; int weight;
		for (int i = 0; i <= W;i++) {
			if (value < dp[0][i]) {
				value = dp[0][i];
				weight = i;
			}
		}
		cout << "Case " << now++ << ":" << endl << value << endl << weight-1 << endl;
	}
}

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

struct treasure{
	int value;
	int weight;
};

bool comp(treasure t1, treasure t2){
	if(t1.weight <= t2.weight) return true;
	else return false;
}

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	while(cin >> w){
		if(w == 0) break;
		int n;
		int cost[maxw + 1];
		int best[maxw + 1];
		for(int i = 0; i <= maxw; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		vector<treasure> treasures;
		cin >> n;
		for(int i = 0; i < n; i++){
			treasure t;
			char c;
			cin >> t.value >> c >> t.weight;
			treasures.push_back(t);
		}
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= treasures[j].weight)
					if(cost[k] < cost[k - treasures[j].weight] + treasures[j].value)
					{
						cost[k] = cost[k - treasures[j].weight] + treasures[j].value;
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;w>0; w--) if(cost[w] != cost[w-1]) break;
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - treasures[best[l]].weight >= 0;){
			if(l - treasures[best[l]].weight >= 0){
				total += treasures[best[l]].weight;
				l -= treasures[best[l]].weight;
			}
		}
		cout << total << endl;
	}
	return 0;
}

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

using namespace std;

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

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 dp[i][j] = 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++: #include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int main(void)
{
	int w,n;
	int i,j;
	int cnt=0;
	int value,weight;
	int vsum,wsum;
	char ch;
	while(1){
		cnt++;
		cin>>w;
		if(w==0){
			break;
		}
		cin>>n;
		
		for(i=0;i<=n;i++){
			for(j=0;j<=w;j++){
				dp[i][j]=0;
			}
		}
		for(i=0;i<n;i++){
			cin>>value>>ch>>weight;
			for(j=0;j<=w;j++){
				if(dp[i+1][j]<dp[i][j]){
					dp[i+1][j]=dp[i][j];
				}
				if(j+weight<=w&&dp[i+1][j+weight]<dp[i][j]+value){
					dp[i+1][j+weight]=dp[i][j]+value;
				}
			}
		}
		vsum=-1;
		for(j=0;j<=w;j++){
			if(vsum<dp[n][j]){
				vsum=dp[n][j];
				wsum=j;
			}
		}
		cout<<"Case "<<cnt<<":"<<endl;
		cout<<vsum<<endl<<wsum<<endl;
	}
	return 0;
}