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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;

int co = 1;
char k;
int W, N, v[10000], w[10000];

int dp[1010][10000];//i???????????§???????????§?????????j??\?????¨?????????????????????????????§???

int main(){
	while (cin >> W&&W){
		cin >> N;
		FOR(i, 1, N + 1)cin >> v[i] >> k >> w[i];
		FOR(i, 1, N + 1){
			REP(j, W){
				if (j >= w[i]){
					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 max = -1, ans = -1;
		REP(i, W){
			if (max < dp[N][i]){
				max = dp[N][i];
				ans = i;
			}
		}
		printf("Case %d:\n", co++);
		cout << max << endl;
		cout << ans << endl;
		REP(i, N + 1)REP(j, W)dp[i][j] = 0;
	}
}

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

int main()
{
	int N,W,T=0;
	while(cin>>W, W)
	{
		cin >> N;
		int dp[1024]={0};

		int v,w; char c;
		for(int i=0; i<N; i++)
		{
			cin>>v>>c>>w;
			for(int j=W-w; j>=0; j--)
			{
				dp[j+w] = max(dp[j+w], dp[j]+v);
			}
		}

		int mi=W;
		for(int i=W; i>=0; i--)
		{
			if(dp[i]!=dp[mi])
			{
				cout << "Case " << ++T << ":\n";
				cout << dp[mi] << "\n" << mi << "\n";
				break;
			}
			mi=i;
		}
	}
}

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

int W,n,v[1001],w[1001];
int dp[1001];

int main(){
   int a = 0;
   while(cin >> W,W){
      cin >> n;
      for(int i=0;i<n;i++) scanf("%d,%d",&v[i],&w[i]);
      
      memset(dp,-1,sizeof(dp)); dp[0] = 0;
      for(int i=0;i<=W;i++){
         if(~dp[i]){
            for(int j=0;j<n;j++){
               if(i + w[j] <= W) dp[i + w[j]] = max(dp[i],dp[i] + v[j]);
            }
         }
      }
      
      cout << "Case " << ++a << ":" << endl;
      int maxi = 0,maxp;
      for(int i=0;i<=W;i++){
         if(maxi < dp[i]){
            maxi = dp[i]; maxp = i;
         }
      }
      cout << maxi << endl << maxp << endl;
   }
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <utility>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int W,N;
const int MAX_N = 1001;
const int MAX_W = 1001;
int v[MAX_N];
int w[MAX_N];
vector<pii> t;
int dp[MAX_N][MAX_W];

void solve() {
	memset(dp, 0, sizeof(dp));
	repa(i,1,N) {
		repa(j,0,W) {
			if(j + w[i] < W) {
				dp[i][j + w[i]] = dp[i-1][j] + v[i];
			}
		}
	}

	int maxval = -1;
	int weight = 0;
	repa(i,0,W) {
		if(maxval < dp[N][i]) {
			maxval = dp[N][i];
			weight = i;
		}
	}
	cout <<	maxval << endl;
	cout << weight << endl;
}

int main() {
	int c = 0;
	while(cin>>W, W) {
		cin >> N;
		rep(i,N) {
			scanf("%d,%d", &v[i], &w[i]);
		}

		printf("Case %d:\n", ++c);
		solve();
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
#include <bitset>
#define INF	100000000
#define pb	push_back
#define fi first
#define sec second
#define SS stringstream
using namespace std;
typedef pair<int, int> P;
typedef long long int	ll;

int w, n;
int t = 0;
int dp[1001][1001];
vector<P> vec;

int rec(int i, int j){
	if(dp[i][j] >= 0) return dp[i][j];
	int res;
	if(i == n) res = 0;
	else if(j < vec[i].sec) res = rec(i+1, j);
	else res = max(rec(i+1, j), rec(i+1, j-vec[i].sec)+vec[i].fi);
	return dp[i][j] = res;
}

int main(){
	while(scanf("%d", &w)){
		t++;
		if(w == 0) break;
		memset(dp, -1, sizeof(dp));
		scanf("%d", &n);
		for(int i = 0; i < n; i++){
			int temp1, temp2;
			char trash;
			scanf("%d%c%d", &temp1, &trash, &temp2);
			vec.pb(P(temp1, temp2));
		}
		printf("Case %d:\n", t);
		printf("%d\n", rec(0, w));
		for(int i = 1; i <= w; i++){
			if(rec(0, w-i) != rec(0,w)){
				printf("%d\n", w-i+1);
				break;
			}
		}
		vec.clear();
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

int dp[1001][1001];

int main(void) {
  int W, n;
  int num = 1;
  while (cin >> W, W) {
    cin >> n;
    memset(dp, 0, sizeof(dp));
    
    vector<int> v(n), w(n);
    char c;
    for (int i = 0; i < n; i++) {
      cin >> v[i] >> c >> w[i];
    }

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

    printf("Case %d:\n", num++);
    cout << dp[n][W] << endl;
    cout << lower_bound(dp[n], dp[n] + W, dp[n][W]) - dp[n] << endl;
  }

  return 0;
}

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[100][10000];
  int dp1[100][10000];
  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;
  }
}