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 <iomanip>
#include <sstream>
#include <stdio.h>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>											
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
typedef pair<int, int> P;
int W, N;
int v[1005]; int w[1005];
P dp[1005][1005];

P solve(int i, int j)
{
	if (i == N) return P(0, 0);
	if (dp[i][j].first >= 0) return dp[i][j];
	if (j < w[i]) return (solve(i + 1, j));
	else
	{
		P a = solve(i + 1, j); P b = solve(i + 1, j - w[i]);
		b.first += v[i]; b.second += w[i];
		if (a.first != b.first) return max(a, b);
		else
		{
			if (a.second>b.second) return b;
			else return a;
		}
	}
}

int main()
{
	int cnt = 0;
	char c = ',';
	while (cin >> W, W)
	{
		memset(dp, -1, sizeof(dp));
		cnt++;
		cin >> N;
		REP(i, N) cin >> v[i] >> c >> w[i];
		cout << "Case :" << cnt << endl << solve(0, W).first << endl << solve(0, W).second << endl;
	}
	return 0;
}

C++: #include <string>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <algorithm>
#include <numeric>
#include <iostream>

using namespace std;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

int W,N;
int c[1000],w[1000];
int t[10000+10];

int main()
{
	for (int i = 1; ; i++){
		cin>>W;
		if(W == 0) break;
		cin>>N;
		for(int j =0 ; j < N; j++) scanf("%d,%d",c+j,w+j);
		printf("Case%d:\n",i);
		memset(t,-1,sizeof(t));
		t[0] = 0;
		for (int i = 0; i < N; i++){
			for (int j = W - w[i]; j >= 0; j--){
				if(t[j] == -1) continue;
				t[j+w[i]] = max(t[j+w[i]],t[j] + c[i]);
			}
		}
		int ans_id = max_element(t,t+W+1) - t;
		printf("%d\n%d\n",t[ans_id],ans_id);
	}
}

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

#define fin cin

using namespace std;
 
int main(void){

   // ifstream fin("input2.txt");

    int Case = 1;
    int maxWeight;
 
    //??????????????????
    while(fin >> maxWeight, maxWeight) {

        int n, sumWeight = 0;
        int w[1000], v[1000];
        int dp[1001] = {0};
        fin >> n;
        for(int i = 0; i < n; i++){

            //value
            string str_in;
            getline(fin, str_in, ',');
            v[i] = stoi(str_in);

            //weight
            getline(fin, str_in);
            w[i] = stoi(str_in);
        }

        //?????????
        for(int i = 1; i <= maxWeight; i++)
            dp[i] = -1;
 
        for(int i = 0; i < n; i++) {
            for(int j = maxWeight - w[i]; j >= 0; j--) {
                if(0 <= dp[j] && j + w[i] <= maxWeight) {
                    dp[j + w[i]] = ((dp[j + w[i]] > dp[j] + v[i]) ? dp[j + w[i]] : (dp[j] + v[i]));
                }
            }
        }
 
        for(int i = 0; i <= maxWeight; i++)
            if(dp[i] > dp[sumWeight]) sumWeight = i;
 
        cout << "Case " << Case++ << ":" << endl;
        cout << dp[sumWeight] << endl;
        cout << sumWeight << endl;
    }
 
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int dp[1001][1001];
  int n,W;
  int v[1000],w[1000];
  int num=0;
  char c;
  int cnt=0;
  while(1){
    cin>> W;
    if(W==0) break;
    cin>> n;
    cnt++;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=W;j++){
	dp[i][j]=0;
      }
    }
    for(int i=0;i<n;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }

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

    int ansv=0,answ=0;
    for(int i=1;i<=W;i++){
      if(ansv<dp[n][i]){
	ansv=dp[n][i]; answ=i;
      }
    }
    printf("Case %d:\n%d\n%d\n",cnt,ansv,answ);
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

int dp[1010][1010][2];

int main() {
	int w,n,num=0;;
	while(w=in(),n=in(),w){
		memset(dp,0,sizeof(dp));
		int a,b,i;
		for(i=0;i<n;i++){
			scanf("%d,%d",&a,&b);
			int k;
			for(k=b;k<=w;k++){
				if(dp[i][k][0]<dp[i][k-b][0]+a
				||dp[i][k][0]==dp[i][k-b][0]+a&&dp[i][k][1]>dp[i][k-b][1]+b){
					dp[i+1][k][0]=dp[i][k-b][0]+a;
					dp[i+1][k][1]=dp[i][k-b][1]+b;
				}else{
					dp[i+1][k][0]=dp[i][k][0];
					dp[i+1][k][0]=dp[i][k][1];
				}
			}
			//cout<<dp[w]<<endl;
		}
		cout<<"Case "<<++num<<":"<<endl;
		cout<<dp[n][w][0]<<endl;
		cout<<dp[n][w][1]<<endl;
	}
	return 0;
}

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

using namespace std;

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

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 solve(int c, int n, item it[1000])
{
	vector< vector<item> > table(n+1,vector<item>(c+1));
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=c;j++)
		{
			if(j==0||i==0)
			{
				table[i][j].value=0;
				table[i][j].weight=0;
			}
			else if(j<it[i-1].weight)
			{
				table[i][j]=table[i-1][j];
			}
			else
			{
				table[i][j]=max(table[i-1][j],add(table[i-1][j-it[i-1].weight],it[i-1]));
			}
		}
	}
	return table[n][c];
}

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