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>

#pragma warning(disable: 4996)

using namespace std;

#define MAX_N 1111
#define MAX_W 1111

int W, N, c, v[MAX_N], w[MAX_N], dp[MAX_N + 1][MAX_W + 1];

int main()
{
	while (true)
	{
		c++;

		cin >> W;

		if (!W) { break; }

		cin >> N;

		for (int i = 0; i < N; i++)
		{
			scanf("%d,%d", &v[i], &w[i]);
		}

		for (int j = 0; j <= MAX_W; j++)
		{
			dp[0][j] = 0;
		}

		for (int i = 1; i <= MAX_N; i++)
		{
			for (int j = 0; j <= MAX_W; j++)
			{
				dp[i][j] = -(1 << 28);
			}
		}

		for (int i = 1; i <= N; i++)
		{
			for (int j = 0; j <= W; j++)
			{
				dp[i][j] = max(dp[i - 1][j], j >= w[i] ? dp[i - 1][j - w[i]] + v[i] : 0);
			}
		}

		cout << "Case " << c << ':' << endl;
		cout << dp[N][W] << endl;
		cout << distance(dp[N], lower_bound(dp[N], dp[N] + W + 1, dp[N][W])) << endl;
	}

	return 0;
}

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

int main(){
	int w, cntcase = 1;
	while (cin >> w, w){
		int n, dp[1000], treas_val, treas_w, ans_val = 0, ans_w = 0;
		cin >> n;
		for (int i = 0; i < w; i++) dp[i] = -1;
		dp[0] = 0;
		for (int i = 0; i < n; i++){
			scanf("%d,%d", &treas_val, &treas_w);
			for (int j = w - 1; j >= 0; j--){
				if (dp[j] != -1 && i + treas_w < w && dp[j + treas_w] < dp[j] + treas_val){
					dp[j + treas_w] = dp[j] + treas_val;
				}
			}
		}
		for (int i = 0; i < w; i++){
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		printf("Case %d:\n%d\n%d\n", cntcase, ans_val, ans_w);
		cntcase++;
	}
	return 0;
}

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) { char c;for (int i = 0, size = vec.size(); i < size; i++) cin >> vec[i].first >> c >> 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 w, n;

// index, weight
int dp[1010][1010];
PArray treasure;

int main(void)
{
	ios::sync_with_stdio(false);
	int count = 1;
	while(cin >> w && w)
	{
		n = getInt();
		for (int i = 0; i <= n; i++)
		{
			memset(dp[i], 0, sizeof(dp[i]));
		}
		treasure.resize(n);
		readAll(treasure);
		P ans = P(-1, INF);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j <= w; j++)
			{
				int nw = j + treasure[i].second;
				if(nw > w) continue;
				dp[i + 1][nw] = max(dp[i + 1][nw], dp[i][j] + treasure[i].first);
				if(ans.first == dp[i + 1][nw] && ans.second > nw)
					ans = P(dp[i + 1][nw], nw);
				else if(ans.first < dp[i + 1][nw]) ans = P(dp[i + 1][nw], nw);
			}
		}

		cout << "Case " << count << ':' << endl;
		cout << ans.first << endl << ans.second << endl;
		count++;
	}
	return 0;
}

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

using namespace std;

const int MAX_w = 1000;
const int MAX_v = 10000;

int dp[MAX_w + 1][MAX_v + 1];


int n, wet ;
int w[MAX_w], v[MAX_v];

// i番目以降の品物から重さの和がj以下なるように選んだときの、
// 取りうる価値の総和の最大値を返す関数
int rec(int i, int j) {
  int res;
  if(dp[i][j] != -1){
  	
	return dp[i][j];
  }

  if (i == n) {
    // 品物がもう残っていないときは、価値の和の最大値は0で確定
    res = 0;
  } else if (j < w[i]) {
    // 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
    // i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
    res = rec(i + 1, j);
  } else {
    // 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
    res = max(
        rec(i + 1, j),
        rec(i + 1, j - w[i]) + v[i]
    );
  }

  return dp[i][j] = res;
}

void solve(int ca) {
  // 0番目以降で容量W以下の場合の結果を表示する
  int wmin;
  
  memset(dp, -1, sizeof(dp));
  
  cout << "Case" << " " << ca << ":" <<endl; 
  
  int ans =  rec(0, wet) ;
  cout << ans << endl;
  
  for (int w = wet -1 ; w>=0 ; w--){
  	if( ans != rec(0,w)){
		 wmin = w+1;
		 break;
  	}
  }
  
  cout << wmin << endl;  
  
}


int main(){
	
	char x;
	int ca = 0;
	
	while(1){
			
		ca++;
		cin >> wet;
		if(wet == 0 ) break;
		cin >> n;
		
		for(int i = 0 ;  i<n ; i++){
			
			cin >> v[i] >> x >> w[i];
		}
		
		
		 solve(ca);
		
	}
	
	return 0;
	
}

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

int dp[1010][1010];

int main() {
	int w;
	int x = 1;
	while (cin >> w, w) {
		int n;
		cin >> n;
		int v[1005], m[1005];
		for (int i = 0;i < n;i++) {	scanf("%d,%d", &v[i], &m[i]);}

		for (int i = 0;i < n;i++)
			for (int j = 0;j < w;j++)dp[i][j] = -1;
		dp[0][0] = 0;

		for (int i = 0;i < n;i++) {
			for (int j = 0;j <= w;j++) {
				if (dp[i][j] !=-1) {
					dp[i + 1][j] = 0;
					if (j + m[i] <= w)dp[i + 1][j + m[i]] = 0;
					dp[i + 1][j] = dp[i][j];
					dp[i + 1][j + m[i]] = dp[i][j] + v[i];
				}
			}
		}

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

C++: #include <bits/stdc++.h>

using namespace std;

int dp[1024];

int main(){
    int W, N;
    int n = 0;
    while(scanf("%d", &W), W){
        n++;
        scanf("%d", &N);

        int v, w;
        for (int i = 0; i < N; i++){
            scanf("%d,%d", &v, &w);

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

        int pos = 0;
        for (int i = 1; i <= W; i++){
            if (dp[pos] < dp[i]){
                pos = i;
            }
        }

        printf("Case %d:\n", n);
        printf("%d\n", dp[pos]);
        printf("%d\n", pos);
    }

    return (0);
}