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<string>
int dp[2000][2000];
using namespace std;
int main(){
	int t,n;
	int w[2000],v[2000];
	int k=0;
	char z;
	while(true){
	cin>>t;
	if(t==0)
		break;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<=t;j++){
			dp[i][j]=-1;
		}
	}
	for(int i=0;i<n;i++){
		cin>>v[i]>>z>>w[i];
		dp[i][0]=0;
	}
	dp[0][w[0]]=v[0];
	for(int i=1;i<n;i++){
		for(int j=1;j<=t;j++){
			if(j-w[i]<0){
				if(dp[i-1][j]==-1)
					dp[i][j]=-1;
				if(dp[i-1][j]!=-1)
					dp[i][j]=dp[i-1][j];
			}
			if(j-w[i]>=0){
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]==-1)
					dp[i][j]=-1;
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]!=-1)
					dp[i][j]=dp[i-1][j-w[i]]+v[i];
				if(dp[i-1][j]!=-1&&dp[i-1][j-w[i]]==-1)
					dp[i][j]=dp[i-1][j];
				if(dp[i-1][j]!=-1&&dp[i-1][j-w[i]]!=-1){
					if(dp[i-1][j]>=dp[i-1][j-w[i]]+v[i])
						dp[i][j]=dp[i-1][j];
					if(dp[i-1][j]<dp[i-1][j-w[i]]+v[i])
						dp[i][j]=dp[i-1][j-w[i]]+v[i];
				}
			}
		}
	}
	int c=0;
	for(int j=0;j<=t;j++){
		if(dp[n-1][j]>c)
			c=dp[n-1][j];
	}
	k++;
	bool g=false;
	for(int j=0;j<=t;j++){
		if(c==dp[n-1][j]&&g==false){
			cout<<"Case "<<k<<":"<<endl;
			cout<<c<<endl;
			cout<<j<<endl;
			g=true;
		}
	}
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 28)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int W, N;
int tres[1001];
int weig[1001];
int maxW, maxV;
int dp[1001][1001];

void dfs(int weight, int value, int index){
	if(weight > W)return;
	if(dp[weight][index]>value)return;
	dp[weight][index] = value;
	if(index == N){
		if(maxV < value){
			maxW = weight;
			maxV = value;
		}else if(maxV == value){
			if(maxW > weight){
				maxW = weight;
				maxV = value;
			}
		}
		return;
	}
	dfs(weight, value, index+1);
	dfs(weight+weig[index], value+tres[index], index+1);
}


int main(void){
	int cnt = 0;
	while(cin >> W, W){
		cnt++;
		cin >> N;
		maxW = 0;
		maxV = 0;
		//fill_n(tres, 1001, 0);
		//fill_n(weig, 1001, 0);
		rep(i,W){
			rep(j,N){
				dp[i][j] = -1;
			}
		}

		rep(i,N){
			int n, w;
			scanf("%d,%d", &n, & w);
			tres[i] = n;
			weig[i] = w;
		}

		dfs(0,0,0);
		cout << "Case " << cnt << ":" << endl;
		cout << maxV << endl << maxW << endl;
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

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

int N,W ;
int m[1010][2] ;
int dp[1010][1010] ;
int cnt = 0 ;

int main(){
	
	while( cin >> W , W ){
		cin >> N ;
		cnt++;
		for( int i=0 ; i<1010 ; i++ )
			for( int j=0 ; j<1010 ; j++ )
				dp[i][j] = 0 ;
		
		for( int i=1 ; i<=N ; i++ ){
			scanf("%d,%d",&m[i][0] ,&m[i][1]) ;
		}
		
		for( int i=1 ; i<=N ; i++ ){
			for( int j=0 ; j<=W ; j++ ){
				if( j-m[i][1] >= 0 ){
					dp[i][j] = max( dp[i-1][j] , dp[i-1][j-m[i][1]] + m[i][0] ) ;
				}
			}
		}
		
		int best[2] = {} ;
		for( int j=0 ; j<=W ; j++ ){
			if( best[0] < dp[N][j] ){
				best[0] = dp[N][j] ;
				best[1] = j ;
			}
		}
		
		cout << "Case " << cnt << ':' << endl ;
		cout << best[0] << endl ;
		cout << best[1] << endl ;
	}
	
return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<algorithm>
#include<queue>
#include <functional>
using namespace std;

int main(){
  int w,n,t[1001][2];
  int memo[1111]={0};
  int ans=0;
  int omosa,kati;
  int nam=1;
  char a;
  
  while(1){
    ans=0;
    for(int i=0;i<1111;i++){
      memo[i]=0;
    }
    for(int i=0;i<1001;i++){
      for(int j=0;j<2;j++){
	t[i][j]=0;
      }
    }
    cin >> w;
    if(w == 0)break;
    cin >> n;
    for(int i=1;i<=n;i++){
      cin >> t[i][0] >> a >> t[i][1];
    }
    for(int i=0;i<=n;i++){
      omosa=t[i][1];
      kati=t[i][0];
      for(int j=i+1;j<=n;j++){
	if(omosa+t[j][1] < w){
	  omosa+=t[j][1];
	  kati+=t[j][0];
	  if(ans<kati){
	    ans=kati;
	  }
	  if(memo[omosa]<kati){
	    memo[omosa]=kati;
	  }
	}
      }
    }
    for(int i=0;i<=w;i++){
      if(memo[i]==ans){
	printf("Case %d:\n",nam);
	cout << ans << endl;
	cout << i << endl;
	nam++;
	break;
      }
    }
  }
}

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

int dp[1001][1001];

int main(){
	int case_number = 1;
	while(1){
		int W, N;
		scanf("%d%d", &W, &N);
		if(W == 0) break;

		int v, w;
		for(int i = 1; i <= N; ++i){
			scanf("%d,%d", &v, &w);
			for(int j = w; j <= W; ++j){
				dp[i][j] = max(dp[i-1][j], dp[i-1][j-w] + v);
			}
		}

		int answ = 0;
		for(int j = 0; j <= W; ++j){
			if(dp[N][j] > dp[N][answ]){
				answ = j;
			}
		}

		printf("Case %d:\n%d\n%d\n", case_number, dp[N][answ], answ);
		++case_number;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[1002];
int W,w[1001],v[1001],n,cnt = 1,wm;
int main(){
  while(1){
    memset(dp, 0, sizeof(dp));
    cin >> W;
    if(W == 0) break;
    cin >> n;
    for(int i = 0; i < n; i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    for(int i = 0; i < n; i++){
      //dp[w[i]] = v[i];
      for(int j = W; j >= 0; j--){
	if(j + w[i] <= W) dp[j + w[i]] = max(dp[j + w[i]],dp[j] + v[i]);
      }
    }
    wm = 0;
    for(int i = 0; i <= W; i++){
      if(dp[i] > dp[wm]) wm = i;
    }
    cout << "Case " << cnt <<  ":" << endl;
    cout << dp[wm] << endl;
    cout << wm << endl;
    cnt++;
  }
}