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 <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<cstdio>
#include<iostream>
#include<array>
using namespace std;
array<pair<int,int>,1001> pairs;
array<array<pair<int,int>,10001>,1001> m;
int main(){
  for(int caseNo=0;;caseNo++){
    int w; scanf("%d",&w);
    if(w==0) break;
    int n; scanf("%d",&n);
    for(int i=1;i<=n;i++){
      scanf("%d,%d",&pairs[i].first,&pairs[i].second);
    }
    for(int j=0;j<=w;j++){
      m[0][j].first=0;
      m[0][j].second=0;
    }
    for(int i=1;i<=n;i++){
      for(int j=0;j<=w;j++){
	if(pairs[i].second<=j){
	  if(m[i-1][j].first>m[i-1][j-pairs[i].second].first+pairs[i].first){
	    m[i][j].first=m[i-1][j].first;
	    m[i][j].second=m[i-1][j].second;
	  }else{
	    m[i][j].first=m[i-1][j-pairs[i].second].first+pairs[i].first;
	    m[i][j].second=m[i-1][j-pairs[i].second].second+pairs[i].second;
	  }
	}else{
	  m[i][j].first=m[i-1][j].first;
	  m[i][j].second=m[i-1][j].second;
	}
      }
    }
    printf("Case %d:\n%d\n%d\n",caseNo+1,m[n][w].first,m[n][w].second);
  }
}

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<cstdio>
using namespace std;
const int INF=-1;
int main(){
    int W,cnt=0;
    while(cin>>W,W){
        cnt++;
        int dp[1001],n;
        cin>>n;

        for(int i=0;i<1001;i++)dp[i]=INF;

        dp[0]=0;
        for(int i=0;i<n;i++){
            int v,w;
            scanf("%d,%d",&v,&w);
            for(int j=W;j>=0;j--){
                if(dp[j]==INF)continue;
                if(j+w>W)continue;
                dp[j+w]=max(dp[j+w],dp[j]+v);

            }
        }
        pair<int,int>P;
        P.first=0;
        for(int i=0;i<W;i++){
            if(dp[i]>P.first){
                P.first=dp[i];
                P.second=i;
            }
        }
        cout<<"Case "<<cnt<<":"<<endl;
        cout<<P.first<<endl<<P.second<<endl;
    }
    return 0;

}