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<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const int MAX_V=105;
int INF = 1000000000;
  
int main(){
  int n=1;
  int W,N,i,j;
  while(cin >> W ,W){
    cin >> N;
    int dp[N+1][W+1];
    rep(i,N+1){
      rep(j,W+1){
        dp[i][j]=0;
      }
    }

    rep(i,N){
      int v,w;
      char c;
      cin >> v >> c >> w;
      rep(j,W+1){
        dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
        if(j+w<W+1){
        dp[i+1][j+w]=max(dp[i][j]+v,dp[i+1][j+w]);
        }
      }
    }
    int value1=0;
    int weight=0;
/*    rep(j,W+1){
      if(dp[N][j]>value1){
        value1=dp[N][j];        
      }
}*/
    rep(j,W+1){
      if(dp[N][j]==dp[N][W]){
        weight=j;
        break;
      }
    }
    
    cout << "Case" << ' ' << n++ << ":" << endl;
    cout << dp[N][W]<< endl;
    cout << weight << endl;  
    
  }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<string.h>
using namespace std;

int main(){
    int w,n,va,we;
    int dp[1000];
    char c;
    vector<pair<int,int> >vec;
    
    while(cin>>w>>n,w){
        for(int i=0;i<n;i++){
            cin>>va>>c>>we;
            vec.push_back(make_pair(va, we));
        }
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++){
            int tv=vec[i].first, tw=vec[i].second;
            for(int j=w;j>=0;j--){
                if(tw+j<=w){
                    dp[tw+j]=max(dp[tw+j],dp[j]+tv);
                }
            }
        }
        int aw=0,av=0;
        for(int i=0;i<=w;i++) {
            if(dp[i]>av){
                aw=i;
                av=dp[i];
            }
        }
        cout<<av<<endl<<aw<<endl;
        vec.clear();
    }
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define latte WORLD

struct Data{
    int v,w;
    Data(){}
    Data(int _v,int _w){
        v=_v; w=_w;
    }
    bool operator <(const Data &a)const{
        if(v==a.v) return w>a.w;
        return v<a.v;
    }
    Data operator +(const Data &a)const{
        return Data(v+a.v,w+a.w);
    }
};

int N,W;
Data t[1000],memo[1000][1001];

Data latte(int n,Data st){
    if(st.w>W) return Data(-1,-1);
    if(n==N) return st;
    if(memo[n][st.w].v!=-1) return memo[n][st.w];
    memo[n][st.w] = max(latte(n+1,st+t[n]),latte(n+1,st));
    return memo[n][st.w];
}

int main(){
    for(int ds=1;cin>>W,W;ds++){
        cin>>N;
        for(int i=0;i<N;i++) scanf("%d,%d",&t[i].v,&t[i].w);
        for(int i=0;i<1000;i++)for(int j=0;j<1001;j++) memo[i][j] = Data(-1,-1);
        Data ans = latte(0,Data(0,0));
        printf("Case %d:\n%d\n%d\n",ds,ans.v,ans.w);
    }
}

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

using namespace std;

int main(){

	int W;
	int n;


	int cs=0;
	while(cin>>W&&W!=0){
		int v[1001];
		int w[1001];
		
		int dp[1001][1001];
		cin>>n;
		for(int i = 0; i < n; i++){
			char cm;
			cin>>v[i]>>cm>>w[i];
		}

		for(int i = 0; i < n; i++){
			for(int j = 0; j <= 1000; j++){
				if(i==0){
					if(j==w[0]){
						dp[i][j]=v[i];
					}
					else{
						dp[i][j]=0;
					}
				}
				else{
					if(j-w[i]>=0){
						dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
					}
					else{
						dp[i][j]=dp[i-1][j];
					}
				}
			}
		}
		int idx=0;
		int maxNum=-1;
		for(int i = 0; i <= W; i++){
			if(maxNum<dp[n-1][i]){
				maxNum=dp[n-1][i];
				idx=i;
			}
		}
		cout<< "Case " << cs+1 << ':' << endl;
		cout<<maxNum<<endl;
		cout<<idx<<endl;

		cs++;
	}

	return 0;
}

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

using namespace std;

int main()
{

    int w, cas = 1;

    while (cin >> w, w) {
        int n, max_idx, max_value;
        int dp[1001][1001] = {0};
        int we[1001], v[1001];
        char ch;

        cin >> n;
        for (int i = 0; i < n; i++)
            cin >> v[i] >> ch >> we[i];

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

        max_idx = max_value = 0;
        for (int j = 0; j <= w; j++)
            if (max_value < dp[n][j]) {
                max_value = dp[n][j];
                max_idx = j;
            }
        
        cout << "Case " << cas++ << ":\n" << max_value << '\n' << max_idx << endl;
    }

    return 0;
}

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

int dp[1001];
int main() {
  int w,n,cnum=0;
  while(cin>>w, w) {
    cin>>n;
    vector<int> chika(n),miu(n);
    for(int i=0; i<n; ++i) scanf("%d,%d", &chika[i], &miu[i]);
    for(int i=0; i<1001; ++i) dp[i] = 0;


    for(int i=0; i<n; ++i) {
      for(int j=0; j<1001; ++j) {
	if(j + chika[i] < w) continue;
	if(j == 0 || dp[j] > 0) dp[j+miu[i]] = dp[j] + chika[i];
      }
    }

    int maxc = 0,mi = -1;
    for(int j=w; j>=0; --j) {
      if(maxc <= dp[j]) {
	mi = j;
	maxc = dp[j];
      }
    }

    cout<<"Case "<<++cnum<<":"<<endl;
    cout<<maxc<<endl;
    cout<<mi<<endl;
  }
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define MP make_pair
#define PB push_back
#define REP(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long LL;
typedef vector<int> VI;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int dp[1001][1001];

int main(){
	int W,N,lp=1;
	while(true){
		cin >> W;
		if(W == 0) break;

		cin >> N;
		vector<pair<int,int> > data;
		REP(i,N){
			int a,b;
			char c;
			cin >> a >> c >> b;
			data.PB(MP(a,b));
		}

		

		REP(i,N+1){
			REP(j,W+1){
				dp[i][j] = -1;
			}
		}
		dp[0][0] = 0;

		REP(i, N){
			REP(j, W+1){
				if(dp[i][j] != -1 && data[i].second+j <= W){
					dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
					dp[i+1][data[i].second+j] = max(dp[i+1][data[i].second+j], dp[i][j]+data[i].first);
				}
			}
		}

		int ans1=0, ans2=0;
		REP(i,W+1){
			if(ans1 < dp[N][i]){
				ans1 = dp[N][i];
				ans2 = i;
			}
		}
		cout << "Case " << lp << ":" << endl;
		cout << ans1 << endl;
		cout << ans2 << endl;
		lp++;
	}
}