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>
using namespace std;
main(){
  int a,b,c,N,K[1010],P[1010],i,m,j,W,dp[1010],h=1,y,g;
  char d;
  while(1){
    dp[3000];
    a=0;
    b=0;
    cin >> W;
    for(i=0;i<=W;i++){
      dp[i]=0;
    }
    if(W==0){
      break;
    }
    cin >> N;
    for(i=0;i<N;i++){
      cin >> P[i] >> d >> K[i];
      c=K[i];
      dp[K[i]]=P[i];
      if(a<K[i]){
	a=K[i];
      }
      for(j=a;j>=1;j--){
	if(j!=c){
	  if(dp[j]!=0){
	    if(dp[c+j]!=0){
	      if(dp[c+j]<dp[c]+dp[j]){
		dp[c+j]=dp[c]+dp[j];
	      }
	      else{
		continue;
	      }
	    }
	    if(dp[c+j]==0){
	      dp[c+j]=dp[c]+dp[j];
	    }
	  }
	}
      }
    }
    for(i=1;i<=W;i++){
      if(dp[i]>b){
	y=i;
	b=dp[i];
      }
      if(dp[i]==b && y>i){
	y=i;
      }
    }
    cout << "Case:" << " " << h << endl;
    cout << b << endl << y << endl;
    h++;
  }
}

C++: #include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const int MAX_V=105;
int INF = 1000000000;
  
int main(){
  int n=1;
  int W,N,i,j;
  while(cin >> W ,W){
    cin >> N;
    int dp[N+1][W+1];
    rep(i,N+1){
      rep(j,W+1){
        dp[i][j]=0;
      }
    }

    rep(i,N){
      int v,w;
      char c;
      cin >> v >> c >> w;
      rep(j,W){
        dp[i+1][j]=max(dp[i][j],dp[i+1][j]);
        if(j+w<W+1){
        dp[i+1][j+w]=max(dp[i][j]+v,dp[i+1][j+w]);
        }
      }
    }
    int value1=0;
    int weight=0;
/*    rep(j,W+1){
      if(dp[N][j]>value1){
        value1=dp[N][j];        
      }
}*/
    rep(j,W+1){
      if(dp[N][j]==dp[N][W]){
        weight=j;
        break;
      }
    }
    
    cout << "Case" << ' ' << n++ << ":" << endl;
    cout << dp[N][W]<< endl;
    cout << weight << endl;  
    
  }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int,int> P;
typedef pair<P,int> status;

int main() {
	int W,N,cnt = 1;
	while(true){
		cin >> W;
		if(W==0) return 0;
		cin >> N;
		int val[1001],weight[1001];
		memset(val,0,sizeof(val));
		memset(weight,0,sizeof(weight));
		for(int i = 0;i<N;i++){
			cin >> val[i];
			cin.get();
			cin >> weight[i];
		}
		int dp[1003];
		memset(dp,0,sizeof(dp));
		for(int i = 0;i<N;i++){
			for(int j = W;j >= weight[i];j--){
				if(j-weight[i]<0)continue;
				dp[j] = max(dp[j],dp[j-weight[i]]+val[i]);
			}
		}
		int mini;
		for(int i = W-1;dp[i] == dp[W-1];i--) mini = i;
		cout << "Case " << cnt << ":" << endl;
		cout << dp[W-1] << endl;
		cout << mini << endl;
		cnt++;
	}
	return 0;
}

C++: #include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
using namespace std;
int inf = 1000000000;
// dp[i][j] = i ???????????§??§?????? j ??§?????????
// dp[i][j] = dp[i][j-?] + v[i];

int dp[1001][1001];

int main(){

  int W, N, l;
  l = 1;
  while(cin >> W){
    if( W == 0 ) break;
    cin >> N;
    vector<int> v(N);
    vector<int> w(N);
    for(int i=0; i<N; i++) scanf("%d,%d", &v[i], &w[i]);

    for(int i=0; i<1001; i++) for(int j=0; j<1001; j++) dp[i][j] = 0;

    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+1][j], dp[i][ j - w[i] ] + v[i]);
        dp[i+1][j] = max(dp[i][j], dp[i+1][j]);
      }
    }

    int ansi = 0;
    int ans = 0;
    for(int i=0; i<=W; i++){
      if( ans < dp[N][i] ){
        ans = dp[N][i];
        ansi = i;
      }
    }

    cout << "Case " << l << ":" << endl;
    cout << ansi << endl;
    cout << ans << endl;

    l++;
  }




  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<string.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define max_n 1000
#define max_w 10000
using namespace std;

int N,W;
int dp[max_n][max_w];
int dw[max_n][max_w];
int w[max_n],v[max_n];

int rec(int i,int j){
	int res;
	if(dp[i][j]!=-1)return dp[i][j];
	if(i==N){
		res=0;
		dw[i][j]=0;
	}else if(j<w[i]){
		res=rec(i+1,j);
		dw[i][j]=dw[i+1][j];
	}else{
		int r1=rec(i+1,j);
		int r2=rec(i+1,j-w[i])+v[i];
		if(r1>r2){
			res=r1;
			dw[i][j]=dw[i+1][j];
		}else if(r2>r1){
			res=r2;
			dw[i][j]=dw[i+1][j-w[i]]+w[i];
		}else{
			if(dw[i+1][j]>(dw[i+1][j-w[i]]+w[i])){
				res=r2;
				dw[i][j]=dw[i+1][j-w[i]]+w[i];
			}else{
				res=r1;
				dw[i][j]=dw[i+1][j];
			}
		}
	}
	return dp[i][j]=res;
}

int main(){
	int p=1;
	while(cin>>W,W){
		cin>>N;
		rep(i,N){
			scanf("%d,%d",&v[i],&w[i]);
		}
		memset(dp,-1,sizeof(dp));
		memset(dw,-1,sizeof(dw));
		cout<<"Case "<<p<<":"<<endl;
		cout<<rec(0,W)<<endl;
		cout<<dw[0][W]<<endl;
		p++;
		/*cout<<"value="<<rec(0,W)<<" cost="<<dw[0][W]<<endl;//????????§??????????????????
		cout<<"cost="<<dw[0][W]<<endl;*/
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX_N = 1000, MAX_W = 1000;

int main(){
	int dp[MAX_N+1][MAX_W+1], wdp[MAX_N+1][MAX_W+1];
	int W,N,v[MAX_N],w[MAX_N],C = 1;
	while(cin >> W,W){
		memset(dp,0,sizeof(dp));
		memset(v,0,sizeof(v));
		memset(w,0,sizeof(w));
		std::cin >> N;
		for(int i=0;i<N;i++){
			int a,b;
			scanf("%d,%d",&a,&b);
			v[i] = a, w[i] = b;
		}
		for(int i=1;i<=N;i++){
			for(int j=1;j<=W;j++){
				if(w[i] > j)//ツ選ツづ猟づ按つ「ツづつォ
					dp[i][j] = dp[i-1][j];
				else{//ツ選ツづ猟づ按つ「ツづつォ
					dp[i][j] = max(dp[i-1][j],dp[i-1][j - w[i]] + v[i]);
				}
			}
		}
		int wmv;
		for(int i=0;i<=W;i++){
			if(dp[N][i] == dp[N][W]){wmv = i;break;}
		}
		cout << "Case " << C++ << ":" << endl << dp[N][W] << endl << wmv << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stdio.h>

using namespace std;

int MAX = 1000*1000;

void solve(int n, int W, vector<int> v, vector<int> w) {
	int **dp = new int*[n];
	for (int i=0; i<n; i++) {
		dp[i] = new int[MAX+1];
		for (int j=0; j<=MAX+1; j++)
			dp[i][j] = 0;
	}

	int ans = 0;

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

	int val = 0, weight = 0;
	for (int j=0; j<=W; j++) {
		if (val < dp[n-1][j]) {
			val = dp[n-1][j];
			weight = j;
		}
	}

	cout << val << endl << weight << endl;

	for (int i=0; i<n; i++)
		delete [] dp[i];
	delete [] dp;
}

int main() {
	

	int n, W;
	int ver = 1;

	cin >> W;
	while (W!=0) {
		cin >> n;

		vector<int> v(n), w(n);
		for (int i=0; i<n; i++)
			scanf("%d,%d", &v[i], &w[i]);

		cout << "Case " << ver << ":" << endl;
		solve(n, W, v, w);

		cin >> W;
		ver++;
	}
}