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<cstdio>
using namespace std;
int field[1001][1001];
int main(void){
    int w;
    int ans_e,ans_w;
    int c = 0;
    while(scanf("%d",&w),w){
        ans_e = 0;
        ans_w = 0;
        c++;
        int i_exp[1001];
        int i_cos[1001];
        int n;
        scanf("%d",&n);
        for(int i = 1;i <= n;i++){
            scanf("%d,%d",&i_exp[i],&i_cos[i]);
        }
        for(int i = 1;i <= n;i++){
            for(int j = 0;j <= w;j++){
                field[i][j] = field[i-1][j];
                if(j - i_cos[i] >= 0 && field[i][j] <= field[i-1][j-i_cos[i]]+i_exp[i]) field[i][j] = field[i-1][j-i_cos[i]]+i_exp[i];
            }
        }
        ans_e = field[n][w];
        for(int i = w;i >= 0;i--){
            if(ans_e != field[n][i]){
                ans_w = i+1;
                break;
            }
        }
        printf("Case %d:\n%d\n%d\n",c,ans_e,ans_w);
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int W,N,w[1001],v[1001];
int memo[1001][1001],ans2;
int func(int i,int j){
    if(i==N)return 0;
    if(memo[i][j]!=-1)return memo[i][j];
    if(j+w[i]>W)return func(i+1,j);
    int res;
    if(j<W){
        ans2=max(ans2,j);
    }
    res=max(func(i+1,j),func(i+1,j+w[i])+v[i]);
    return memo[i][j]=res;
}
int main(){
    int ans,cou=0,hinan;
    while(cin>>W,W){
        cin>>N;
        fill(memo[0],memo[0]+1001*1001,-1);
        for(int i=0;i<N;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        ans=func(0,0);
        hinan=ans2;
        for(int i=0;i<N;i++){
            if(w[i]==ans2){
                for(int j=0;j<N;j++){
                    if(v[j]==ans-v[i])hinan=w[j];
                }
                break;
            }
        }
        cout<<ans<<endl<<ans2+hinan<<endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
int main(){
    int w, n;
    int ct = 1;
    while(cin >> w >> n, w){
    int va[n], we[n];
    for(int i = 0; i < n; i++)  scanf("%d, %d",&va[i], &we[i]);
    int dp[1010][1010];
    for(int i = 0; i < 1010; i++)
        for(int j = 0; j < 1010; j++)
            dp[i][j] = 0;
    for(int i = 0; i < n; i++)
        for(int j = 0; j <= w; j++){
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
            if(j + we[i] <= w)
                dp[i + 1][j + we[i]] = max(dp[i + 1][j + we[i]], dp[i][j] + va[i]);
        }
    int macs = 0, sumwe = 1010;
    for(int i = 0; i <= n; i++)
        for(int j = 0; j <= w; j++)
            if(dp[i][j] == dp[n][w])
                sumwe = min(j, sumwe);
    cout << "Case " << ct << ":" << endl << dp[n][w] << endl << sumwe << endl;
    ct++;
    }
    return 0;
}

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

using namespace std;

int N,W;

struct Treasurea
{

  int value;
  int weight;
  int flag;

  Treasurea()
  {
    value = 0;
    weight = 0;
    flag = false;
  }

  Treasurea& operator+=(const Treasurea &obj)
  {
    value += obj.value;
    weight += obj.weight;
  };

};

Treasurea knapsack(vector<Treasurea> &treasurea, int correntWeight,int correntValue, int kind)
{

  if(correntWeight < 0)
    {
      Treasurea temp;
      temp.weight = W - (correntWeight + treasurea[kind - 1].weight);
      temp.value = correntValue - treasurea[kind - 1].value;
      return temp;
    }
  else if(kind == treasurea.size())
    {
      Treasurea temp;
      temp.weight = W - correntWeight;
      temp.value = correntValue;
      return temp;
    }

  Treasurea total;

  for(int i = 0;i < treasurea.size(); ++i)
    {

      if(treasurea[i].flag) continue;

      treasurea[i].flag = true;

      if((total.value < knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).value ) || (total.value == knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).value && total.weight > knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1).weight))
         total = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

      treasurea[i].flag = false;

    }

  return total;

}

int main()
{

  int case_number = 0;
  vector<Treasurea> treasurea(N);
  Treasurea result;

  while(cin >> W && W != 0)
    {

      cin >> N;

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

          Treasurea temp;
          scanf("%d,%d",&temp.value,&temp.weight);
          treasurea.push_back(temp);

        }

      result = knapsack(treasurea,W,0,0);

      cout << "Case " << ++case_number << endl << result.value << endl << result.weight << endl;

      treasurea.erase(treasurea.begin(),treasurea.end());

    }

  return 0;

}

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

struct treasure{
	int value;
	int weight;
};
bool comp(treasure t1, treasure t2){
	if(t1.weight <= t2.weight) return true;
	else return false;
}

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	while(cin >> w){
		int n;
		int cost[maxw + 1];
		int best[maxw + 1];
		for(int i = 0; i <= maxw; i++){
			cost[i] = 0;
			best[i] = 0;
		}
		vector<treasure> treasures;
		cin >> n;
		for(int i = 0; i < n; i++){
			treasure t;
			char c;
			cin >> t.value >> c >> t.weight;
			treasures.push_back(t);
		}
		sort(treasures.begin(), treasures.end(), comp);
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= treasures[j].weight)
					if(cost[k] < cost[k - treasures[j].weight] + treasures[j].value)
					{
						cost[k] = cost[k - treasures[j].weight] + treasures[j].value;
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;cost[w] == cost[w-1]; w--);
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - treasures[best[l]].weight >= 0;){
			if(l - treasures[best[l]].weight >= 0){
				total += treasures[best[l]].weight;
				l -= treasures[best[l]].weight;
			}
		}
		cout << total << endl;
	}
	return 0;
}

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

#define SIZE 1000

int w, n;
int dp[SIZE + 1][SIZE + 1];
int va[SIZE], wa[SIZE];

int main() {
	int u = 0;
	while (cin >> w) {
		if (w == 0) break;
		cin >> n;
		u++;

		char t;
		for (int i = 0; i < n; i++) {
			cin >> va[i] >> t >> wa[i];
		}

		dp[1001][1001] = { 0 };
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= w; j++) {
				if (wa[i] <= j) {
					dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - wa[i]] + va[i]);
				} else {
					dp[i][j] = dp[i - 1][j];
				}
			}
		}

		for (int i = w; i >= 0; i--) {
			if (dp[n][w] > dp[n][i]) {
				cout << "Case " << u << ":" << endl;
				cout << dp[n][i + 1] << endl;;
				cout << i + 1 << endl;
				break;
			}
		}
	}
	return 0;
}