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

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	int cost[maxw + 1];
	int best[maxw + 1];
	for(int i = 0; i <= maxw; i++){
		cost[i] = 0;		
		best[i] = 0;
	}
	while(cin >> w){
		if(w == 0) break;
		int n;
		int values[maxw + 1];
		int weights[maxw + 1];
		cin >> n;
		for(int i = 0; i < n; i++){
			char c;
			cin >> values[i] >> c >> weights[i];
		}
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= weights[j])
					if(cost[k] < cost[k - weights[j]] + values[j])
					{
						cost[k] = cost[k - weights[j]] + values[j];
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;w>0; w--) if(cost[w] != cost[w-1]) break;
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - weights[best[l]] >= 0;){
			if(l - weights[best[l]] >= 0){
				total += weights[best[l]];
				l -= weights[best[l]];
			}
		}
		cout << total << endl;
		for(int i = 0; i <= w; i++){
			cost[i] = 0;
			best[i] = 0;
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

int W, N, value, weight, num = 1;
int v[1000], w[1000];
int dp[1000][1000]; //[n][w]
	
int main(){
	while(true){
		scanf("%d", &W);
		if(W == 0) break;
		scanf("%d", &N);
		for(int i = 0; i < N; i++){
			scanf("%d,%d", &v[i], & w[i]);
		}
		for(int i = 0; i < 1000; i++){
			for(int j = 0; j < 1000; j++){
				dp[i][j] = 0;
			}
		}
//		cout << "aa" <<endl;
		for(int i = N - 1; i >= 0; i--){
			for(int j = 0; j < W; j++){
				if(j < w[i]) dp[i][j] = dp[i + 1][j];
				else dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
			}
		}
		/*for(int i = 0; i < N; i++){
				cout << v[i] << " " << w[i] << endl;
		}
		for(int i = 0; i <= N; i++){
			for(int j = 0; j < 109; j++){
				cout << dp[i][j] << " " ;
			}
			cout << endl;
		}*/
		value = 0;
		weight = 0;
		for(int j = 0; j < W; j++){
			if(value < dp[0][j]){
				value = dp[0][j];
				weight = j;
			}
		}
		cout << "Case " << num << ":" << endl;
		cout << value << endl << weight << endl;
		num++;
	}
	return 0;
}

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;
    REP(i,N) {
        int dp[1001] = {0}; // dp[i]: maxvalue under weight i
        int v, w;
        scanf("%d,%d", &v, &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) {
        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++: #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll dp[2][1005];

int main() {
	int W, N, wei[1005], cost[1005],t,s,r=0;
	while (cin >> W >> N, W) {
		r++;
		t = N  % 2;
		fill(dp[0], dp[0] + N, 0);
		fill(dp[1], dp[1] + N, 0);
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &wei[i], &cost[i]);
		}
		for (int i = 0; i < N; i++) {
			for (int w = 0; w <= W; w++) {

				if (w - wei[i] >= 0) dp[(i + 1) % 2][w] = max(dp[i][w], dp[i][w - wei[i]] + cost[i]);
				
			}
		}
		for (int i = 0; dp[t][W - i]==dp[t][W]; i++) {
			s = i;
		}
		cout << "Case " << r << ":" << endl;
		cout << dp[t][W] << endl;
		cout << W-s << endl;
	}
}

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

std::pair<int,int> dp[1000][10000];
int W;
int n;
int v[1000], w[1000];
int now = 1;

int main()
{
	while( 1 ){
		std::cin >> W;
		if( !W ) break;
		std::cin >> n;
		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].first = std::max( dp[i][j].first, dp[i][j-w[i]].first + v[i] );
					
					if( dp[i][j].first > dp[i][j-w[i]].first + v[i] ){
						dp[i+1][j].second = dp[i][j].second;
					}
					else{
					     	dp[i+1][j].second = dp[i][j-w[i]].second + w[i];
					}
				}
			}
		}
		
		std::cout << "Case " << now << ":" << std::endl;
		std::cout << dp[n][W].first << std::endl;
		std::cout << dp[n][W].second << std::endl;
		
		now++;	
	}
	

 	return 0;
}

C++: #include<iostream>
#include<string>
int dp[1000][1000];
using namespace std;
int main(){
	int t,n;
	int w[2000],v[2000];
	int k=0;
	char z;
	while(true){
	cin>>t;
	if(t==0)
		break;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<t;j++){
			dp[i][j]=-1;
		}
	}
	for(int i=0;i<n;i++){
		cin>>v[i]>>z>>w[i];
		dp[i][0]=0;
	}
	dp[0][w[0]]=v[0];
	for(int i=1;i<n;i++){
		for(int j=1;j<=t;j++){
			if(j-w[i]<0){
				if(dp[i-1][j]==-1)
					dp[i][j]=-1;
				if(dp[i-1][j]!=-1)
					dp[i][j]=dp[i-1][j];
			}
			if(j-w[i]>=0){
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]==-1)
					dp[i][j]=-1;
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]!=-1)
					dp[i][j]=dp[i-1][j-w[i]]+v[i];
				if(dp[i-1][j]!=-1&&dp[i-1][j-w[i]]==-1)
					dp[i][j]=dp[i-1][j];
				if(dp[i-1][j]!=-1&&dp[i-1][j-w[i]]!=-1){
					if(dp[i-1][j]>=dp[i-1][j-w[i]]+v[i])
						dp[i][j]=dp[i-1][j];
					if(dp[i-1][j]<dp[i-1][j-w[i]]+v[i])
						dp[i][j]=dp[i-1][j-w[i]]+v[i];
				}
			}
		}
	}
	int c=0;
	for(int j=0;j<t;j++){
		if(dp[n-1][j]>c)
			c=dp[n-1][j];
	}
	k++;
	for(int j=0;j<t;j++){
		if(c==dp[n-1][j]){
			cout<<"Case "<<k<<":"<<endl;
			cout<<c<<endl;
			cout<<j<<endl;
		}
	}
	}
	return 0;
}