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 <algorithm>
#include <stdio.h>

using namespace std;

int main(){
        int w;
        cin >> w;
        int count = 0;
        while(w){
                count++;
                int n;
                cin >> n;
                int list[n][2];
                for(int i = 0; i < n; i++){
                        scanf("%d,%d", &list[i][0],&list[i][1]);
                }   
                int c[n+1][w+1];
                for(int i = 0; i < n+1; i++){
                        c[i][0] = 0;
                }   
                for(int i = 0; i < w+1; i++){
                        c[0][i] = 0;
                }   
                for(int i = 1; i < n+1; i++){
                        for(int j = 1; j < w+1; j++){
                                if(list[i-1][1] <= j){ 
                                        c[i][j] = max(c[i-1][j], c[i-1][j-list[i-1][1]]+list[i-1][0]);
                                }else{
                                        c[i][j] = c[i-1][j];
                                }   
                        }   
                }   
                int p = 0;
                int _w = 0;
                for(int i = 1; i <= w; i++){
                        if(c[n][i-1] < c[n][i]){
                                p = c[n][i];
                                _w = i;
                        }   
                }   
                cout << "Case " << count << ":" << endl << p << endl << _w << endl;
                cin >> w;
        }   
}

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

using namespace std;

int N,W;

struct Treasurea
{

  int value;
  int weight;
  int flag;

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

};

struct Memo
{

  Treasurea record;
  bool flag;

  Memo()
  {
    flag = false;
  }

};

Memo memo[1001][1001];

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(memo[correntWeight][kind].flag) return memo[correntWeight][kind].record;
  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;

      Treasurea temp = knapsack(treasurea,correntWeight - treasurea[i].weight,correntValue + treasurea[i].value,i + 1);

      if(total.value < temp.value || (total.value == temp.value && total.weight > temp.weight))
        total = temp;

      treasurea[i].flag = false;

    }

  memo[correntWeight][kind].record = total;
  memo[correntWeight][kind].flag = true;

  return total;

}

int main()
{

  int case_number = 0;

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

      cin >> N;

      vector<Treasurea> treasurea(N);
      Treasurea result;

      for(int i = 0; i < N; ++i) scanf("%d,%d",&treasurea[i].value,&treasurea[i].weight);

      memset(memo,0,sizeof(memo));

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

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

    }

  return 0;

}

C++: #include<cstdio>
#include<algorithm>
#include<windows.h>

using namespace std;

int W,N,c;
int w[1001],p[1001];
int dp[10001][1000];

int main(void){
	while(scanf("%d", &W)&&W){
		int ans;
		scanf("%d", &N);
		for(int i = 1; i <= N; i++){
			scanf("%d,%d", &p[i], &w[i]);
		}
		for(int i = 1; i <= N; i++){
			for(int j = 1;j <= W; j++){
				if(i-1 < 0 || j-w[i]<0)continue;
				dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+p[i]);
			}
		}
		c++;
		printf("Case%2d:\n",c);
		printf("%d\n",dp[N][W]);
		for(int i = 0; i < W; i++){
			if(dp[N][W] == dp[N][W-i]){
				ans = i;
			}
		}
		printf("%d\n",W-ans);
		/*ツ渉可甘コツ可サ*/
		for(int i = 0; i < 1001; i++){
			w[i] = 0;
			p[i] = 0;
		}
		for(int i = 0; i < 10001; i++){
			for(int j = 0; j < 1000; j++){
				dp[i][j] = 0;
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fr first
#define sc second
using namespace std;

int w,n;
typedef pair<int,int> P;
P dp[1001][1001];
bool chg[1001][1001];
vector <P> dat;

P rec(int idx,int wi,int va){
    if(idx == n){
        return P(va,wi);
    }
    if(chg[idx][wi]) return dp[idx][wi];
    P p;
    if(wi - dat[idx].sc >= 0) p = max(rec(idx+1,wi-dat[idx].sc,va+dat[idx].fr),rec(idx+1,wi,va));
    else p = rec(idx+1,wi,va);
    chg[idx][wi] = true;
    return dp[idx][wi] = p;
}

int main(){
    while(cin >> w,w){
        int a = 0;
        memset(dp,-1,sizeof(dp));
        memset(chg,false,sizeof(chg));
        cin >> n;
        for(int i=0;i<n;i++){
            int t,m; scanf("%d,%d",&t,&m); dat.push_back(P(t,m));
        }
        P p = rec(0,w,0);
        cout << "Case " << ++a << ":\n" << p.fr << endl << w - p.sc << endl;
    }
}

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

using namespace std;

const int W = 1001;
const int N = 1001; 

int table[W][N];

void solve(int n,int lim, vector<int> price, vector<int> weight){
  int maxi_p = 0,maxi_w = 0;
  for(int i = 0;i < W;i++){
    for(int j = 0;j < N;j++){
      table[i][j] = 0;
    }
  }
  
  for(int i = 0;i < n;i++){
    table[weight[i]][i] = price[i]; 
  }
  
  
  for(int i = 0;i <=lim;i++){
    for(int j = 1;j < n;j++){
      table[i][j] = table[i][j-1]; 
      if(i - weight[j] < 0)continue;
      table[i][j] =max(table[i - weight[j]][j-1]+price[j],table[i][j-1]);
    } 
  }

  for(int i = 0;i <= lim;i++){
    if(maxi_p < table[i][n-1]){
      maxi_p = table[i][n-1];
      maxi_w = i;
    }

    
  }
  cout << maxi_p << endl; 
  cout << maxi_w << endl;
}  

int main(){
  int count = 1;
  int lim;
  int n;
  int price,weight;  
  while(cin >> lim >> n &&lim){

    vector<int> a,b;
    int tmp;
    char damy;
    cout << "Case "<< count <<":"<<endl;
    for(int i = 0;i < n;i++){
      cin >> price >> damy >> weight;
      a.push_back(price);
      b.push_back(weight);
    }
    
    solve(n,lim,a,b);
    count++;
  }
  return 0;
}