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 <bits/stdc++.h>
  
using namespace std;
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)
  
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
  
#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

#define INF 1000000000

typedef long double Real;
typedef complex<Real> P;
Real dot(P a,P b){return real(conj(a)*b);}
Real cross(P a,P b){return imag(conj(a)*b);}

int main(){
  int cnt = 1;
  while(true){
    int w;
    cin>>w;
    if(w==0)break;
    int n;
    cin>>n;
    vi dp(w+1,-INF);
    dp[0] = 0;
    REP(_,n){
      int val,wei;
      scanf("%d,%d",&val,&wei);
      for(int i=w;i>=0;--i){
        if(i-wei<0)break;
        dp[i] = max(dp[i],dp[i-wei]+val);
      }
    }
    int mxval = 0, thenwei = 0;
    REPR(i,w){
      if(dp[i]>mxval){
        mxval = dp[i];
        thenwei = i;
      }
    }
    printf("Case %d:\n",cnt);
    cout<<mxval<<endl<<thenwei<<endl;
    ++cnt;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/


void solve(int W,int N){
    vector<int> v(N),w(N);
    char c;
    rep(i,N) cin>>v[i]>>c>>w[i];

    vector<vector<int>> dp(N+10,vector<int>(W+10,0));
    for(int i=0;i<N;i++){
        // use v[i],w[i]
        for(int j=0;j<=W;j++){
            if(j+w[i]<=W){
                dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]],dp[i][j]+v[i]);
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }else{
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }
        }
    }

    for(int i=0;i<=W;i++) if(dp[N][i]==dp[N][W]){
        printf("%d\n%d\n",dp[N][W],i);
        return;
    }
    //for(int i=0;i<=N;i++){
    //    for(int j=0;j<=w;j++){
    //        printf("%2d ",dp[i][j]);
    //    }
    //    cout << endl;
    //}
    assert(false);
}

int main(){
    int w,n;
    int cnt=0;
    while(1){
        cin>>w;
        if(w==0) break;
        cin>>n;
        printf("case %d:\n",++cnt);
        solve(w,n);
    }
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cctype>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

//#define deb_table(arr, w, h) rep(w_n, w){  rep(h_n, h) {cout << arr[w][h] << " "  } cout << endl; }
#define rep(i, n) for(int i = 0; i < n; i++)

// P(価値, 重さ)
typedef pair<int ,int> P;

class data
{
    public:
        int v;
        int w;
    public:
        data(int a, int b){ v=a; w=b;}
};

int case_num=1;
/*
風呂敷の耐えられる重さW(1000 以下)
お宝の数 N
お宝 1 の価値(整数), お宝 1 の重さ( ＜ W:整数)
お宝 2 の価値(整数), お宝 2 の重さ( ＜ W:整数)
.
.
.
お宝 N の価値(整数), お宝 N の重さ( ＜ W:整数)
*/

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

int solve()
{
    int w,n;
    //vector<P> v;
    vector<data> v;
    cin >> w >> n;
    if(w == 0) { return -1; }
    cout << "Case " << case_num << ":" << endl;
    int value, weight;
    //dp[重さ][考えたインデックス]
    //int dp[n+1][w+1];
    //dp[考えたインデックス][重さ]
    //インデックスは０から数えてnまで　=> [n+1]
    //重さは０から数えてwまで　=> [w+1]
    int dp[n+1][w+1];
    rep(i, n)
    {
        //カンマ区切りでの入力を受け付ける様に変更
        scanf("%d,%d" ,&value, &weight);
        data tmp = data(value, weight);
        v.push_back( tmp );
        //cout << "v == " << v[i].v <<endl;
    }

    //dp[何番目まで考えたか][重さ] = 最大値
    //dp init
    //rep(i,n+1)rep(j,w+1) dp[i][j] = 0;  
    rep(i,n+1)rep(j,w+1) dp[i][j] = 0;  


    rep(i,n+1)
    {
        rep(j,w+1)
        {
            if( i == 0) {continue;}
            //枠の中に入っていたら
            /*
            if(0 <= i-1 && i-1 <= n && 0 <= j-v[i].w && j-v[i].w <= w)
            {
            ////////////////////////////////////////////////////////////////////////////////////////////////////
                //cout << "dp = max(" << dp[i - 1][j] << "or" << dp[i-1][j - v[i].w] + v[i-1].v << endl;
                //dp[i][j] = max( dp[i - 1][j] , dp[i-1][ j - v[i].w ] + v[i].v );
                dp[i+1][j] = max (dp[i+1][j] , dp[i][j]);
                dp[i+1][j + v[i+1].v] = max (dp[i+1][j] , dp[i][j]);
            }
            */

            if(1 <= i && i < n && 0 <= j && j  <= w )
            {
            ////////////////////////////////////////////////////////////////////////////////////////////////////
                //cout << " (i , j) " << i << ", " << j <<endl;
                dp[i+1][j] = max (dp[i+1][j] , dp[i][j]);
                //cout << "max " <<  dp[i+1][j] << "or " << dp[i][j] << endl;
                if ( j+v[i+1].w <= w )
                {
                    //cout << "max " <<  dp[i+1][j+v[i+1].w] << "or " << dp[i][j] + v[i+1].v << endl;
                    dp[i+1][j + v[i+1].w] = max(dp[i+1][j + v[i+1].w] , dp[i][j] + v[i+1].v);
                }
            }
        }
    }
    /*
    rep(i,n+1){
        cout << "[";
        rep(j,w+1){
            cout << dp[i][j] << " ";
        }
        cout << "]" << endl;
    }
    */
    int index;
    //int max_weight;
    rep(j,w){
         if(dp[n][j] < dp[n][j+1])
         {
             index = j+1;
         }
    }
    cout << dp[n][w] << endl;
    cout << index << endl;
    return 0;
}

int main()
{
    while (1)
    {
        if(solve() == -1){ break;};
        case_num++;
    }
}