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>
using namespace std;

int main(void){
  int n,mw,cnt = 1;
  while(cin>>mw,mw!=0){
    cin>>n;
    printf("Case %d:\n",cnt++);
  int w[100],v[100];
  int dp[1000][1000];
  int dp1[1000][1000];
  for(int i = 0 ; i < n ; i ++)
    scanf("%d,%d",&v[i],&w[i]);
  for(int j = w[0] ; j < mw + 1 ; j ++)
    dp[0][j] = v[0],dp1[0][j] = w[0];
  for(int i = 1 ; i < n ; i ++){
    for(int j = 0 ; j < w[i] ; j ++){
      dp[i][j] = dp[i-1][j];
      dp1[i][j] = dp1[i-1][j];
    }
    for(int j = w[i] ; j < mw + 1 ; j ++){
      dp[i][j] = max(dp[i-1][j] , dp[i-1][j-w[i]] + v[i]);    
      if(dp[i-1][j] < dp[i-1][j-w[i]] + v[i]){
	// update
	dp1[i][j] = dp1[i-1][j-w[i]] + w[i];
      }else{
	dp1[i][j] = dp1[i-1][j];
      }
    }
  }
  int res = -1,resw = -1;
  for(int i = 0 ; i < mw+1 ; i ++){
    if(res < dp[n-1][i]){
      res = dp[n-1][i];
      resw = dp1[n-1][i];
    }else if(res == dp[n-1][i]){
      resw = min(dp1[n-1][i],resw);
    }
  }
  /*
  for(int i = 0 ; i < n  ; i ++){
    for(int j = 0 ; j < mw + 1; j ++)
      cout<<dp1[i][j]<<" ";
    cout<<endl;
  }
  //*/
  cout<<res<<endl<<resw<<endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define MAX_V 10000
#define MAX_W 10000
int W,N;
long int dp[MAX_V+1][MAX_W+1];
int w[MAX_W+1];
long int v[MAX_V+1];
void solve(){
  int cnt = 0;
  vector<int> data(N,0);
  for(int i = N - 1 ; i >= 0 ; i --){
    
    for(int j = 0 ; j <= W ; j ++){
    data[i]++;
      if(j<w[i]){
	data[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]);

	if(dp[i+1][j] < dp[i+1][j-w[i]] + v[i]){
	  data[i]--;
	}
      }
    }
  }
  rep(i,N){
    if(data[i]==0){
      cnt+= w[i];
    }
  }
  printf("%ld\n",dp[0][W]);
  cout<<cnt<<endl;
}

int main(void){
  int cnt = 0;
  while(cin>>W,W){
    cout<<"Case "<<++cnt<<":"<<endl;
    cin>>N;
    for(int i = 0 ; i < N ; i ++){
      scanf("%ld,%d",&v[i],&w[i]);
    }
    solve();
    
  }
  
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int N, W, v[1002], w[1002]; // v[i],w[i] := iツ氾板姪堋づ個品ツ閉ィツづ個可ソツ値ツ、ツ重ツつウ
int dp[1002][1002]; // dp[i][j] := iツ氾板姪堋づ個品ツ閉ィツづ慊づ考ツ猟カツつオツつスツ篠楪づ個、ツ重ツつウjツづ可つィツつッツづゥツ催妥・ツづ個可ソツ値
void solve() {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= W; ++j) {
      if (j + w[i] <= W)
        dp[i+1][j+w[i]] = max(dp[i][j+w[i]], max(dp[i][j]+v[i], dp[i+1][j+w[i]]));
      else
        dp[i+1][j+w[i]] = max(dp[i][j+w[i]], dp[i+1][j+w[i]]);
    }
  }
  int *itr = max_element(dp[N], dp[N]+W);
  printf("%d\n%d\n", *itr, itr - dp[N]);
}

int main() {
  int z = 1;
  while (scanf("%d",&W),W) {
    scanf("%d",&N);
    rep(i,N) scanf("%d,%d",v+i,w+i);
    printf("Case %d:\n", z);
    solve();
    ++z;
  }
  return 0; 
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1000
#define W 1000
int dp[N+1][W+1];
int value[N+1], weight[N+1];
int main()
{
    for (int I=1; ; ++I) {
        memset(dp, 0, sizeof(dp));
        int w; scanf("%d", &w); if (!w) break;
        int n; scanf("%d", &n);
        for (int i=0; i<n; ++i) scanf("%d,%d", value+i, weight+i);

        for (int i=1; i<=n; ++i) {
            for (int j=1; j<=w; ++j) {
                if (j >= weight[i-1] )
                    dp[i][j]
                        = max(dp[i][j], dp[i-1][j-weight[i-1]] + value[i-1]);
                else
                    dp[i][j] = max(dp[i][j], dp[i-1][j]);
            }
        }
        int resv = 0, resw;
        for (int i=w; i>=1; --i)
            if (resv <= dp[n][i]) { resv = dp[n][i], resw = i; }
        printf("Case %d:\n%d\n%d\n", I, resv, resw);
    }
    return 0;
}

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

int a[1001] = { 0 };
int b[1001] = { 0 };
int w, n, mv, mw;
void solve(int W, int f, int sum) {
	f++;
	for (int i = f; i < n; i++) {
		if (W - b[i] < 0) {
			if (sum == mv) {
				mw = min(w - W, mw);
				continue;
			}
			mw = max(w - W, mw);
			mv = max(sum, mv);
			continue;
		}
		solve(W - b[i], f, sum + a[i]);
	}
}

int main() {
	int u = 0;
	while (scanf("%d", &w)) {
		if (w == 0) break;
		scanf("%d", &n);

		mv = 0;
		mw = w;
		u++;
		for (int i = 0; i < n; i++) {
			scanf("%d,%d", &a[i], &b[i]);
		}

		solve(w, -1, 0);
		printf("Case %d:\n%d\n%d\n", u, mv, mw);
	}
	return 0;
}

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