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<algorithm>
using namespace std;

int main(){

	int cnt, N, W, value, weight, v[1001][1001], w[1001][1001];

	cnt = 0;

	while(1){
		cin >> W;
		if(W == 0) break;
		
		cnt++;
		cin >> N;

		for(int i=0; i<=N; i++){
			for(int j=0; j<=W; j++){
				v[i][j] = 0;
				w[i][j] = 0;
			}
		}
		
		for(int i=1; i<=N; i++){
			scanf("%d, %d", &value, &weight);
			for(int j=0; j<=W; j++){
				for(int k=0; k<=j; k++){
					if(w[i-1][k] + weight <= j && v[i-1][k] + value > v[i][j]){
						v[i][j] = v[i-1][k] + value;
						w[i][j] = w[i-1][k] + weight;
					}
					else if(v[i-1][k] > v[i][j]){
						v[i][j] = v[i-1][k];
						w[i][j] = w[i-1][k];
					}
					else if(v[i-1][k] == v[i][j] && w[i-1][k] < w[i][j]){
						w[i][j] = w[i-1][k];
					}
				}
			}
		}

		cout << "Case " << cnt << ":" << endl;
		cout << v[N][W] << endl;
		cout << w[N][W] << endl; 
		
	}

	return 0;

}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;
typedef pair<int, int> pii;

int main(){
	int caseNum = 1;
	while(true){
		int W, N;
		cin >> W;
		if(W == 0){ break; }
		cin >> N;
		vector<pii> items(N);
		for(int i = 0; i < N; ++i){
			char c;
			cin >> items[i].first >> c >> items[i].second;
		}
		vector<int> dp(W + 1, -1);
		dp[0] = 0;
		for(int i = 0; i < N; ++i){
			for(int j = W; j >= 0; --j){
				if(dp[j] < 0){ continue; }
				int weight = j + items[i].second;
				if(weight > W){ continue; }
				dp[weight] = max(dp[weight], dp[j] + items[i].first);
			}
		}
		int answer = 0;
		for(int i = 0; i <= W; ++i){
			if(dp[i] > dp[answer]){ answer = i; }
		}
		cout << "Case " << caseNum++ << ":" << endl;
		cout << dp[answer] << endl << answer << endl;
	}
	return 0;
}

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

using namespace std;

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

int main()
{
    int W, N;
    char t;
    int cnt = 1;

    while(cin >> W >> N)
    {
        for (int i = 0; i < N; i++)
        {
            cin >> v[i] >> t >>  w[i];
        }

        for(int i = 0; i <= W; i++) dp[0][i] = 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]);
            }
        }

        int ans1 = 0, ans2 = 0;

        for(int i = 0; i <= W; i++)
        {
            if(dp[N][i] > ans1)
            {
                ans1 = dp[N][i];
                ans2 = i;
            }
        }

        cout << "Case " << cnt << ":" << endl;
        cout << ans1 << endl << ans2 << endl;
    }
}

C++: #include <iostream>
#include <map>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>

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

using namespace std;

int dp[1001][1001];

vector<string> split(const string &str, char delim){
  vector<string> res;
  size_t current = 0, found;
  while((found = str.find_first_of(delim, current)) != string::npos){
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int main()
{
	int count = 0;
	int W,n,ans;
	int w[1001],v[1001];

	while(cin >> W && W)
	{
		count++;
		memset(dp,0,sizeof(dp));
		cin >> n;
		rep(i,n)
		{
			string s;
			cin >> s;
			vector<string> _s = split(s,',');
			v[i] = atoi(_s[0].c_str());
			w[i] = atoi(_s[1].c_str());
		}

		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])
			{
				ans = j;
				break;
			}
		}
		cout << "Case " << count << endl;
		cout << dp[n][W] << endl << ans << endl;
	
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>
#include <ctime>
#define INF 100000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;

/*---------------------------------------------------*/

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

void DEBUG(){
  rep(i,W+1)cout<<i<<": "<<dp[N][i]<<endl;
}

void init(){
  rep(i,1005){
    rep(j,1005){
      dp[i][j]=-INF;
    }
  }
}

void DP(){
  for(int i=0;i<1005;i++)dp[0][i]=0;
  for(int i=0;i<=N;i++){
    for(int j=0;j<=W;j++){
      if(j>=w[i])dp[i+1][j]=max(dp[i][j-w[i]]+v[i],dp[i][j]);
      else dp[i+1][j]=dp[i][j];
    }
  }
}

int main(){
  int cnt=1;
  while(cin>>W){
    if(W==0)break;
    cin>>N;
    rep(i,N)scanf("%d,%d",&v[i],&w[i]);
    init();
    DP();
    //DEBUG();
    cout<<"case: "<<cnt<<":"<<endl;
    cout<<dp[N][W]<<endl;
    for(int i=W;i>=1;i--)if(dp[N][i]!=dp[N][i-1]||i==1){cout<<i<<endl;break;}
    cnt++;
  }
  
  return 0;
}

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

int main(){
	const int nmax = 100;
	char c;
	int dp[nmax+1][nmax+1];
	int n, w;
	int val[nmax+1], weight[nmax+1];
	while(cin >> w){
		if(w == 0) break;
		cin >> n;
		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 >> val[i] >> c >> weight[i];
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j <= w; j++){
				if(j < weight[i]){
					dp[i+1][j] = dp[i][j];
				}else{
					dp[i+1][j] = max(dp[i][j], dp[i][j - weight[i]] + val[i]);
				}
			}
		}
		cout << dp[n][w] << endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int W, N, v[1000], w[1000], dp[1001][1001], c, r;
int main() {
	while (~scanf("%d", &W)) {
		if (!W) break;
		scanf("%d", &N);
		for (int i = 0; i < N; i++) scanf("%d%d", &w[i], &v[i]);
		for (int i = 1; i <= N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = dp[i - 1][j];
				if (j >= w[i - 1]) {
					dp[i][j] = max(dp[i][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
				}
			}
		}
		printf("Case %d:\n", ++c);
		printf("%d\n", dp[N][W]);
		printf("%d\n", lower_bound(dp[N], dp[N] + W + 1, dp[N][W]) - dp[N]);
	}
	return 0;
}