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
}

Python: import re
def solve(w,v):
    n = len(v)
    val0 = 0
    wgt0 = 0
    for i in range(1,2**n):
        s = bin(i)
        s1 = '0'*n + re.sub(r'0b', '', s)
        t = list(map(int, list(s1[-n:])))
        val = wgt = 0
         
        for i in range(n):
            if t[i]==1:
                val += v[i][0]  
                wgt += v[i][1]  
        if wgt > w:
            continue
        if val > val0 or (val==val0 and wgt<wgt0):
            val0 = val
            wgt0 = wgt
    return(val0,wgt0)

case=0
while True:
    case += 1
    f = []
    wlim = int(input().strip())
    if wlim==0:
        break
    n = int(input().strip())
    for i in range(n):
        v,w = map(int,input().strip().split(','))
        f.append((v,w))
    
    val,wgt = solve(wlim,f)
    
    print('Case %d:' % case)
    print(val)
    print(wgt)

Python: if __name__ == '__main__':
    W = int(input())
    v = [0]
    w = [0]
    d = []
    m = []
    counter = 1
    result = []
    while W != 0:
        N = int(input())
        v[:] = [0]
        w[:] = [0]
        d[:] = []
        m[:] = []

        """the initialization of array d"""
        for x in range(W + 1):
            temp = [0] * (N + 1)
            d.append(temp)

        for x in range(W + 1):
            temp = [0] * (N + 1)
            m.append(temp)

        """prepare the dataset"""

        for x in range(N):
            i = input().split(',')
            v.append(int(i[0]))
            w.append(int(i[1]))

        """processing"""

        for weight in range(W + 1):
            for kind in range(N + 1):
                if weight != 0 and kind != 0:
                    if w[kind] > weight:
                        d[weight][kind] = d[weight][kind - 1]
                        m[weight][kind] = m[weight][kind - 1]
                    else:
                        if d[weight - w[kind]][kind - 1] + v[kind] > d[weight][kind - 1]:
                            d[weight][kind] = d[weight - w[kind]][kind - 1] + v[kind]
                            m[weight][kind] = m[weight - w[kind]][kind - 1] + w[kind]
                        elif d[weight - w[kind]][kind - 1] + v[kind] == d[weight][kind - 1]:
                            d[weight][kind] = d[weight][kind - 1]
                            if m[weight - w[kind]][kind - 1] + w[kind] < m[weight][kind - 1]:
                                m[weight][kind] = m[weight - w[kind]][kind- 1] + w[kind]
                            else:
                                m[weight][kind] = m[weight][kind - 1]
                        else:
                            d[weight][kind] = d[weight][kind - 1]
                            m[weight][kind] = m[weight][kind - 1]

        result.append("Case " + counter.__str__() + ":")
        result.append(d[W][N])
        result.append(m[W][N])

        W = int(input())
        counter += 1

    for line in result:
        print(line)

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int dp[1000][1000] = {{0}};
int main(){
  int w,n;
  char ch;
  int P[1000]={0},W[1000]={0},t = 1;
  while(cin >> w,w ){
    cin >> n;
    for(int i = 0 ; i < n ; i++ ){
      cin >> P[i] >> ch >> W[i];
    }
    for(int i = 0 ; i < n ; i++ ){
      for(int j = 0 ; j <= w ; j++ ){
	if(j - W[i] >= 0 ){ 
	  dp[i+1][j] = max(dp[i][j],dp[i][j-W[i]] + P[i]);
	}else dp[i+1][j] = dp[i][j];
      }
    }
    cout << "Case " << t << ":" << endl;
    for(int i = w ; i >= 0 ; i--){
      if(dp[n][i] != dp[n][w]){
	cout << dp[n][w] << endl << i + 1 << endl;
	break;
      }
    }
    t++;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,x,n) for(int i=x;i<n;i++)
#define pb push_back
#define MAX 100005
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;



signed main(){
  int W,N;
  int c=1;
  while(cin>>W>>N,W){
    vector<int> v(N),w(N);
    char x;
    vector<vector<int> > dp(N+1,vector<int>(W+1));
    rep(i,N)cin>>v[i]>>x>>w[i];
    loop(i,1,N+1){
      rep(j,W+1){
        if(j-w[i-1]>=0){
          dp[i][j]=max(dp[i-1][j-w[i-1]]+v[i-1],dp[i-1][j]);
        }else{
          dp[i][j]=dp[i-1][j];
        }
      }
    }
    int val=dp[N][W],wei;
    rep(i,W+1){
      if(dp[N][i]==val)wei=i;
    }
    cout<<"Case "<<c<<':'<<endl;
    cout<<val<<endl;
    cout<<wei<<endl;
    c++;
  }

  return 0;
}

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

using namespace std;

int main( void )
{
	int map[1001], save[1001];
	int w; long long count;
	count = 1;
	while(cin >> w, w) {
		++w;
		for(int i=0; i < w; ++i) map[i] = save[i] = 0;
		int n, value, weight;
		cin >> n;
		scanf("%d,%d", &value, &weight);
		save[weight] = map[weight] = max(save[weight], value);
		for(int i=1; i < n; ++i) {
			scanf("%d,%d", &value, &weight);
			save[weight] = max(save[weight], value);
			for(int j=0; j < w; ++j) {
				if(map[j]) {
					int ww = j + weight;
					if(ww < w)
						save[ww] = max(save[ww], map[j] + value);
					save[j] = max(save[j], map[j]);
				}
			}
			for(int j=0; j < w; ++j) {
				map[j] = save[j]; save[j] = 0;
			}
		}
		int Max = 0, idx;
		for(int i=1; i < w; ++i) {
			if(Max < map[i]) {
				Max = map[i];
				idx = i;
			}
		}
		cout << "Case " << count++ << ':' << endl;
		cout << Max << endl;
		cout << idx << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <map>
#include <stdio.h>
int main(){
  using std::cin;
  using std::cout;
  using std::endl;
  using std::map;
  int weight,n,w,v,s,c=1;
  map<int,int> m;
  while(cin>>weight){
    if(!weight)return 0;
    cin>>n;
    map<int,int>::iterator it;
    for(;n;n--){
      scanf("%d,%d",&v,&w);
      if(m.empty()){m[w]=v;continue;}
      it = m.begin();
      while(it!=m.end()){
	s=(*it).second+v;
	  if((*it).first+w<weight){
	    if(m[(*it).first+w]<s)m[(*it).first+w]=(*it).second+v;
	  }
	it++;
      }
      m[w]=v;
    }
    it=m.end();
    it--;
    cout<<"Case "<<c++<<":"<<endl<<(*it).second<<endl<<(*it).first<<endl;
    m.clear();
  }
}

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

#define MAX 1001

int dp[MAX][MAX];
int v[MAX], w[MAX];

int main() {
	int W, n, u = 0;
	char t;
	while (cin >> W) {
		if (W == 0) break;
		cin >> n;
		u++;

		dp[MAX][MAX] = { 0 };
		for (int i = 0; i < n; i++) {
			cin >> v[i] >> t >> w[i];
		}

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

		int mv = 0, mw;
		for (int i = 0; i <= W; i++) {
			if (dp[n][i] > mv) {
				mv = dp[n][i];
				mw = i;
			}
		}

		cout << "Case " << u << ":" << endl;
		cout << mv << endl;
		cout << mw << endl;
	}
	return 0;
}