Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	Case := 1
	for {
		scanner.Scan()
		W, _ := strconv.Atoi(scanner.Text())
		if W == 0 {
			break
		}
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		w := make([]int, n+1)
		v := make([]int, n+1)
		for i := 1; i <= n; i++ {
			scanner.Scan()
			info := strings.Split(scanner.Text(), ",")
			v[i], _ = strconv.Atoi(info[0])
			w[i], _ = strconv.Atoi(info[1])
		}
		// fmt.Println(v)
		// fmt.Println(w)
		dp := make([][]int, n+1)
		for i := range dp {
			dp[i] = make([]int, W+1)
		}
		maxV := -(1 << 21)
		minW := 1 << 21
		for i := 1; i <= n; i++ {
			for j := 1; j <= W; j++ {
				// fmt.Println(dp)
				if j-w[i] >= 0 {
					dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]]+v[i])
					if maxV < dp[i][j] {
						maxV = dp[i][j]
						minW = j
					} else if maxV == dp[i][j] {
						minW = min(minW, j)
					}
				} else {
					dp[i][j] = dp[i-1][j]
				}
			}
		}
		fmt.Printf("Case %d:\n", Case)
		Case += 1
		fmt.Println(maxV)
		fmt.Println(minW)
	}
}
func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

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;

}