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 <cstdio>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int mp[1001][1001];

int main(){
	int W;char c;
	for(int num = 0;cin >> W&&W;num++){
		int v[1001], w[1001], n;
		cin >> n;
		fill((int*)mp, (int*)(mp+n+1), 0);
		for(int i = 0;i < n;i++)cin >> v[i] >> c >> w[i];
		for(int i = 0;i < n;i++){
			for(int j = 0;j < W+1;j++){
				if(j < w[i])mp[i+1][j] = mp[i][j];
				else mp[i+1][j] = max(mp[i][j], mp[i][j-w[i]]+v[i]);
			}
		}
		int ans = 0;
		for(int i = 1;i < W+1;i++){
			if(mp[n][i] > mp[n][ans])ans = i;
		}
		cout << "Case " << num+1 << ":" << endl;
		cout << mp[n][ans] << endl << ans << endl;
	}
	return 0;
}
/*
1
y eazqyp pnop pngtg ye obmpngt xmybp mr lygw
*/

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
 
#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
 
using namespace std;
 
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
 
int main(void){
    for(int w, n, cnt = 0; cin >> w >> n, w;){
        vi dp(w + 1); fill(ALL(dp), -1);
        dp[0] = 0;

        char c;
        while(n --){
            int tv, tw; cin >> tv >> c >> tw;
            for(int i = w; 0 <= i; i --) if(i + tw <= w && dp[i] >= 0) dp[i + tw] = max(dp[i + tw], dp[i] + tv);
        }
        
        vi::iterator it = max_element(ALL(dp));
        cout << "Case " << ++ cnt << endl << *it << endl << it - dp.begin() << endl;
    }
 
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(void){
    for(int x=1;;x++){
        int l;
        int n;
        int v[1010]={0};
        int w[1010]={0};
        int dp[10][1010]={0};
        int we[10][1010]={0};
        cin >> l;
        if(l==0)break;
        cin >> n;
        for(int i=1;i <= n;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        for(int i=1;i <= n;i++){
            for(int j=0;j <= l;j++){
                if(j < w[i]){
                    dp[2][j] = dp[1][j];
                    we[2][j] = we[1][j];
                }
                else{
                    if(dp[1][j]>dp[1][ j-w[i] ] + v[i]){
                        dp[2][j] = dp[1][j];
                        we[2][j] = we[1][j];
                    }
                    else{
                        dp[2][j] = dp[1][ j-w[i] ] + v[i];
                        we[2][j] = we[1][ j-w[i] ]+w[i];
                    }
                }
            }
            /*for(int k=0;k<=l;k++)cout<<we[i+1][k]<<" ";
            cout<<endl;*/
            for(int k=0;k <= l;k++){
                dp[1][k] = dp[2][k];
                we[1][k] = we[2][k];
            }
        }
        /*for(int i=1;i<=n;i++){
            for(int j=0;j<=l;j++){
            cout<<dp[i][j]<<" ";
            }
            cout<<endl;
        }*/
        cout<<"Case "<<x<<":"<<endl;
        cout<<dp[2][l]<<endl;
        cout<<we[2][l]<<endl;

    }
	return 0;
}

C++: #include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>

int max(int a,int b){
	if(a>=b)return a;
	else return b;
}

int min(int a,int b){
	if(a>=b)return b;
	else return a;
}

void swap(int *a,int *b){
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void babbleSort(int *data,int n){
	for(int i=0;i<n;i++){
		for(int j=n-1;j>i;j--){
			if(data[j]>=data[j-1]){
				swap(&data[j],&data[j-1]);
			}
		}
	}
}


using namespace std;

/*
cin.ignore();
	getline(cin,dataStr);
	data = new int[n];
	stringToInteger(&dataStr,data,n);
*/

void stringToInteger(string *str,int *data,int n){
	for(int i=1;i<=n;i++){
		int spaceN = str->find(' ',0);

		data[i] = atoi(str->substr(0,spaceN).c_str());
		str->erase(0,spaceN+1);
	}
}



int dp[1010];
//dp[i] i:重さ




int main(){


	int w,n;
	while(1){
		cin>>w;
		if(w==0)break;

		cin>>n;

		for(int i=0;i<=w;i++){
			dp[i]=0;
		}
		int value,weight;
		for(int i=0;i<n;i++){
			scanf("%d,%d",&value,&weight);
			for(int j=w;j>=0;j--){
				if((j==0||dp[j]>0)&&((j+weight)<=w)){
					int tmp = dp[j]+value;
					dp[j+weight] = max(tmp,dp[j+weight]);
					cout<<j+weight<<" "<<dp[j+weight]<<endl;
				}
			}
		}

		int ans=0;
		int ansCnt=0;
		for(int i=0;i<=w;i++){
			if(ans<dp[i]){
				ans=dp[i];
				ansCnt=i;
			}
		}
		cout<<ans<<endl;
		cout<<ansCnt<<endl;
	}



	return 0;
}

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=1;i<=N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		P dp[1001][1001];
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				dp[i][j] = P(0,0);
			}
		}
		dp[0][0] = P(0,0);
		for(int i=1;i<=N;i++){
			dp[i][0] = P(0,0);
			for(int j=0;j<i;j++){
				if(dp[i-1][j].second+we[i] <= W){
					P p;
					p = P(dp[i-1][j].first+tr[i],dp[i-1][j].second+we[i]);
					if(p.first == dp[i-1][j+1].first){
						if(p.second < dp[i-1][j+1].second) dp[i][j+1] = p;
						else dp[i][j+1] = dp[i-1][j+1];
					}
					else if(p.first > dp[i-1][j+1].first){
						dp[i][j+1] = p;
					}
					else dp[i][j+1] = dp[i-1][j+1];
				}
				else dp[i][j+1] = dp[i-1][j+1];
			}
		}
		int ans1=0,ans2=0;
		for(int i=0;i<=N;i++){
			if(dp[N][i].first == ans1){
				if(dp[N][i].second < ans2) ans2 = dp[N][i].second;
			}
			else if(dp[N][i].first > ans1){
				ans1 = dp[N][i].first;
				ans2 = dp[N][i].second;
			}
		}

		cout << "Case " << cou << ":" << endl;
		cout << ans1 << endl;
		cout << ans2 << endl;
	}
}