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 <string>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define rep(i,b) for(auto i=0;i<(b);i++)
#define pb push_back
#define sz(c) int((c).size())

bool piicomp(const pii& a, const pii& b)
{
  if (a.first < b.first)
    return true;
  else if (a.first > b.first)
    return false;
  else {
    if (a.second > b.second)
      return true;
    else
      return false;
  }
}

typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
const int maxitem = 1005;
vvpii dp(maxitem, vpii(maxitem, pii(0,0)));

pii rec(int capacity, int i, vi& value, vi& weight)
{
  if (i >= sz(value))
    return pii(0,0);
  if (dp[capacity][i].first != -1)
    return dp[capacity][i];
  if (weight[i] > capacity)
    return dp[capacity][i] = rec(capacity, i+1, value, weight);

  pii tmp = rec(capacity-weight[i], i+1, value, weight);
  return dp[capacity][i] = max(pii(tmp.first+value[i], tmp.second+weight[i]),
                               rec(capacity, i+1, value, weight),
                               piicomp);
}

void thief(int capacity, vi& value, vi& weight)
{
  for (auto& i : dp) {
    for (auto& j : i) {
      j = pii(-1,-1);
    }
  }
  pii res = rec(capacity, 0, value, weight);
  cout << res.first << endl;
  cout << res.second << endl;
}

int main(int argc, char **argv)
{
  int casenr = 0;
  for (;;) {
    int W, N;
    cin >> W;
    if (!W)
      break;
    cin >> N;
    vi value(N), weight(N);
    rep (i, N) {
      char c;
      cin >> value[i] >> c >> weight[i];
    }
    cout << "Case " << ++casenr << ":" << endl;
    thief(W, value, weight);
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define MAX_N 1000
#define MAX_W 1000


int main()
{	
	int cnt = 0;
	int W, N;
	while( cin>>W && W && cin>>N){
		cnt++;
		int dp[MAX_N+1][MAX_W+1];
		for(int i=0;i<N;i++){
			int v,w;
			scanf("%d,%d",&v,&w);
			for(int j=0;j<W+1;j++){
				if(j-w>=0){
					dp[i+1][j] = max( dp[i][j], dp[i][j-w] + v);
				}else{
					dp[i+1][j] = dp[i][j];
				}
			}
		}
		cout<<"Case"<<' '<<cnt<<":"<<endl;
		for(int j=0;j<W+1;j++){
			if( dp[N][j] == dp[N][W] ){
				cout<< dp[N][j] << endl << j << endl;
				// そこからは全て一致するのでbreak
				break;
			}
		}
	}
	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;
    char c;
    int dp[1001] = {0}; // dp[i]: maxvalue under weight i
    REP(i,N) {
        int v, w;
        cin >> v >> c >> 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+1) {
        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 <iostream>
#include <cstring>
#include <vector>
using namespace std;

int main()
{
	int w, n, i, j, cs, val[1001], wg[1001], dp[1001], inserted[1001];
	cs = 1;
	
	while( cin >> w , w)
	{
		memset( dp, 0, sizeof(dp));
		memset( val, 0, sizeof(val));
		memset( wg, 0, sizeof(wg));
		memset( inserted, 0, sizeof(inserted));
		
		inserted[0] = 1;
		
		cin >> n;
		
		for( i = 1; i <= n; i++)
		{	
			char c;
			cin >> val[i] >> c >> wg[i];
		}
		
		
		for( j = 1; j <= n; j++)
			for( i = 0; i <= w; i++)
				if( inserted[i] && i+wg[j] <= w && dp[i+wg[j]] < dp[i] + val[j] )
				{
						dp[i+wg[j]] = max( dp[i] + val[j], dp[i+wg[j]] );
						
						inserted[i+wg[j]] = 1;
						
						i = (i+wg[j]) + 1;
				}
		
		for( i = w; i > 0; i--)
		{
			if( dp[i] > dp[i-1] )
			{
				cout << "Case " << cs << ":" << endl;
				cout << dp[i] << endl;
				cout << i << endl;
				cs++;
				break;
			}
		}
		
	}
	
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct Item {
  int value;
  int weight;
};

int main () {
  for( int case_n = 1; ; case_n++  ) {
    int w, n;
    vector< Item >items;
    vector< vector<int> >dp;
    vector< vector<int> >dpw;
    cin >> w;
    if( !w ) { return EXIT_SUCCESS; }
    cin >> n;
    
    for( int i = 0; i < n; i++ ) {
      string line;
      cin >> line;
      replace( line.begin(), line.end(), ',', ' ' );
      istringstream iss( line );
      
      Item item;
      iss >> item.value >> item.weight;
      items.push_back( item );
    }
    for( int i = 0; i <= n; i++ ) {
      vector<int> dpline;
      vector<int> dpwline;
      for( int j = 0; j <= w; j++ ) {
        int opt, optw;
        if( i == 0 || j == 0 ) {
          opt = 0;
          optw = 0;
        } else if( j < items[ i - 1 ].weight ) {
          opt = dp[ i - 1 ][ j ];
          optw = dpw[ i - 1 ][ j ];
        } else { 
          if( 
            dp[ i - 1 ][ j ] <
            dp[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].value
          ) {
            opt  =  dp[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].value;
            optw = dpw[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].weight;
          } else if(
            dp[ i - 1 ][ j ] ==
            dp[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].value
          ) {
            opt  =  dp[ i - 1 ][ j - items[ i - 1 ].weight ] + items[ i - 1 ].value;
            optw = min(
              dpw[ i - 1 ][ j ],
              dpw[ i - 1 ][ j  - items[ i - 1 ].weight ] + items[ i - 1 ].weight
            );
          } else {
            opt = dp[ i - 1 ][ j ];
            optw = dpw[ i - 1 ][ j ];
          }
        }
        dpline.push_back( opt );
        dpwline.push_back( optw );
      }
      dp.push_back( dpline );
      dpw.push_back( dpwline );
    }
    cout << "Case " << case_n << ":"<<endl;
    cout << dp[ n ][ w ] << endl;
    cout << dpw[ n ][ w ] << endl;
  }
  return 0;
}