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<vector>

using namespace std;

int N,W;

struct Treasurea
{

	int value;
	int weight;
	int flag;

	Treasurea()
	{

		value = 0;
		weight = 0;
		flag = false;

	}

};

struct Memo
{

	Treasurea record;
	bool flag;

	Memo()
	{

		flag = false;

	}

};

Memo memo[1001][1001];

Treasurea knapsack(vector<Treasurea> &treasurea, int correntWeight, int correntValue, int kind)
{

	if(correntWeight < 0)
	{

		Treasurea temp;
		temp.value = correntValue - treasurea[kind - 1].value;
		temp.weight = W - (correntWeight + treasurea[kind - 1].weight);
		return temp;

	}
	else if(treasurea.size() == kind)
	{

		Treasurea temp;
		temp.value = correntValue;
		temp.weight = W - correntWeight;
		return temp;

	}
	else if(memo[correntWeight][kind].flag) return memo[correntWeight][kind].record;


	Treasurea total;

	for(int i = 0; i < N; ++i)
	{

		if(treasurea[i].flag) continue;
			treasurea[i].flag = true;

		Treasurea temp = knapsack(treasurea,correntWeight - treasurea[i].weight, correntValue + treasurea[i].value, i + 1);

		if( (total.value < temp.value) || (total.value == temp.value && total.weight > temp.weight))
			total = temp;

		treasurea[i].flag = false;

	}

	memo[correntWeight][kind].record = total; 
	memo[correntWeight][kind].flag = true;

	return total;

}

int main()
{

	int case_count = 0;
	
	while(cin >> W && W != 0)
	{
		
		cin >> N;

		vector<Treasurea> treasurea(N);
		Treasurea result;

		for(int i = 0; i < N; ++i)	scanf("%d,%d",&treasurea[i].value,&treasurea[i].weight);			

		result = knapsack(treasurea,W,0,0);

		cout << "Case " << ++case_count << ":" << endl << result.value << endl << result.weight << endl;

		treasurea.erase(treasurea.begin(),treasurea.end());

	}

}

C++: #include<stdio.h>
#include<string.h>
#include<math.h>
int a,s,d[1001],f[1001],g[1001][1001],h,i,j,z,x=1;
int main(void)
{
	while(1){
		scanf("%d",&a);
		if(a==0)	break;
		scanf("%d",&s);
		for(i=0;i<s;i++)
			scanf("%d,%d",&d[i],&f[i]);
		for(i=0;i<s;i++){
			for(j=0;j<a;j++)
				g[i][j]=-1;
		}
		g[0][0]=0;
		for(i=0;i<s;i++){
			for(j=0;j<=a;j++){
				if(g[i][j]!=-1){
					if(g[i][j]>g[i+1][j])
						g[i+1][j]=g[i][j];
					if(j+f[i]<=a){
						if(g[i][j]+d[i]>g[i+1][j+f[i]])
							g[i+1][j+f[i]]=g[i][j]+d[i];
					}
				}
			}
		}
		for(i=0;i<s;i++){
			for(j=0;j<=a;j++)
				printf("%d ",g[i][j]);
			printf("\n");
		}
		h=0;
		for(i=0;i<=a;i++){
			if(h<g[s][i]){
				h=g[s][i];
				z=i;
			}
		}
		printf("Case %d:\n",x);
		printf("%d\n%d\n",h,z);
		x++;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define pb push_back
#define MAX 100005
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;



signed main(){
  int W,N;
  int c=1;
  while(cin>>W,W){
    cin>>N;
    vector<int> v(N),w(N);
    char x;
    vector<vector<int> > dp(N+1,vector<int>(W+1));
    rep(i,N)cin>>v[i]>>x>>w[i];
    loop(i,1,N+1){
      rep(j,W+1){
        if(j-w[i-1]>=0){
          dp[i][j]=max(dp[i-1][j-w[i-1]]+v[i-1],dp[i-1][j]);
        }else{
          dp[i][j]=dp[i-1][j];
        }
      }
    }
    int val=dp[N][W],wei;
    rep(i,W+1){
      if(dp[N][i]==val){
        wei=i;
        break;
      }
    }
    cout<<"Case "<<c<<':'<<endl;
    cout<<val<<endl;
    cout<<wei<<endl;
    c++;
  }

  return 0;
}

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

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 <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <cstdlib>
#define INF 100000000
#define INFLL (long long)1e14
#define Loops(i, n) for(int i = 0; i < (int)n; i++)
#define Loops1(i, n) for(int i = 1; i <= (int)n; i++)
int dp[2000][2000];
int w[2000];
int v[2000];
int N;
int W;
int count=1;
int kati=0,omosa=0;
int main(void){
	while(1){
		std::cin>>W;
		if(W==0){
			break;
		}
		std::cin>>N;
		Loops(i,N){
			scanf("%d,%d",&v[i],&w[i]);
//			std::cin>>w[i]>>v[i];
		}
		Loops(i,1000){
			Loops(j,1000){
				dp[i][j]=0;
			}
		}
		Loops(i,N){
			Loops(j,W+1){
				if(j-w[i]>=0){
					dp[i+1][j]=std::max(dp[i][j],dp[i][j-w[i]]+v[i]);
				}else {
					dp[i+1][j]=dp[i][j];				
				}
			}
		}
		Loops(i,W+1){
			if(kati<dp[N][i]){
				kati=dp[N][i];
				omosa=i;
			}
		}
		std::cout<<"Case 1:"<<std::endl;
		std::cout<<kati<<std::endl;
		std::cout<<omosa<<std::endl;
		count++;
	}
/*	Loops(i,N+1){
		Loops(j,W+1){
			std::cout<<dp[i][j]<<" ";
		}
		std::cout<<std::endl;
	}*/
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(){
    int W,N;
    for(int ii=1;;ii++){
        scanf("%d",&W);
        if(W==0) break;
        int values[W+1];
        for(int i=0;i<=W;i++){
            values[i]=0;
        }
        scanf("%d",&N);
        int v,n;
        for(int i=0;i<N;i++){
            scanf("%d,%d",&v,&n);
            for(int j=W;j>=n;j--){
                values[j]=max(values[j],values[j-n]+v);
            }
        }
        int maxw,maxv=-1;
        for(int i=0;i<=W;i++){
            if(values[i]>maxv){
                maxw=i;
                maxv=values[i];
            }
        }
        printf("Case %d:\n%d\n%d\n",ii,maxv,maxw);
    }
}