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 <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()
#define tpl(...) make_tuple(__VA_ARGS__)

typedef long long LL;
typedef pair<int, int> P; typedef pair<int, P> IP; typedef pair<P, P> PP;
typedef vector<int> Array; typedef vector<vector<int> > Array2;
typedef vector<LL> LArray; typedef vector<P> PArray; typedef vector<string> SArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

inline int getInt() { int itiv; return (cin >> itiv, itiv); }
template <typename T> void readAll(vector<T>& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i]; }
template <typename T, typename U> void readAll(vector<pair<T, U> >& vec) { for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> vec[i].second; }
template < typename T > inline string toString( const T &a ){ ostringstream oss; oss << a; return oss.str(); };
inline bool between(int min, int max, int n) { return min <= n && n <= max; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

int n, w;
const int M_W = 1001;
const int M_N = 1001;
P dp[M_N][M_W];
PArray arr;

P dfs(int idx, int weight)
{
	if(idx == n) return P(0, weight);
	if(dp[idx][weight].first) return dp[idx][weight];

	P v1 = P(-1, weight + arr[idx].second);
	if(v1.second < w)
	{
		P ret = dfs(idx + 1, weight + arr[idx].second);
		v1.first = ret.first + arr[idx].first;
		v1.second = ret.second;
	}
	P v2 = dfs(idx + 1, weight);
	return dp[idx][weight] = max(v1, v2);
}

int main(void)
{
	ios::sync_with_stdio(false);
	int counter = 0;
	while(cin >> w && w)
	{
		counter++;
		cin >> n;
		arr.resize(n);
		char c;
		for (int i = 0; i < n; i++)
		{
			cin >> arr[i].first >> c >> arr[i].second;
		}
		memset(dp, 0, sizeof(dp));
		P ans = dfs(0, 0);
		
		cout << "Case " << counter << ":" << endl;
		cout << ans.first << endl;
		cout << ans.second << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
#define fir first
#define sec second
 
typedef pair <int, int> P;
vector <P> v(1028);
 
int dp[1028][2048];
 
int main(void)
{
    int cnt = 1;
    while (1){
        int w, n;
        cin >> w; if (w == 0) break;
        cin >> n;
        char a;
        for (int i = 0; i < n; i++){
            scanf("%d,%d", &v[i].fir, &v[i].sec);
        }
        for (int i = 0; i < n; i++){
            for (int j = 0; j <= w; j++){
                if (j < v[i].sec) dp[i+1][j] = dp[i][j];
                else dp[i+1][j] = max(dp[i][j], dp[i][j-v[i].sec]+v[i].fir);
            }
        }
        int res1 = 0, res2 = 0;
        for (int i = 1; i <= w; i++){
            if (res1 < dp[n][i]){
                res1 = dp[n][i];
                res2 = i;
            }
        }
        cout << "Case " << cnt << ":" << endl << res1 << endl << res2 << endl;
        cnt++;
    }
    return 0;
}

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;
	  if(i)dp[i][j]+= dp[i-1][j-weight];
	}
      }
    }
    int ablep = 0, ablew = 10000;
    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<iostream>
#include<memory.h>
#include<algorithm>
using namespace std;
int main()
{
	int W,n,c=0;
	int w,v;
	int dp[1001][1001];
	while (1){
		cin >> W;
		if (W == 0)break;
		c++;
		cin >> n;

		for (int i = 1; i <= n; i++){
			cin >> v >> w;
			for (int i2 = 0; i2 <= W; i2++){
				if (i2 < w)
					dp[i][i2] = dp[i - 1][i2];
				else
					dp[i][i2] = max(dp[i - 1][i2], dp[i - 1][w - i2] + v);
			}
		}
		int maxv=0, maxw;
		for (int i = 0; i <= W; i++){
			if (maxv < dp[n][i]){
				maxv = dp[n][i];
				maxw = i;
			}
		}
		cout << "Case" << c << ":" << endl;
		cout << maxv << endl << maxw << endl;
	}
	
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;

int main(){
	int W,N,cou=0;
	int tr[1001],we[1001];
	while(cin >> W , W){
		cou++;
		cin >> N;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		int dp[1010];
		for(int i=0;i<=N;i++){
			dp[i]= 0;
		}
		for(int i=0;i<N;i++){
			for(int j=W;j>=we[i];j--){
				dp[j] = max(dp[i],dp[j-we[i]]+tr[i]);
			}
		}
		int ret = 0;
		for(int i=0;i<W;i++){
			if(dp[i] > dp[ret]) ret = i;
		}
		cout << "Case " << cou << ":" << endl;
		cout << dp[ret] << endl;
		cout << ret << endl;
	}
}

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