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 <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;
}