Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
	"strings"
)

const MaxW = 1000
const MaxN = 1000

type Tuple struct {
	Value  int
	Weight int
}

func nextLine(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func solve(w int, n int, vws []Tuple) Tuple {
	var dp [MaxN + 1][MaxW + 1]int

	for i := 0; i < n; i++ {
		for j:= w; j > 0; j-- {
			if j < vws[i].Weight {
				dp[i + 1][j] = dp[i][j]
			} else {
				dp[i + 1][j] = int(math.Max(float64(dp[i][j]), float64(dp[i][j - vws[i].Weight] + vws[i].Value)))
			}
		}
	}

	maxValue := dp[n][w]
	minWeight := w

	for j := w - 1; j > 0; j-- {
		if maxValue == dp[n][j] {
			minWeight = j
		} else {
			break
		}
	}

	return Tuple{Value: maxValue, Weight: minWeight}
}

func main() {
	var sc = bufio.NewScanner(os.Stdin)
	index := 1
	for {
		var w, n int
		w, _ = strconv.Atoi(nextLine(sc))
		if w == 0 {
			return
		}
		n, _ = strconv.Atoi(nextLine(sc))

		vws := make([]Tuple, n, n)
		for i := 0; i < n; i++ {
			array := strings.Split(nextLine(sc), ",")
			v, _ := strconv.Atoi(array[0])
			w, _ := strconv.Atoi(array[1])
			vws[i] = Tuple{Value: v, Weight: w}
		}

		t := solve(w, n, vws)
		fmt.Printf("Case %d:\n", index)
		fmt.Println(t.Value)
		fmt.Println(t.Weight)
		index++
	}
}

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

int dp[1002][1002];

int main() {
  vector <int> cost,ne;
  int w;
  int n;
  int count = 0;
  while(1) {
    count++;
    cin >> w;
    if(w == 0) break;
    cin >> n;

    string str,a;
    cost.resize(n);
    ne.resize(n);
    for(int i=0;i<n;i++) {
      cin >> str;

      int flg = 0,b,c;
      a.clear();
      cost[i] = ne[i] = 0;
      for(int j=0;j<str.size();j++) {
	if(str[j] == ',') {
	  c= 1;
	  for(int k=j-1;k>=0;k--) {
	    ne[i] += (str[k]-'0')*c;
	    c *= 10;
	  }
	  b = j;
	  continue;
	}
	a += str[i];
      }
      c = 1;
      for(int k=str.size()-1;k>b;k--) {
	cost[i] += (str[k]-'0')*c;
	c *= 10;
      }
    }
    
    for(int i=0;i<=n;i++)
      for(int j=0;j<=w;j++)
	dp[i][j] = -1;
   
    for(int i=0;i<=n;i++) dp[i][0] = 0;

    for(int i=1;i<=n;i++) {
      for(int j=0;j<=w;j++) {
	if(dp[i-1][j] != -1 && cost[i-1]+j <= w) { 
	  dp[i][cost[i-1]+j] =max(ne[i-1] + dp[i-1][j],dp[i][j+cost[i-1]]);
	}
	dp[i][j] =max(dp[i][j], dp[i-1][j]);
      }
    }
    
    /*    for(int i=0;i<=n;i++){
      for(int j=0;j<=w;j++)
	cout << dp[i][j] <<" ";
	  cout << endl;
	  } */

    
    int   ans = -1,ans2;
    for(int i=0;i<=w;i++)
      if(ans < dp[n][i] )
	ans = dp[n][i],ans2 = i;
    
    cout <<"Case "<< count <<":"<<endl;
    cout << ans <<endl;
    cout << ans2 <<endl;
    
    
  }
  return 0;
}

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

int dp[1001][1001];
int W, N;
int w[1000];
int v[1000];
int solve(){
    for(int i=0;i<N;i++){
        for(int j=0;j<=W;j++){
            if(j < w[i]) dp[i+1][j] = dp[i][j];
            else dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]]+v[i]);
        }
    }
    return dp[N][W];
}
int main(){
    char comma;
    int count=0;
    while(cin >> W && W){
        count++;
        cin >> N;
        for(int i=0;i<N;i++){
            cin >> v[i] >> comma >> w[i];
        }
        for(int j=0;j<W;j++){
            dp[0][j] = 0;
        }
        cout << "Case " << count << ':' << endl;
        cout << solve() << endl;
        for(int j=W-1;j>=0;j--){
            if(dp[N][j] < dp[N][W]){
                cout << j+1 << endl;
                break;
            }
        }
    }
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
	int ww,n,c=1,p;
	while(cin>>ww,ww){
		cin>>n;
		int w[n+1],v[n+1],dp[n+1][ww+1];
		r(i,0,n+1)r(j,0,ww+1)dp[i][j]=0;
		r(i,1,n+1)scanf("%d,%d",&v[i],&w[i]);
		r(i,1,n+1){int p=1;
			r(j,1,ww+1){
				if(j-w[i]>0){dp[i][j]=max(dp[i-1][j-w[i]]+v[i],max(dp[i-1][j],dp[i][j-1]));p=0;}
				else {dp[i][j]=max(dp[i-1][j],dp[i][j-1]);}
			}
		}
		r(i,0,ww+1)if(dp[n][i]<dp[n][ww])p=i;
		printf("Case %d:\n%d\n%d\n",c++,dp[n][ww],p);
	}
}

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

int dp[1000][1000];
int W;
int n;
int v[1000], w[1000];
int now = 1;

int main()
{
	while( 1 ){
		scanf( "%d", &W );
		if( !W ) break;
		scanf( "%d", &n );
		for( int i = 0; i < n; i++ ){
			scanf( "%d,%d",  &v[i], &w[i] );
		}
		
		memset( dp, 0, sizeof( dp ) );
		
		for( int i = 0; i < n; i++ ){
		 	for( int j = 0; j <= W; j++ ){
				if( j < w[i] ){
				 	dp[i+1][j] = dp[i][j];
				}
				else{
				 	dp[i+1][j] = std::max( dp[i][j], dp[i][j-w[i]] + v[i] );
				}
			}
		}
		
		int res = W;
		for( int i = W; i >= 0; i-- ){
			if( dp[n][i] == dp[n][W] ) res = std::min( res, i );
			else break;
		}
		
		printf( "Case %d:\n", now );
		printf( "%d\n", dp[n][W] );
		printf( "%d\n", res );
		
		now++;	
	}
	

 	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-1][j], dp[i-1][j-weight[i-1]] + value[i-1]);
                else
                    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<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

int main(){
    int W;
    int cnt = 0;
    while(cin >> W,W){
        int n;
        cin >> n;

        int v[1005], w[1005];
        rep(i,n){
            char g;
            cin >> v[i] >> g >> w[i];
        }

        int dp[2005] = {0};
        rep(i,W + 1) dp[i] = -1;
        dp[0] = 0;

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

        int ans_v = 0, ans_w;
        rep(i,W + 1){
            if(ans_v < dp[i]){
                ans_v = dp[i];
                ans_w = i;
            }
        }

        cout << "Case " << cnt << ":" << endl;
        cout << ans_v << endl;
        cout << ans_w << endl;
        cnt++;
    }
}