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 <cstring>
#include <vector>
using namespace std;

int main()
{
	int w, n, i, j, cs, val[1001], wg[1001], dp[1001], inserted[1001];
	cs = 1;
	
	while( cin >> w , w)
	{
		memset( dp, 0, sizeof(dp));
		memset( val, 0, sizeof(val));
		memset( wg, 0, sizeof(wg));
		memset( inserted, 0, sizeof(inserted));
		
		inserted[0] = 1;
		
		cin >> n;
		
		for( i = 1; i <= n; i++)
		{	
			char c;
			cin >> val[i] >> c >> wg[i];
		}
		
		
		for( j = 1; j <= n; j++)
			for( i = 0; i <= w; i++)
				if( inserted[i] && i+wg[j] <= w && dp[i+wg[j]] < dp[i] + val[j] )
				{
						dp[i+wg[j]] = max( dp[i] + val[j], dp[i+wg[j]] );
						
						inserted[i+wg[j]] = 1;
						
						i = (i+wg[j]) + 1;
				}
		
		for( i = w; i > 0; i--)
		{
			if( dp[i] > dp[i-1] )
			{
				cout << "case " << cs << ":" << endl;
				cout << dp[i] << endl;
				cout << i << endl;
				cs++;
				break;
			}
		}
		
	}
	
	return 0;
}

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

using namespace std;

int value[1000], weight[1000];
int dp[1001][1001]; // dp[?¬?????????????][??????] = ?????§??????

int main() {
	int W;
	int c = 1;
	while ( cin >> W, W ) {
		int N; cin >> N;
		for (int i = 0; i < N; ++i) scanf("%d,%d", &value[i], &weight[i]);

		fill(&dp[0][0], &dp[0][0]+1001*1001, 0);

		for (int i = 0; i < N; ++i) {
			for (int w = 0; w <= W-weight[i]; ++w) {
				dp[i+1][w] = max(dp[i+1][w], dp[i][w]);
				dp[i+1][w+weight[i]] = max(dp[i+1][w+weight[i]], dp[i][w]+value[i]);
			}
		}
		int av = 0, aw = 0;
		for (int w = 0; w < W; ++w) {
			if (dp[N][w] > av) {
				av = dp[N][w];
				aw = w;
			}
		}
		printf("Case %d:\n", c++);
		printf("%d\n", av);
		printf("%d\n", aw);
	}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <random>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)

int caseId = 1;

void solve42(int W) {
    cout << "Case " << caseId << ":" << endl;
    int N;
    cin >> N;
    char c;
    int dp[1001] = {0}; // dp[i]: maxvalue under weight i
    REP(i,N) {
        int v, w;
        cin >> v >> c >> w;
        for(int j = W; j >= w; j--) {
            dp[j] = max(dp[j], dp[j-w] + v);
        }
    }
    int value = 0, weight = 0;
    REP(i,W+1) {
        if (value < dp[i]) {
            value = dp[i];
            weight = i;
        }
    }
    cout << value << endl << weight << endl;
}

int main() {
    int W;
    while (cin >> W, W) {
        solve42(W);
        caseId++;
    }
    return 0;
}

C++: #define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define M_N 1000
#define M_W 10000

typedef pair<int, int> ii; //?????????, ?????????

int W, N;
int v[M_N], w[M_N];
int dp[M_N + 1][M_W + 1][2];

//?????????j??\?????¶??????i????????\????????????????????????
ii dfs(int i, int j) {
	if (dp[i][j][0] != -1)
		return ii(
			dp[i][j][0],
			dp[i][j][1]
		);

	if (i == N) {
		//?????°??¶?????????????????¨???
		return ii(
			dp[i][j][0] = 0,
			dp[i][j][1] = 0
		);
	}
	else if (j < w[i]) {
		//i??????????????????????????¶?????§??\??????????????¨???
		return ii(
			dp[i][j][0] = dfs(i + 1, j).first,
			dp[i][j][1] = dfs(i + 1, j).second
		);
	}
	else {
		//???????????\???????????\??????????????§?????????????????????????????§??¨?????????
		int a = dfs(i + 1, j).first,
			b = dfs(i + 1, j - w[i]).first + v[i];
		if (a > b)
			return ii(
				dp[i][j][0] = a,
				dp[i][j][1] = dfs(i + 1, j).second
			);
		else
			return ii(
				dp[i][j][0] = b,
				dp[i][j][1] = dfs(i + 1, j - w[i]).second + w[i]
			);

	}
}

int main() {
	for (int h = 0; cin >> W, W; h++) {
		cin >> N;
		memset(dp, -1, sizeof(dp));
		for (int i = 0; i < N; i++) {
			char c;
			cin >> v[i] >> c >> w[i];
		}
		cout << "Case " << h << ":" << endl;
		cout << dfs(0, W).first << endl;
		cout << dfs(0, W).second << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<string.h>
using namespace std;

int main(){
    int w,n,va,we;
    int dp[1000];
    char c;
    vector<pair<int,int> >vec;
    
    while(cin>>w>>n,w){
        for(int i=0;i<n;i++){
            cin>>va>>c>>we;
            vec.push_back(make_pair(va, we));
        }
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++){
            int tv=vec[i].first, tw=vec[i].second;
            for(int j=w;j>=0;j--){
                if(tw==j)continue;
                if(tw+j<=w){
                    dp[tw+j]=max(dp[tw+j],dp[j]+tv);
                }
            }
        }
        int aw=0,av=0;
        for(int i=0;i<=w;i++) {
            if(dp[i]>av){
                aw=i;
                av=dp[i];
            }
        }
        cout<<av<<endl<<aw<<endl;
        vec.clear();
    }
}

C++: #include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>

using namespace std;
int dp[1001][1001];

void solve()
{
	int W;
	int count = 1;
	while(cin >> W, W)
	{
		int N;
		cin >> N;
		vector<int> w(N);
		vector<int> v(N);
		memset(dp, 0, sizeof(dp));
		for(int i = 0; i < N; ++i)
		{
			scanf("%d,%d", &v[i], &w[i]);
		}
		for(int i = 0; i < N; ++i)
		{
			for(int j = 0; j <= W; ++j)
			{
				if(j < w[i])
				{
					dp[i + 1][j] = dp[i][j];
				}
				else
				{
					dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
				}
			}
		}
		int pos = 0;
		for(int i = 0; i <= W; ++i)
		{
			if(dp[N][i] > dp[N][pos])
			{
				pos = i;
			}
		}
		cout << "Case " << count << ":" << endl;
		cout << dp[N][pos] << endl; 
		cout << pos << endl;
		++count;
	}
}

int main()
{
	solve();
	return(0);
}