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 <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0,  1, -1,  1, -1 };
static const int dy[] = { 0, -1, 0, 1 , 1,  1, -1, -1 };

int can_take, n, cnt = 0;
PII dp[ 1001 ][ 1001 ];
vector<PII> Item;
PII ans;

PII solve( int w, int now ) {
	if ( dp[ now ][ w ].first != -1 ) {
		return dp[ now ][ w ];
	}
	if ( now == n || w == can_take ) {
		return PII( 0, w );
	}
	PII res = PII( 0, 0 );
	if ( w + Item[ now ].second <= can_take ) {
		res = solve( w + Item[ now ].second, now + 1 );
		res.first += Item[ now ].first;		
	}

	PII d = solve( w, now + 1 );
	if ( res.first < d.first || ( res.first == d.first && res.second > d.second ) ) {
		res = d;
	}
	return dp[ now ][ w ] = res;
}

int main() {
	while ( ~scanf( "%d", &can_take ) && can_take ) {
		printf( "Case %d:\n", ++cnt );
		scanf( "%d", &n );
		Item.resize( n );
		for ( int i = 0; i < n; i++ ) {
			scanf( "%d,%d", &Item[ i ].first, &Item[ i ].second );
		}
		for ( int i = 0; i < 1001; i++ ) {
			for ( int j = 0; j < 1001; j++ ) {
				dp[ i ][ j ] = PII( -1, -1 );
			}
		}
		ans = solve( 0, 0 );
		printf( "%d\n", ans.first );
		printf( "%d\n", ans.second );
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <vector>
#include <string>
#include <cmath>
#include <map> 
using namespace std;
 
const int MAX= 10000100;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
#define MAX_V 1000
#define MAX_W 10000
int W,N;
int dp[MAX_V+1][MAX_W+1];
int w[MAX_W+1];
int v[MAX_V+1];
void solve(){
  int cnt = 0;
  vector<int> data(5,0);
  for(int i = N - 1 ; i >= 0 ; i --){
    
    for(int j = 0 ; j <= W ; j ++){
    data[i]++;
      if(j<w[i]){
	data[i]--;
	dp[i][j] = dp[i+1][j];
      }else {
	dp[i][j] = max(dp[i+1][j] , dp[i+1][j-w[i]] + v[i]);

	if(dp[i+1][j] < dp[i+1][j-w[i]] + v[i]){
	  data[i]--;
	}
      }
    }
  }
  rep(i,5){
    if(data[i]==0){
      cnt+= w[i];
    }
  }
  printf("%d\n",dp[0][W]);
  cout<<cnt<<endl;
}

int main(void){
  int cnt = 0;
  while(cin>>W,W){
    cout<<"Case "<<++cnt<<":"<<endl;
    cin>>N;
    for(int i = 0 ; i < N ; i ++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    solve();
    
  }
  
}

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

int W,N;
int dp[1001][1001]; // 宝の数×重さ
struct Obj {
	int price;
	int weight;
} obj[1001];

int main(void){
	while (cin >> W, W) {
		// input
		cin >> N;
		char buf;
		for (int i = 0; i < N; i++) {
			cin >> obj[i].price >> buf >> obj[i].weight;
		}
		
		// init
		for (int i = 0; i < N; i++) {
			for (int j = 0; j <= W; j++) {
				dp[i][j] = 0;
			}
		}
		dp[0][obj[0].weight] = obj[0].price;

		// calc
		int wei;
		for (int i = 0; i < N - 1; i++) {
			for (int j = 0; j <= W; j++) {
				if (j != 0 && dp[i][j] == 0) {
					continue;
				}
				// 下
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
				// 右下
				if (wei = j + obj[i + 1].weight, wei <= W) {
					dp[i + 1][wei] = max(dp[i + 1][wei], dp[i][j] + obj[i + 1].price);
				}
			}
		}
		
		cout << endl;
		struct Obj res = {0, 0};
		for (int i = 0; i <= W; i++) {
			for(int j = 0; j < N; j++) {
				if (res.price < dp[j][i]) {
					res.price = dp[j][i];
					res.weight = i;
				}
			}
		}
		cout << endl;

		// output
		cout << res.price << endl << res.weight << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define SIZE 1001
#define MAX_W 1001

int main(void) {
    int V[SIZE][MAX_W] = {};
    int v[SIZE];
    int w[SIZE];
    int W, N;
    int ans_v, ans_w;
    int n = 1;
    
    while(true) {
        cin >> W;
        if(W == 0)   break;
        
        cin >> N;
        for(int i = 0; i < N; ++i) {
            scanf("%d,%d", &v[i], &w[i]);
        }
        
        for(int i = 1; i <= N; ++i) {
            for(int j = 1; j <= W; ++j) {
                if(w[i - 1] > j) {
                    V[i][j] = V[i - 1][j];
                } else {
                    V[i][j] = max(V[i - 1][j], V[i - 1][j - w[i - 1]] + v[i - 1]);
                }
            }
        }
        
        ans_w = W;
        while(V[N][ans_w] == V[N][W]) {
            --ans_w;
        }
        ++ans_w;
        ans_v = V[N][ans_w];
        
        printf("Case %d:\n", n);
        cout << ans_v << endl << ans_w << endl;
        
        ++n;
    }
    
    return 0;
}

C++: #include <iostream>
#define MAX_N 1000
#define MAX_W 10000

using namespace std;

main(){
  int W, N;
  int v[MAX_N], w[MAX_N];
  int dp[MAX_N+1][MAX_W+1];

  for(int count=1;;count++){
    cin >> W;
    if(W==0) break;

    cin >> N;
    for(int i=0;i<N;i++){
      scanf("%d,%d", &v[i], &w[i]);
    }
    for(int i=0;i<=W;i++){
      dp[0][i]=0;
    }
    for(int i=0;i<N;i++){
      for(int j=0;j<=W;j++){
	if(j<w[i]){
	  dp[i+1][j]=dp[i][j];
	}else{
	  dp[i+1][j]=max(dp[i][j], dp[i][j-w[i]]+v[i]);
	}
      }
    }
    int ans;
    for(int i=0;i<=W;i++){
      if(dp[N][W]==dp[N][i]){
	ans=i;
	break;
      }
    }
    printf("Case %d:\n", count);
    cout << dp[N][W] << endl;
    cout << ans << endl;
  }
  return 0;
}