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>

using namespace std;

const int MAX_W = 1001;
const int MAX_T = 1001;

int dp[MAX_T][MAX_W];

void init(){
  for(int i = 0; i < MAX_T; i++){
    fill(dp[i],dp[i]+MAX_W,0);
  }
}

int main(){
  int rev = 0, max;
  while(cin >> max && max){
    rev++;
    init();
    int num;
    cin >> num;
    for(int i = 1; i <= num; i++){
      char trash;
      int price, weight;
      cin >> price >> trash >> weight;
      for(int j = 0; j <= max; j++){
	if(weight <= j){
	  dp[i][j] = price + dp[i-1][j-weight];
	}
      }
    }
    int ablep = 0, ablew = 0;
    for(int i = 0; i <= num; i++){
      for(int j = 0; j <= max; j++){
	if(ablep < dp[i][j]){
	  ablep = dp[i][j];
	  ablew = j;
	}else if(ablep == dp[i][j]){
	  if(ablew > j) ablew = j;
	}
      }
    }
    cout << "Case " << rev << ":" << endl;
    cout << ablep << endl << ablew << endl;

  }
  return 0;
}

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

struct Item{
	int v,w;
	int idx;	
};
const int INF = 1000000000;
class Knapsack{
public:
	
	
	int maximumWeight;
	vector<Item> items;
	Knapsack(int maximumWeight_,vector<Item> items_) : maximumWeight(maximumWeight_),items(items_) {
		used.resize(items.size(),false);
		totalWeight = totalValue = 0;
		sort(items.begin(),items.end(),[](Item a,Item b){ return a.v * b.w > b.v * a.w; });
	}
	int totalValue;
	int totalWeight;
	vector<bool> used;
	bool operator < (const Knapsack &op) const {
		if( totalValue == op.totalValue )
			return totalWeight > op.totalWeight;
		return totalValue < op.totalValue;
	}

	bool canPut(const Item &x){
		return !used[x.idx] && x.w <= maximumWeight - totalWeight;
	}
	bool putItem(const Item &x){
		if( canPut(x) ){
			totalWeight += x.w;
			totalValue += x.v;
			used[x.idx] = true;
			return true; // success
		}else{
			return false; // failure
		}
	}
	bool removeItem(const Item &x){
		if( used[x.idx] ){
			totalWeight -= x.w;
			totalValue -= x.v;
			used[x.idx] = false;
			return true;
		}
		return false;
	}
	int swapAtoB(Item x,Item y){
		if( used[x.idx] && !used[y.idx] ){
			if( totalWeight - x.w + y.w > maximumWeight ) return -INF;
			return y.v - x.v;
		}else{
			return -INF;
		}
	}
	Knapsack knapSackDP(int scale) const{
		Knapsack res = *this;
		int maximumValue = -INF;
		for( auto i : res.items ){
			maximumValue = max(maximumValue,i.v);
		}

		double rate = min(1.0,1.*scale/maximumValue);
		for( auto &i : res.items ){
			i.v = (int)(ceil(rate*i.v)+1e-9);
		}
		
		vector<int> dp(scale+1,INF),lastId(scale+1,-1);
		dp[0] = 0;
		for(int i = 0 ; i < res.items.size() ; i++){
			if( res.used[i] ) continue;
			for(int j = scale ; j >= res.items[i].v ; j--){
				if( dp[j-res.items[i].v] + res.items[i].w < dp[j] ){
					dp[j] = dp[j-res.items[i].v] + res.items[i].w;
					lastId[j] = i;
				}
			}
		}
		for(int i = scale ; i >= 0 ; i--){
			if( dp[i] <= maximumWeight - totalWeight ){
				int cur = i;
				while(cur > 0){
					int p = lastId[cur];
					res.putItem(res.items[p]);
					cur -= res.items[p].v;
					if( p == -1 ) exit(0);
				}
				break;
			}
		}
		return res;
	}
	Knapsack localImprovement() const{
		Knapsack res = *this;
		while(true){
			// ????¬??????????????????????????????§??????????
			for(int i = 0 ; i < res.items.size() ; i++){	
				res.putItem(res.items[i]);
			}
			auto reflesh = [&](){
				for(int i = 0 ; i < res.items.size() ; i++){
					if( res.used[i] ){

						auto tmp = res;
						tmp.removeItem(tmp.items[i]);
						for(int j = 0 ; j < tmp.items.size() ; j++){
							if( i != j && !tmp.used[j] ){	
								tmp.putItem(tmp.items[j]);
							}
						}
						if( res < tmp ){
							res = tmp;
							return true;
						}
					}
				}
				return false;
			};
			if( reflesh() ) continue;
			
			auto reflesh2 = [&](){
				for(int i = 0 ; i < res.items.size() ; i++){
					if( res.used[i] ){

						for(int j = 0 ; j < res.items.size() ; j++){
							if( !res.used[j] ){
								if( res.swapAtoB(res.items[i],res.items[j]) >= 0 ){
									res.removeItem(res.items[i]);
									res.putItem(res.items[j]);
									return true;
								}
							}
						}
					}
				}
				return false;
			};
			if( reflesh2() ) continue;
			break;
		}
		return res;
	}
};




int main(){
	int n,m;
	int t=1;
	while(cin >> n >> m && n){
		vector<Item> items;
		for(int i = 0 ; i < m ; i++){
			int x,y;
			scanf("%d,%d",&x,&y);
			items.push_back({x,y,i});
		}
		Knapsack knap(n,items);
		knap = knap.knapSackDP(11000).localImprovement();
		cout << "Case " << t++ << ":" << endl;
		//knap = knap.localImprovement();
		cout << knap.totalValue << "\n" << knap.totalWeight << endl;
	}
}

C++: const int MAX_N = 1000; // n????????§???
const int MAX_W = 5000; // W????????§???

// ??\???
int n, W;
int w[MAX_N], v[MAX_N];

// DP????????????
// dp[i][j]???i????????\?????????????????????????????????j??\?????????????????????????????¨??????????????????????????§????????¨??????
int dp[MAX_N + 1][MAX_W + 1];

void solve_dp2() {
  for (int j = 0; j <= W; j++) {
    dp[n][j] = 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= W; j++) {
      if (j < w[i])
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[0][W] << endl;
}

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

struct treasure{
	int val;
	bool combi[1000];
};

treasure dp[1001][1001];
int main(){
	int w, n;
	for (int dataset = 1; cin >> w, w; dataset++){
		cin >> n;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < w; j++){
				dp[i][j].val = -1;
				for (int k = 0; k < n; k++){
					dp[i][j].combi[k] = false;
				}
			}
		}
		dp[0][0].val = 0;
		int t_w[1000], t_val[1000], ans_val = 0, ans_w = 0;
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &t_val[i], &t_w[i]);
		}
		for (int i = 0; i <= n; i++){
			for (int j = 0; j < w; j++){
				if (dp[i][j].val == -1) continue;
				if (dp[i][j].val > ans_val){
					ans_val = dp[i][j].val;
					ans_w = j;
				}
				for (int k = 0; k < n; k++){
					if (dp[i][j].combi[k] || j + t_w[k] >= w
						|| dp[i + 1][j + t_w[k]].val > dp[i][j].val + t_val[k])
						continue;
					dp[i + 1][j + t_w[k]] = dp[i][j];
					dp[i + 1][j + t_w[k]].val = dp[i][j].val + t_val[k];
					dp[i + 1][j + t_w[k]].combi[k] = true;
				}
			}
		}
		printf("Case %d:\n%d\n%d\n", dataset, ans_val, ans_w);
	}
	return 0;
}