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<stdio.h>
#include<algorithm>
using namespace std;

int main(){
    int W,N;
    int cnt=1;
    while(true){
        cin>>W;
        if(W==0)break;
        cin>>N;
        int treasure[N+1][2];
        for(int i=1;i<N+1;i++)
            scanf("%d,%d",&treasure[i][0],&treasure[i][1]);
        
        int table[N+1][W+1];
        for(int i=0;i<N+1;i++)
            for(int j=0;j<W+1;j++)
                table[i][j]=0;
        
        for(int i=1;i<N+1;i++){
            for(int j=1;j<W+1;j++){
                if(j<treasure[i][1]){table[i][j] = table[i-1][j];continue;}
                int new_value = table[i-1][j-treasure[i][1]] + treasure[i][0];
                table[i][j] = max(new_value,table[i-1][j]);
            }
        }

        int m=0,w=0;
        for(int i=0;i<W+1;i++){
            if(table[N][i]>m){m=table[N][i];w=i;}
        }
        cout<<"Case "<<cnt<<":"<<endl;
        cout<<m<<endl<<w<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>

using namespace std;

int w[1001];
int v[1001];

int dp[1001][10001];

int W,N;

int main(){
    int t=1;
    while(cin >> W, W){
        memset(dp, 0, sizeof(dp));
        memset(w, 0, sizeof(w));
        memset(v, 0, sizeof(v));
        cin >> N;
        for(int i=0; i<N; i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        cout << "Case " << t++ << ":" << endl;

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

        
        int i = N, j = W;
        int wei = 0;
        while(i != 0){
            if(w[i-1] <= j){
                int take = dp[i-1][j - w[i-1]] + v[i-1];
                if(take == dp[i][j]){
                    wei += w[i-1];
                    j -= w[i-1];
                }
            }
            i--;
        }
        cout << wei << endl;
    }
}

C++: 
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
using namespace std;
#define rep(i, j) for(int i = 0; i < j; i++)
#define f(i, k, j) for(int i = k; i < j; i++)
#define P pair<int, int>
const int INF = (1 << 26);
const int MAX_N = 1001;
const int MAX_W = 1001;

int w, n, dp[MAX_N][MAX_W];
vector<P> vec;

void solve();

int main(){
    int c = 1;
    while(cin >>w && w != 0){
        cin >>n;
        vec.clear();
        rep(i, n){
            int a, b;
            scanf("%d,%d", &a, &b);
            //first = 価値, second = 重さ
            vec.push_back( P(a, b) );
        }
        solve();
        int ans = -1, ansi = 0;
        rep(i, w + 1){
            if(dp[n][i] > ans){
                ans = dp[n][i];
                ansi = i;
            }
        }
        cout <<"Case " <<c <<":" <<endl;
        cout <<ans <<endl <<ansi <<endl;
        c++;
    }
    return 0;
}

void solve(){
    memset(dp, 0, sizeof(dp));
    rep(i, w) dp[0][i] = 0;

    rep(i, n){
        //cout <<"! " <<vec[i].first <<", " <<vec[i].second <<endl;
        rep(j, w + 1){
            if(j < vec[i].second) dp[i + 1][j] = dp[i][j];
            else dp[i + 1][j] = max(dp[i][j], dp[i][j - vec[i].second] + vec[i].first);
        }
        //rep(i, n){
        //    cout <<":";
        //    rep(j, w + 1){
        //        cout <<dp[i][j] <<", ";
        //    }
        //    cout <<endl;
        //}
    }
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  int w,n,tre[1001][2],dp[1001][1001] = {0},x;
  for(int c = 1; cin >>w,w; c++){
    cin >>n;
    for(int i=1; i<=n; i++){
      scanf("%d,%d",&tre[i][0],&tre[i][1]);
    }
    for(int i=1; i<=n; i++){
      for(int j=1; j<=w; j++){
	dp[i][j] = dp[i-1][j];
	if(j>=tre[i][1]){dp[i][j] = max(dp[i][j],dp[i-1][j-tre[i][1]]+tre[i][0]);}
      }
    }
    for(int j=0; j<w; j++){
      if(dp[n][j] == dp[n][w]){x = j;break;}
    }
    cout <<"Case "<<c<<":"<<endl<<dp[n][w]<<endl<<x<<endl;
  }
  return 0;
}

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

int main()
{
	int w, n, i, j, cs, val[1001], wg[1001], dp[1001], inserted[1001];
	cs = 1;
	
	while( cin >> w , w)
	{
		memset( dp, 0, sizeof(dp));
		memset( val, 0, sizeof(val));
		memset( wg, 0, sizeof(wg));
		memset( inserted, 0, sizeof(inserted));
		
		inserted[0] = 1;
		
		cin >> n;
		
		for( i = 1; i <= n; i++)
		{	
			char c;
			cin >> val[i] >> c >> wg[i];
		}
		
		
		for( j = 1; j <= n; j++)
			for( i = 0; i <= w; i++)
				if( inserted[i] && i+wg[j] <= w && dp[i+wg[j]] < dp[i] + val[j] )
				{
						dp[i+wg[j]] = max( dp[i] + val[j], dp[i+wg[j]] );
						
						inserted[i+wg[j]] = 1;
						
						i = (i+wg[j]) + 1;
				}
		
		for( i = w; i > 0; i--)
		{
			if( dp[i] > dp[i-1] )
			{
				cout << "case" << cs << ":" << endl;
				cout << dp[i] << endl;
				cout << i << endl;
				cs++;
				break;
			}
		}
		
	}
	
	return 0;
}