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 <utility>

using namespace std;

pair<int, int> napsacDP(int capacity, int n,
    const vector<pair<int, int>>& contents) {
  vector<vector<int>> dp(n+1, vector<int>(capacity+1));
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= capacity; ++j) {
      dp[i][j] = dp[i-1][j];
      if (j >= contents[i-1].second) {
        dp[i][j] = max(dp[i][j],
            dp[i-1][j-contents[i-1].second]
                + contents[i-1].first);
      }
    }
  }
  int maxval = 0;
  int maxweight = 0;
  for (int i = 0; i <= capacity; ++i) {
    if (maxval < dp[n][i]) {
      maxval = dp[n][i];
      maxweight = i;
    }
  }
  return make_pair(maxval, maxweight);
}

int main() {
  int count = 1;
  while(1) {
    int w,n;
    cin >> w;
    if (w == 0) break;
    cin >> n;
    vector<pair<int, int>> contents;
    for (int i = 0; i < n; ++i) {
      int value, weight;
      scanf("%d,%d", &value, &weight);
      contents.emplace_back(value, weight);
    }
    auto result = napsacDP(w, n, contents);
    cout << "Case " << count << ":" << endl;
    cout << result.first << endl;
    cout << result.second << endl;
    ++count;
  }
  return 0;
}

C++: #include <stdio.h>

int dp[1024][10010];

int main(void)
{
	int i, j;
	int n;
	int wei;
	int w[1024];
	int k[1024];
	int b;
	int max;
	int mw;
	
	b = 0;
	while (scanf("%d", &wei), wei != 0){
		scanf("%d", &n);
		for (i = 0; i <= n; i++){
			if (i < n){
				scanf("%d,%d", &k[i], &w[i]);
			}
			for (j = 0; j < 10000; j++){
				dp[i][j] = 0;
			}
		}
		for (i = 0; i < n; i++){
			for (j = 0; j <= wei; j++){
				if (dp[i + 1][j + w[i]] < dp[i][j] + k[i]){
					dp[i + 1][j + w[i]] = dp[i][j] + k[i];
				}
				if (dp[i + 1][j] < dp[i][j]){
					dp[i + 1][j] = dp[i][j];
				}
			}
		}
		max = 0;
		for (i = 0; i < wei; i++){
			if (max < dp[n][i]){
				max = dp[n][i];
				mw = i;
			}
		}
		b++;
		printf("Case %d:\n", b);
		printf("%d\n%d\n", max, mw);
	}
	return (0);
}

C++: #include<iostream>
using namespace std;
int W,N,ansap,answp,a,b,c=0,nw;
int dp[1001][1001];
int ap[1001];
int wp[1001];
char mj [5];


int main(){
  char ch;
  while(true){
    c++;
    cin>>W;
    if(W==0)break;
    cin>>N;
    for(int i=1;i<=N;i++){
      cin>>ap[i];
      cin >> ch;
      cin>>wp[i];
    }
    
    for(int i=0;i<1001;i++){
      dp[0][i]=0;
      dp[i][0]=0;
    }
    
    for(int i=1;i<=N;i++){
      for(int j=1;j<=W;j++){
	nw=j-wp[i];
	if(nw>=0){
	  if(dp[i-1][j]<dp[i-1][nw]+ap[i]){
	    dp[i][j]=dp[i-1][nw]+ap[i];
	  }else{
	    dp[i][j]=dp[i-1][j];
	  }
	}else{
	  dp[i][j]=dp[i-1][j];
	}
      }
    }
    ansap=0;
    for(int i=1;i<=W;i++){
      if(ansap<dp[N][i]){
	ansap=dp[N][i];
	answp=i;
      }
    }
    


    cout<<"Case "<<c<<":"<<endl;
    cout<<ansap<<endl;
    cout<<answp<<endl;
  }
  return 0;
}

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

#define READ_BY_TEXT false

vector<string> splitByDelimiter(string target, string delim) {
    std::string s = target;
    std::string delimiter = delim;
    size_t pos = 0;
    std::string token;
    vector<string> arr;
    while ((pos = s.find(delimiter)) != std::string::npos) {
        token = s.substr(0, pos);
        arr.push_back(token);
        s.erase(0, pos + delimiter.length());
    }
    arr.push_back(s);
    return arr;
}

int W, N;
int w[1000], v[1000];
int dp[1000][1000];
//vector<int> lines;
//int max_v, min_w;

int recursive(int i, int j) {
    
    if (dp[i][j] != -1) return dp[i][j];
    
    int ret;
    if (i >= N) {
        ret = 0;
    }
    else if (j < w[i]) {
        ret = recursive(i+1, j);
    }
    else {
        ret = max(recursive(i+1, j), recursive(i+1, j-w[i])+v[i]);
    }
    dp[i][j] = ret;
    
    return ret;
}

int calcMinWeight(int i, int j) {
    
    if (i >= N) {
        return 0;
    }
    
    int m_w = W;
    if (dp[i][j] == -1) {
        m_w = min(m_w, calcMinWeight(i+1, j));
    }
    else {
        m_w = min(m_w, calcMinWeight(i+1, j-w[i]) + w[i]);
    }
    
    return m_w;
}

int main() {
    
#if READ_BY_TEXT
    // Get values from test.txt
    std::ifstream ifs("test.txt");
    std::string str;
    if (ifs.fail())
    {
        std::cerr << "??±???" << std::endl;
        return -1;
    }
    getline(ifs, str);
    vector<string> words = splitByDelimiter(str, " ");
    num_d = stoi(words[0]);
    num_n = stoi(words[1]);
    for (int i=0; i<num_n; i++) {
        getline(ifs, str);
        lines.push_back(stoi(str));
    }
#else
    
    int case_v = 1;
    while (true) {
        cin >> W;
        if (!W) break;
        cin >> N;
        string input;
        for (int i = 0; i < N; i++) {
//            getline(cin, input);
            cin >> input;
            vector<string> words = splitByDelimiter(input, ",");
            v[i] = stoi(words[0]);
            w[i] = stoi(words[1]);
        }
        memset(dp, -1, sizeof(dp));
        
        // Solve
        int min_w = W;
        int max_v = recursive(0, W);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j <= W; j++) {
//                cout << "dp[" << i << "][" << j << "] = " << dp[i][j] << endl;
                if (dp[i][j] == max_v) {
//                    cout << "i " << i << " j " << j << endl;
                    min_w = min(min_w, calcMinWeight(i, j));
//                    min_w = min(j, min_w);
                }
            }
        }
        
        cout << "Case " << case_v << ":" << endl;
        cout << max_v << endl;
        cout << min_w << endl;
        case_v++;
    }
    
//    string input;
//    getline(cin, input);
//    vector<string> words = splitByDelimiter(input, " ");
//    num_d = stoi(words[0]);
//    num_n = stoi(words[1]);
//    string input1;
//    for (int i=0; i<num_n; i++) {
//        getline(cin, input1);
//        lines.push_back(stoi(input1));
//    }
#endif
    
//    recursive(0, 0, "");
//    cout << result << endl;
    
    //    cout << "num_d = " << num_d << " num_n = " << num_n << "\n";
    //
    //    // Check parsed contents
    //    for (int i = 0; i < lines.size(); i++) {
    //        cout << "line[" << i << "] " << to_string(lines[i]) << "\n";
    //    }
    
    return 0;
}