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 <algorithm>
#include <cstring>

using namespace std;

int W, N, casenum = 0, ans, wmin;
int v[1000], w[1000];
int dp[1001][1001];

int rec_dp(int, int);
void solve();

int main(){
	int i;
	char c;
		
	while(1){
		cin >> W >> N;
		if(W == 0)
			break;
		
		casenum++;
		for(i = 0; i < N; i++)
			cin >> v[i] >> c >> w[i];
		
		memset(dp, -1, sizeof(dp));
		ans = rec_dp(0, W);
		
		for(i = 0; i < W; i++){
			if(rec_dp(0, i) == ans){
				wmin = i;
				break;
			}
		}
		
		solve();
	}
	
	return 0;
}

int rec_dp(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 = rec_dp(i + 1, j);
	else
		res = max(rec_dp(i + 1, j), rec_dp(i + 1, j - w[i]) + v[i]);
	
	return dp[i][j] = res;
}

void solve(){
	cout << "Case" << " " << casenum << ":" << endl;
	cout << ans << endl;
	cout << wmin << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

bool cmp(pair<int,int> &a,pair<int,int> &b){
	if( a.first != b.first ) return a.first < b.first;
	else return a.second > b.second;
}
int main(){
	int n,W;
	int t = 1;
	while( cin >> W >> n && W ){
		vector< pair<int,int> > cur;
		cur.push_back({0,0});
		for(int i = 0 ; i < n ; i++){
			int w,c;
			scanf("%d,%d",&c,&w);
			for(int j = 0 , t = cur.size() ; j < t ; j++ )
				cur.push_back(pair<int,int>(cur[j].first+w,cur[j].second+c));
			sort(cur.begin(),cur.end(),cmp);
			int maxi = -1;
			vector< pair<int,int> > nex;
			for(int j = 0 ; j < cur.size() ; j++){
				if( maxi >= cur[j].second ) continue;
				nex.push_back(cur[j]);
				maxi = cur[j].second;
			}cur = nex;
		}
		int ans = 0, ans2 ;
		for(int i = 0 ; i < cur.size() ; i++){
			if( cur[i].first <= W ){
				ans = cur[i].second;
				ans2 = cur[i].first;
			}
		}

		cout << "Case " << t++ << ":" << endl;
		cout << ans << endl;
		cout << ans2 << endl;
	}
}

C++: #include<iostream>

using namespace std;

class item
{
public:
	int value;
	int weight;
	bool flag;
};

bool input(int *c, int *n, item it[1000])
{
	cin>>*c;
	if(*c==0)
	{
		return false;
	}
	cin>>*n;
	for(int i=0;i<*n;i++)
	{
		char t;
		cin>>it[i].value>>t>>it[i].weight;
	}
	return true;
}

item max(item a, item b)
{
	if(a.value>b.value)
	{
		return a;
	}
	else
	{
		return b;
	}
}

item add(item a, item b)
{
	a.value+=b.value;
	a.weight+=b.weight;
	return a;
}

item table[1000][1000];

item solve(int c, int n, item it[1000])
{
	if(table[c][n].flag!=true){
		if(c==0||n==0)
		{
			table[c][n].value=0;
			table[c][n].weight=0;
		}
		else if(c<it[n-1].weight)
		{
			table[c][n]=solve(c,n-1,it);
		}
		else
		{
			table[c][n]=max(solve(c,n-1,it),add(solve(c-it[n-1].weight,n-1,it),it[n-1]));
		}
		table[c][n].flag=true;
	}
	return table[c][n];
}

int main(void)
{
	for(int i=1;;i++)
	{	
		int c,n;
		item it[1000];
		if(input(&c,&n,it)==false)
		{
			break;
		}
		for(int k=0;k<1000;k++)
		{
			for(int j=0;j<1000;j++)
			{
				table[k][j].flag=false;
				table[k][j].value=0;
				table[k][j].weight=0;
			}
		}
		item ans=solve(c,n,it);
		cout<<"Case "<<i<<':'<<endl<<ans.value<<endl<<ans.weight<<endl;
	}
	return 0;
}

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

using namespace std;

int main(void) {
    for (int c = 1; ; c++) {
        int W, N;
        cin >> W;
        if (W == 0) break;
        cin >> N;
        int v[N], w[N], dp[N + 1][W + 1];
        memset(dp, 0, sizeof(dp));
        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++) {
                if (w[i] > j) {
                    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 " << c << ":" << endl;
        for (int j = W; j > 0; j--) {
            if (dp[N][j] > dp[N][j - 1]) {
                cout << dp[N][j] << endl;
                cout << j << endl;
                break;
            }
        }
    }
    return 0;
}

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

struct Item{
  int v,w;
};

int main() {
  int W;    
  Item item[1001];
  int dp[1001][1001];
  int cnt = 0;
  while( cin >> W , W ) {
    int n; cin >> n;
    char ch;

    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++ ) {
      cin >> item[i].v >> ch >> item[i].w;
    }

    for ( int i = 1; i <= n; i++ ) {
      for ( int j = 1; j <= W; j++ ) {
	if ( item[i].w <= j ) {
	  if ( item[i].v + dp[i-1][ j - item[i].w ] > dp[i-1][j] ) 
	    dp[i][j] = item[i].v + dp[i-1][ j - item[i].w ];
	  else dp[i][j] = dp[i-1][j];
	}else dp[i][j] = dp[i-1][j];
      }
    }
    
    /* for ( int i = 1; i <= n; i++ ) {
      for ( int j = 1; j <= W; j++ ) {
	cout << dp[i][j] << " ";
      }
      cout << endl;
    }
    */
    cnt++;
    cout << "Case" << " " << cnt << ":" << endl;
    cout << dp[n][W] << endl;
    for ( int i = 0; i <= W; i++ ) {
      if ( dp[n][i] == dp[n][W] ) {
	cout << i << endl;
	break;
      }
    }
  }
  return 0;
}

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

int main(){
	int dataset = 1, w;
	while (cin >> w, w){
		int dp[1001], n, treas_val[1000], treas_w[1000], ans_val = 0, ans_w;
		for (int i = 0; i <= w; i++){
			dp[i] = -1;
		}
		cin >> n;
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &treas_val[i], &treas_w[i]);
		}
		dp[0] = 0;
		for (int i = 0; i < n; i++){
			for (int j = w; j >= 0; j--){
				if (dp[j] == -1) continue;
				if (j + treas_w[i] <= w && dp[j + treas_w[i]] < dp[j] + treas_val[i])
					dp[j + treas_w[i]] = dp[j] + treas_val[i];
			}
		}
		for (int i = 0; i <= w; i++){
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		printf("Case %d:\n%d\n%d\n", dataset, ans_val, ans_w);
	}
	return 0;
}