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<string>
#include<vector>

using namespace std;

struct Treasure
{

  long value;
  long weight;
  bool flag;
};

bool flag;
long limit_weight;
long treasure_number;
Treasure result_treasure;

int AThief(vector<Treasure> &treasure,long &total_value,long &total_weight)
{

  if(limit_weight < total_weight){
    return 0;
  }
  else
    {

      for(int i = 0; i < treasure_number; i++)
        {

          if(treasure[i].flag)
            {

              total_value += treasure[i].value;
              total_weight += treasure[i].weight;
              treasure[i].flag = false;

              if(AThief(treasure,total_value,total_weight) == 0)
                {

                  total_value -= treasure[i].value;
                  total_weight -= treasure[i].weight;

                  if(result_treasure.value == total_value && result_treasure.weight > total_weight) result_treasure.weight = total_weight;
                  else if(result_treasure.value < total_value)
                    {
                      result_treasure.value = total_value;
                      result_treasure.weight = total_weight;
                    }
                  flag = false;
                }
              else
                {
                  total_value -= treasure[i].value;
                  total_weight -= treasure[i].weight;
                }

              treasure[i].flag = true;

            }

        }

    }

  //全部足しても耐えられる重さを越えなかった時
  if(flag)
    {
      result_treasure.value = total_value;
      result_treasure.weight = total_weight;
      flag = false;
    }


  return 1;

}


int main()
{

  int dummy;
  Treasure temp;
  int case_number = 0;
  long total_value = 0;
  long total_weight = 0;
  vector<Treasure> treasure;

  scanf("%d",&limit_weight);

  while(limit_weight!=0)
    {

      scanf("%d",&treasure_number);

      treasure.clear();

      for(int i = 0; i < treasure_number; i++)
        {

          scanf("%d,%d",&temp.value,&temp.weight);
          temp.flag = true;
          treasure.push_back(temp);

        }

      result_treasure.value = -1;
      result_treasure.weight = 2147483647;
      flag = true;
      dummy = AThief(treasure,total_value,total_weight);

      case_number++;

      cout << "Case " << case_number << ":" << endl;
      cout << result_treasure.value << endl;
      cout << result_treasure.weight << endl;

      cin >> limit_weight;

    }

}

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

using namespace std;

#define price first
#define weight second

int main(){
	int W;
	int cnt = 1;
	while (cin >> W, W) {
		int N; cin >> N;
		vector<pair<int, int> > tres(N);
		vector<int> dp(W+1);
		for (int i = 0; i < N; i++) { scanf("%d,%d", &tres[i].price, &tres[i].weight);}
		for (int i = 0; i < W+1; i++) { dp[i] = -1; }
		dp[0] = 0;

		for (int i = 0; i < N; i++) {
			for (int j = W; j >= 0; j--) {
				if (dp[j] == -1) { continue; }
				if (j+tres[i].weight <= W) {
					if (dp[j+tres[i].weight] < dp[j]+tres[i].price) {
						dp[j+tres[i].weight] = dp[j]+tres[i].price;
					}
				}
			}
		}
		int PRICE = 0;
		int WEIGHT = 0;
		for (int i = W; i >= 0; i--) {
			if (PRICE < dp[i]) {
				PRICE = max(PRICE, dp[i]);
				WEIGHT = i;
			}
		}

		cout << "Case " << cnt << ":" << endl;
		cout << PRICE << endl;
		cout << WEIGHT-1 << endl;
		cnt++;
	}

	return 0;
}

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

int dp[1001][1001];

int main(void)
{
	int w;
	int n;
	int cnt;
	int max;
	
	cnt = 1;
	while (1){
		int value[1000];
		int weight[1000];
		
		scanf("%d", &w);
		
		if (w == 0){
			break;
		}
		
		scanf("%d", &n);
		
		memset(dp, -1, sizeof(dp));
		
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &value[i], &weight[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 && j + weight[i - 1] <= w){
					dp[i][j + weight[i - 1]] = dp[i - 1][j] + value[i - 1];
				}
				if (dp[i - 1][j] != -1){
					dp[i][j] = dp[i - 1][j];
				}
			}
		}
		
		for (int i = 0; i <= w; i++){
			if (dp[n][i] > dp[n][max]){
				max = i;
			}
		}
		
		printf("Case:%2d\n%d\n%d\n", cnt++, dp[n][max], max);
	}
	
	return (0);
}

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

int table[1001];

int main(){
  int C;
  for(int i=1; cin>>C, C; ++i)
  {
    for (int k = 0; k < 1001; ++k) table[k] = -1e9;
    table[0] = 0;

    int n; cin >> n;
    for(;n--;) {
      int v, w; char comma;
      cin >> v >> comma >> w;
      for (int k = 1000 - w; k >=0; --k) { // table の上から更新
        table[k+w] = max<int>(table[k+w], table[k] + v);
      }
    }

    int W, V=table[0];
    for (int k = 0; k <= C; ++k) { // 最小のインデクスを答える必要
      if (V < table[k]) {
        V = table[k];
        W = k;
      }
    }

    printf("Case %d:\n%d\n%d\n", i, V, W);
  }
}

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

long long dp[1001];
int main(){
	long long w, n, ans_w = 0, ans_val = 0;
	for (int dataset = 1; cin >> w, w; dataset++){
		long long t_w, t_val;
		cin >> n;
		for (long long i = 0; i <= w; i++) dp[i] = -1;
		dp[0] = 0;
		for (long long i = 0; i < n; i++){
			scanf("%lld,%lld", &t_val, &t_w);
			for (long long j = w; j >= 0; j--){
				//printf("%d ", dp[j]);
				if (dp[j] == -1 || j + t_w > w || dp[j + t_w] > dp[j] + t_val)
					continue;
				dp[j + t_w] = dp[j] + t_val;
			}
			//puts("");
		}
		for (long long i = 0; i <= w; i++){
			//printf("%d ", dp[i]);
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		//puts("");
		printf("Case %d:\n%lld\n%lld\n", dataset, ans_val, ans_w);
	}
	return 0;
}

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

const int MAX_N = 1010;
const int MAX_W = 1010;
int memo[MAX_N][MAX_W];
int W, N, v[MAX_N], w[MAX_N], min_w;

int main()
{
    for(int c = 1; ~scanf("%d", &W) && W; ++c)
    {
        scanf("%d", &N);
        for(int i = 0; i < N; ++i)
            scanf("%d,%d", v+i, w+i);
        fill_n(memo[N], W+1, 0);
        for(int i = N - 1; i >= 0; --i)
            for(int j = 0; j <= W; ++j)
                if(j < w[i])
                    memo[i][j] = memo[i+1][j];
                else
                    memo[i][j] = max(memo[i+1][j], memo[i+1][j-w[i]] + v[i]);
        min_w = W;
        while(memo[0][W] == memo[0][min_w-1])
            --min_w;
        printf("Case %d:\n%d\n%d\n", c, memo[0][W], min_w);
    }
}