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 <stdio.h>
#include <cmath>
#include <algorithm>

using namespace std;


int main(){
	int W,N,count = 1,max_v,max_w;

	int** dp = new int*[1001];
	for(int i=0; i < 1001; i++){
		dp[i] = new int[1001];
	}

	while(true){
		scanf("%d",&W);
		if(W == 0)break;

		for(int i = 0; i < 1001; i++){
			for(int k=0; k < 1001; k++)dp[i][k] = 0;
		}

		max_v = max_w = -1;

		scanf("%d",&N);

		int v[N],w[N];

		for(int i=0; i < N; i++){
			scanf("%d,%d",&v[i],&w[i]);
		}

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

		for(int i = W; i >=0; i--){
			if(dp[0][i] >= max_v){
				max_v = dp[0][i];
				max_w = i;
			}
		}

		printf("Case %d:\n%d\n%d\n",count,max_v,max_w);
		count++;
	}


	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int w,n;
int kati[1111],omo[1111];
int memo[1111][1111];
int main(){
  int cas=0;
  int anskati=0;
  int ansomo;
  int w,n;
  int kati[1111],omo[1111];
  int memo[1111][1111];
  while(1){
    cas++;
    cin>>w;
    if(w==0) break;
    cin>>n;
    for(int i=0;i<=w;i++){
      memo[0][i]=0;
    }
    for(int i=0;i<n;i++){
      scanf("%d,%d",&kati[i],&omo[i]);
    }
    cout<<"Case "<<cas<<':'<<endl;
    
    for(int i=1;i<=n;i++){
      for(int j=1;j<=w;j++){
	if(j<omo[i-1]){
	  memo[i][j]=memo[i-1][j];
	}else{
	  memo[i][j]=max(memo[i-1][j],memo[i-1][j-omo[i-1]]+kati[i-1]);
	}
      }
    }
    for(int i=0;i<=w;i++){
      if(memo[n][i]>anskati){
	anskati=memo[n][i];
	ansomo=i;
      }
    }
    cout<<anskati<<endl;
    cout<<ansomo<<endl;
    
  }
}

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

struct pre{ int v, w; };

int main(){
	const int MAX_N = 1000, MAX_W = 1000;
	int N, W;
	pre dat[MAX_N];
	int dp[MAX_N][MAX_W + 1];
	int p;
	
	for(int n = 1;; n++){
		scanf("%d", &W);
		if(W == 0) break;
		scanf("%d", &N);
		for(int i = 0; i < N; i++) scanf("%d,%d", &dat[i].v, &dat[i].w);
		
		for(int i = 0; i < MAX_N; i++){
			for(int j = 0; j < MAX_W; j++){
				dp[i][j] = 0;
			}
		}
		
		for(int i = dat[0].w; i <= W; i++) dp[0][i] = dat[0].v;
		for(int i = 1; i < N; i++){
			for(int j = 0; j <= W; j++){
				dp[i][j] = (dat[i].w <= j)? dp[i - 1][j - dat[i].w] + dat[i].v : dp[i - 1][j];
			}
		}
		
		for(int i = 0; i <= W; i++){
			if(dp[N - 1][i] == dp[N - 1][W]){
				printf("Case %d:\n%d\n%d\n", n, dp[N - 1][W], i);
				break;
			}
		}
	}
	
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<utility>
#include<algorithm>

using namespace std;

int w,n;
vector< pair<int,int> > v;

int** GetArray(int w,int h){
	int** pt = new int*[w];
	for(int i=0;i<w;i++){
		pt[i] = new int[h];
		for(int j=0;j<h;j++){
			pt[i][j] = 0;
		}
	}
	return pt;
}

void DeleteArray(int** pt,int w){
	for(int i=0;i<w;i++){
		delete[] pt[i];
	}
	delete[] pt;
}

int main(){
	int setno = 1;
	int a,b;
	while(scanf("%d",&w) && w){
		scanf("%d",&n);
		printf("Case %d:\n",setno);
		v.clear();
		for(int i=0;i<n;i++){
			scanf("%d,%d",&a,&b);
			v.push_back(make_pair(a,b));
		}
		int** dp = GetArray(n,w + 1);
		for(int j=0;j<w;j++){
			//printf("j:%d\n",j);
			dp[0][j] = 0;
		}
		for(int i=1;i<n;i++){
			for(int j=0;j<w + 1;j++){
				//printf("%d:%d\n",i,j);
				if(j < v[i].second){
					dp[i][j] = dp[i-1][j];
				}else{
					dp[i][j] = max(dp[i-1][j] , dp[i-1][j-v[i].second] + v[i].first);
				}
			}
		}
		int ans = dp[n-1][w];
		int answ = -1;
		for(int i=0;i<w + 1;i++){
			if(dp[n-1][i] == ans){
				answ = i;
				break;
			}
		}
		printf("%d\n%d\n",ans,answ);
		DeleteArray(dp,n);
		setno++;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define rep(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define all(v) (v).begin(), (v).end()
#define MP make_pair

using namespace std;


int v[1010];
int w[1010];

int dp[1010][1010];

void solve(int n, int weight){
	for(int i = 1; i <= n; i++){
		for(int j = 0; j <= weight; j++){
			if(j < w[i]){
				dp[i][j] = dp[i-1][j];
			}else{
				dp[i][j] = max(dp[i-1][j - w[i]]+v[i], dp[i-1][j]);
			}
		}
	}
	int bestval = dp[n][weight];
	int bestwei = weight;
	for(int i = weight; i >= 0; i--){
		if(dp[n][i] == bestval){
			bestwei = i;
		}else{
			break;
		}
	}

	cout << bestval << endl;
	cout << bestwei << endl;
}

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


	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <utility>

#define ll (long long)
#define REP(i,n) for(int i = 0;i<(int)n;i++)
#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (a);i>(b);i--)

using namespace std;


int main(void){
    
    /*(x,y) = (??????,??????)*/
    int max_w;
    int number;
    int answer = 0;
    int casecounter = 1;
    while(cin >> max_w,max_w)
    {
        int dp[1002][1002] = {0};
        int w[1002] = {0},v[1002] = {0};
        cin >> number;
        for(int i  =1; i<= number;i++)
            scanf("%d%*c%d",&v[i],&w[i]);
        
        for(int j = 1; j<= number;j++)
            for(int k = 0;k<=max_w;k++)
            {
              if(j == 1 && k>=w[j])
                  dp[j][k] = v[j];
              else if(k >= w[j])
                  dp[j][k] = max(dp[j-1][k],v[j]+dp[j-1][k-w[j]]);
     
                
            }
        int max = 0;
        for(int i = 1;i<=max_w;i++)
        {
            if(max < dp[number][i])
            {
                answer = i;
                max = dp[number][i];
            }
            
        }
        printf("Case %d:\n",casecounter);
        printf("%d\n",dp[number][answer]);
        printf("%d\n",answer);
        casecounter++;
    }
    return 0;
    
}