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 <cstdio>
#include <vector>
using namespace std;


struct treasure{
  int v;
  int w;
};

void thief(int W, int N, vector<treasure>* v, int* retV, int* retW);

int main(void){
  int c=1;
  int W;
  int N;
  while(true){
    cin>>W;
    if(W==0)break;
    cin>>N;
    vector<treasure> v;
    for(int i=0;i<N;i++){
      int _v,w;
      scanf("%d,%d",&_v,&w);
      treasure tmp;
      tmp.v=_v;
      tmp.w=w;
      v.push_back(tmp);
    }

    int retV;
    int retW;
    thief(W,N,&v,&retV,&retW);
    cout<<"Case "<<c<<":"<<endl;
    cout<<retV<<endl;
    cout<<retW<<endl;
    c++;
  }
  return 0;
}



void thief(int W, int N, vector<treasure>* _v, int* retV, int* retW){
  auto v=*_v;
  int table[N][W+1];
  for(int i=0;i<N;i++){
    for(int j=0;j<=W;j++){
      table[i][j]=0;
    }
  }
  for(int j=0;j<=W;j++){
    if(v[0].w>j)continue;
    else{
      table[0][j]=v[0].v;
    }
  }

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

  *retV=table[N-1][W];
  for(int j=W;j>=0;j--){
    if(table[N-1][j]==*retV){
      *retW=j;
    }
  }
}

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

int a, b, n, w;
std::string s;
int c = 0;

int main() {
    while (true) {
        std::cin >> w;
        if (w == 0) {
            return 0;
        }
        std::cin >> n;
        std::vector<std::pair<int, int>> things;
        for (int i = 0; i < n; i++) {
            std::cin >> s;
            std::sscanf(s.c_str(), "%d,%d", &a, &b);
            things.push_back({a, b});
        }
        std::vector<std::vector<int>> dp(n+1, std::vector<int>(w+1, 0));
        for (int j = 0; j < n; j++) {
            for (int p = 1; p <= w; p++) {
                if (things[j].second <= p) {
                   dp[j+1][p] =  std::max(dp[j][p], dp[j][p-things[j].second] + things[j].first);
                }
                else {
                    dp[j+1][p] = dp[j][p];
                }
            }
        }
        int weight = 0;
        int thief_value = 0;
        for (int i = 0; i <= w; i++) {
            if (dp[n][i] > thief_value) {
                weight = i;
                thief_value = dp[n][i];
            }
        }
        c++;
        std::cout << "Case "<< c <<":" << std::endl << dp[n][w] << std::endl << weight << std::endl;
    }
}

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

int main(){
	int dataset = 1, w;
	while (cin >> w, w){
		int dp[1001], n, treas_val[1000], treas_w[1000], ans_val = 0, ans_w;
		for (int i = 0; i <= w; i++){
			dp[i] = -1;
		}
		cin >> n;
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &treas_val[i], &treas_w[i]);
		}
		dp[0] = 0;
		for (int i = 0; i < n; i++){
			for (int j = w; j >= 0; j--){
				if (dp[j] == -1) continue;
				if (j + treas_w[i] <= w && dp[j + treas_w[i]] < dp[j] + treas_val[i])
					dp[j + treas_w[i]] = dp[j] + treas_val[i];
			}
		}
		for (int i = 0; i <= w; i++){
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		printf("Case %d:\n%d\n%d\n", dataset, ans_val, ans_w);
		dataset++;
	}
	return 0;
}

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

int dp[1001][1001];

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;
      }
    }
    cout <<"Case "<< count <<":"<<endl;
    
    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] = ne[i-1] + dp[i-1][j];
	}
      }
    }

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