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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

int main()
{
    int W;
	for(int caseNum = 1; cin >> W && W; caseNum++)
	{
		int N;
		cin >> N;
		
		vint prv(W + 1, 0);
		
		
		rep(i, N)
		{
			vint nxt(W + 1, 0);
			int val, wei;
			char k;
			
			cin >> val >> k >> wei;
			
			rep(j, W - wei + 1)
			{
				if(j == 0 || prv[j] != 0)
				{
					chmax(nxt[j + wei], prv[j] + val);
				}
			}
			
			prv = nxt;
		}
		
		
		int val = 0, wei;
		rep(i, W + 1)
		{
			if(prv[i] != 0)
			{
				wei = i;
				val = prv[i];
			}
		}
		
		
		cout << "Case " << caseNum << ":\n" << val << endl << wei << endl;
	}
}

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

using namespace std;

int main(void){
  int W,n,v[1000],w[1000],dp[1000][1000],t=1;

  while(cin >> W,W){
    cin >> n;

    for(int i=0;i<n;i++)
    scanf("%d,%d",&v[i],&w[i]);

    for(int i=0;i<1000;i++)
      for(int j=0;j<1000;j++)
	dp[i][j]=-1;
    
    for(int i=0;i<1000;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]);

    cout << "Case " << t << ":" << endl;
    
    for(int i=W;i>=0;i--){
      if(dp[n][i] != dp[n][W]){
	cout << dp[n][W] << endl << i+1 << endl;
	break;
      }
    }
      t++;
  }

  return 0;
}

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

typedef struct{
  int g;
  int ye;
}SET;

int main(){
  
  int n,w;
  int cnt=1;
  while(1){
    
    scanf("%d",&w);
    if(w==0)break;
    scanf("%d",&n);
    SET data[1111]={0}; 
    int memo[11111][11111]={{0}};
    int max_y=0;
    int max_g=1111;
    
    for(int i=0;i<n;i++){
      scanf("%d,%d",&data[i].ye,&data[i].g);
    }
    memo[0][data[0].g]=data[0].ye;
    for(int i=1;i<n;i++){
      if(data[i].ye>memo[i-1][data[i].g]){
	memo[i][data[i].g]=data[i].ye;
      }
      for(int j=1;j<=w;j++){
	if(memo[i-1][j]>0){
	  if(memo[i-1][j]>memo[i][j]){
	    memo[i][j]=memo[i-1][j];
	  }
	  if(j+data[i].g<=w){
	    memo[i][j+data[i].g]=memo[i-1][j]+data[i].ye;
	  }
	}
	if(max_y<=memo[i][j]){
	  if(max_y==memo[i][j]){
	    if(max_g>j){
	      max_g=j;
	    }
	  }
	  else{
	    max_g=j;
	    max_y=memo[i][j];
	  }
	}
      }    
    }
    printf("Case %d:\n%d\n%d\n",cnt,max_y,max_g);
    cnt++;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
// #define first first
// #define second se
using namespace std;
typedef pair<int, int> P;
typedef vector<int> vi;

int dp[1100];

signed main(){
    int W;
    int cnt = 0;
    while(cin >> W, W){
        int n;
        cin >> n;
        cnt++;
        vector<int> v(n), w(n);
        rep(i, 0, n){
            char c;
            cin >> v[i] >> c >> w[i];
        }
        rep(i, 0, 1010){
            dp[i] = 0;
        }
        rep(i, 0, n){
            repb(j, W, 0){
                if(j + w[i] <= W){
                    dp[j + w[i]] = max(dp[j] + v[i], dp[j + w[i]]);
                }
            }
        }
        int ans = -1, wt;
        rep(i, 0, W + 1){
            if(dp[i] > ans){
                ans = dp[i];
                wt = i;
            }
        }
        cout << "Case " << cnt << ":" << endl;
        o(ans);
        o(wt);
    }
}

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

int sum[100+1];
int temp[100+1];
int size;

void update(int score, int weight) {
	for (int i = weight; i <= size; i++) {
		int a = sum[i-weight] + score;
		temp[i] = a > sum[i] ? a : sum[i];
	}
	for (int i = weight; i <= size; i++)
		sum[i] = temp[i];
}

int main() {
	int k = 0, n;
	while (cin >> size, size) {
		k++;
		cin >> n;
		for (int i = 0; i <= size; i++)
			sum[i] = temp[i] = 0;
		
		for (int i = 0; i < n; i++) {
			int a, b; scanf("%d,%d", &a, &b);
			update(a, b);
		}

		int w = size;
		while (sum[w-1] == sum[size] && w > 0) w--;
		cout << "Case " << k << ":" << endl;
		cout << sum[size] << endl;
		cout << w << endl;
	}
	return 0;
}

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

int main(void) 
{
  int dp[1001][1001];  // [takara][weight]
  int wei[1001];       // [takara]
  int val[1001];       // [takara]
  int i, w;
  int n, tw;
  int datcnt;
  char c;

  cin >> tw;
  for(datcnt = 1; tw != 0; datcnt++){
    cin >> n;
    for(i = 1; i <= n; i++){
      cin >> val[i];
      cin >> c;
      cin >> wei[i]; 
    }

    // initArrayDP
    for(i = 0; i <= n; i++) {
      for(w = 0; w <= tw; w++) {
	dp[i][w] = 0;
      }
    }

    for(i = 1; i <= n; i++) {
      for(w = 1; w <= tw; w++) {
	int tem = w - wei[i];
	if(tem < 0) continue;

	dp[i][w] = max(dp[i-1][w], dp[i-1][tem] + val[i]);
      }
    }
    
    // [takara]の最終列から価値最大かつ、そのなかで重さ最初を探す。
    int maxval = dp[n][tw];
    int lastwei = INT_MAX;

    for(w = tw; w >= 0; w--)
      if(dp[n][w] == maxval) lastwei = min(lastwei, w);

    cout << "Case " << datcnt << ":" << endl;
    cout << dp[n][tw] << endl << lastwei << endl;
    
    cin >> tw;
  }
  return 0;
}