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

struct treasure {
	int w;
	int v;
	void operator+=(treasure t){
		*this = *this + t;
	}
	treasure operator+(treasure t){
		treasure tt;
		tt.v = this->v + t.v;
		tt.w = this->w + t.w;
		return tt;
	}
};

struct treasure t[1000];
struct treasure dp[1001][1001];
int n;

treasure max(treasure a, treasure b){
	if(a.v == b.v){
		return a.w <= b.w ? a : b;
	}else{
		return a.v >= b.w ? a : b;
	}
}

treasure calc(int i, int w){
	if(dp[i][w].v >=0){ return dp[i][w]; }
	struct treasure res;
	if(i == n){
		res.v = 0;
		res.w = 0;
	}else if(w < t[i].w){
		res += calc(i+1, w);
	}else{
		res = max(calc(i+1, w), calc(i+1, w-t[i].w) + t[i]);
	}
	return dp[i][w] = res;
}

int main(void){
	int w, c=1;
	struct treasure res;
	while(true){
		scanf("%d", &w);
		if(w == 0){ break; }
		scanf("%d", &n);
		for(int i=0; i<n; ++i){
			scanf("%d,%d", &(t[i].v), &(t[i].w));
		}
		memset(dp, -1, sizeof(dp));

		res = calc(0, w);
		printf("Case %d:\n", c++);
		printf("%d\n", res.v);
		printf("%d\n", res.w);
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main()
{
	int sz,n;
	int w,v,dp[1001];
	for(int k=1;scanf("%d%d",&sz,&n),sz;k++){
		memset(dp,0,sizeof(int)*1001);

		for(int i=0;i<n;i++){
			scanf("%d,%d",&v,&w);
			for(int j=sz;j>=0;j--){
				if( (j==0||dp[j]>0) && j+w<=sz )
					dp[j+w]=max(dp[j+w],dp[j]+v);
			}
		}

		int vmax=0,wmin=sz;
		for(int i=0;i<=sz;i++)
			if(vmax<dp[i] || (vmax==dp[i] && wmin>i))
				vmax=dp[i],wmin=i;
		printf("Case %d:\n%d\n%d\n",k,vmax,wmin);
	}

	return 0;
}

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

int item_w[1001];
int item_p[1001];
int n;
int w;
int dp[1001];

pair<int,int> check(int i,int ww,int pp)
{
  pair<int,int> tmp;
  pair<int,int> tmp2;
  if(ww>w)
    return make_pair(0,0);
  if(i>n)
    return make_pair(ww,pp);
  if(dp[ww]>pp)
    return make_pair(ww,dp[ww]);
  dp[ww]=pp;
  tmp=check(i+1,ww,pp);
  tmp2=check(i+1,ww+item_w[i],pp+item_p[i]);
  if(tmp.first>tmp2.first ||(tmp.first==tmp2.first &&tmp.second<tmp2.second))
    {
      return tmp;
    }
  else
    {
      return tmp2;
    }
}

int main()
{
  int casenum = 1;
  for(;cin>>w,w;)
    {
      cin>>n;
      memset(dp,-1,sizeof(dp));
      for(int i=0;i<n;i++)
	{
	  char tmp;
	  cin>>item_p[i]>>tmp>>item_w[i];
	}
      int wa=0;
      int ans=0;
      pair<int,int> ans2=check(0,0,0);
      for(int i=0;i<=w;i++)
	{
	  //	  cout<<"test: i,dp[i]="<<i<<","<<dp[i]<<endl;
	  if(ans<dp[i])
	    {
	      ans=dp[i];
	      wa=i;
	    }
	}
      //      cout<<"Case "<<casenum++<<":"<<endl<<ans.second<<endl<<ans.first<<endl;
      cout<<"Case "<<casenum++<<":"<<endl<<ans<<endl<<wa<<endl;
    }
}

C++: #include<bits/stdc++.h>
#define MAX_N 1001
#define MAX_W 1001
using namespace std;


int dp[MAX_N][MAX_W];
int n,W;
int w[MAX_N];
int v[MAX_N];

void solve(){
  memset(dp,0,sizeof(dp));
  cout<<"Case 1:"<<endl;
  for(int i=0;i<n;i++){
    for(int j=0;j<=W;j++){
      if(j>=w[i]){
	dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
      }else{
	dp[i+1][j]=dp[i][j];
      }
    }
  }
  int tmp=0;
  int weight;
  for(int i=0;i<=W;i++){
    if(dp[n][i]>tmp){
      tmp=dp[n][i];
      weight=i;
    }
  }
cout << tmp << endl;
  cout << weight << endl;
}

int main(){
  while(cin>>W,W){
    cin>>n;
    for(int i=0;i<n;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    solve();
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int dp[1001][1001] = {0};
int main() {
	int W, N;
	int t = 1;
	while ( cin >> W, W ) {
		cin >> N;
		vector<int> v(N), w(N);
		for (int i = 0; i < N; ++i) {
			scanf("%d,%d", &v[i], &w[i]);
		}
		fill(&dp[0][0], &dp[0][0]+1001*1001, 0);
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j <= W; ++j) {
				if (w[i] + j <= W) {
					dp[i+1][w[i] + j] = max(dp[i+1][w[i] + j], dp[i][j] + v[i]);
				}
				dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
			}
		}
		int aw = 0, av = 0;
		for (int i = 0; i <= W; ++i) {
			if (dp[N][i] > av) {
				av = dp[N][i], aw = i;
			}
		}
		cout << "Case " << t << ":" << endl;
		cout << av << endl;
		cout << aw << endl;
		++t;
	}
}

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

using namespace std;

int main() {
    int count = 1;
    while (true) {
        int limit;
        cin >> limit;
        if (limit == 0)break;
        int n;
        cin >> n;

        vector<int> dp(limit + 1, -1145141919);
        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            char s[15];
            cin >> s;
            int v = stoi(strtok(s, ","));
            int w = stoi(strtok(NULL, ","));
            for (int j = limit; j >= 0; j--) {
                if (j + w > limit)continue;
                dp[j + w] = max(dp[j + w], dp[j] + v);
            }
        }

        int maxvalue = 0;
        int minweight = 0;
        for (int i = 0; i <= limit; i++) {
            if (maxvalue < dp[i]) {
                maxvalue = dp[i];
                minweight = i;
            }
        }

        cout << "Case " << count << ":" << endl;
        cout << maxvalue << endl;
        cout << minweight << endl;
        count++;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int dp[1001][1001] = {{0}};
int main(){
  int w,n;
  char ch;
  int P[1001]={0},W[1001]={0},t = 1;
  while(cin >> w,w ){
    cin >> n;
    for(int i = 0 ; i < n ; i++ ){
      cin >> P[i] >> ch >> W[i];
    }
    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][j],dp[i][j-W[i]] + P[i]);
	}else dp[i+1][j] = dp[i][j];
      }
    }
    cout << "Case " << t << ":" << endl;
    for(int i = w ; i >= 0 ; i--){
      if(dp[n][i] != dp[n][w]){
	cout << dp[n][w] << endl << i + 1 << endl;
	break;
      }
    }
    t++;
  }
  return 0;
}

C++: #include<iostream>
#include<stdio.h>
using namespace std;
int dp[1001][1001];
int main(){
  int W,N,v[1000],w[1000];
  int n=1;
  int lastj;
  char jyama;
  while(cin>>W&&W){
    cin>>N;
    for(int i=0;i<N;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    for(int i=0;i<N-1;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]);
	  if(dp[i+1][j]!=dp[i][j]) lastj=j;
	}
      }
    }
    cout<<"Cace "<<n<<":"<<endl;
    cout<<dp[N-1][W]<<endl;
    cout<<lastj<<endl;
  }
}