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 <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;
}