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