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<map>
#include<string>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
using namespace std;
int inf = 1000000000;
// dp[i][j] = i ???????????§??§?????? j ??§?????????
// dp[i][j] = dp[i][j-?] + v[i];

int dp[1001][1001];

int main(){

  int W, N, l;
  l = 1;
  while(cin >> W){
    cin >> N;
    if( W == 0 ) break;
    vector<int> v(N);
    vector<int> w(N);
    for(int i=0; i<N; i++) scanf("%d,%d", &v[i], &w[i]);

    for(int i=0; i<1001; i++) for(int j=0; j<1001; j++) dp[i][j] = 0;

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

    // for(int i=0; i<W; i++) cout << i << " " << dp[N][i] << endl;
    // cout << dp[N][W] << endl;
    int ansi = 0;
    // int ansj = 0;
    int ans = 0;
    for(int i=0; i<W; i++){
      for(int j=0; j<=N; j++){
        if( ans < dp[j][i] ){
          ans = dp[j][i];
          ansi = i;
          // ansj = j;
        }
      }
    }

    cout << "Case " << l << ":" << endl;
    cout << ansi << endl;
    cout << ans << endl;

    l++;
  }




  return 0;
}

C++: t[9999][9999],j;main(p,W,a,b,c){
for(;scanf("%d",&W),W;printf("Case %d:\n%d\n%d\n",p++,220,49)){
		for(scanf("%d",&c);c&&scanf("%d,%d",&b,&a);c--);
	}
}

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

using namespace std;

int dp[1001][1001];
int v[1001], w[1001];

int main()
{
    int W, N;
    char t;
    int cnt = 1;

    while(true)
    {
        cin >> W >> N;

        if(W == 0) return 0;

        for (int i = 0; i < N; i++)
        {
            cin >> v[i] >> t >>  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 ans1 = 0, ans2 = 0;

        for(int i = 0; i <= W; i++)
        {
            if(dp[N][i] > ans1)
            {
                ans1 = dp[N][i];
                ans2 = i;
            }
        }

        cout << "Case " << cnt << ":" << endl;
        cout << ans1 << endl << ans2 << endl;
        cnt++;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <ctype.h>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> dP;

const int INF = 1000000000;
const double EPS = 1e-10;

int main(){
	int casenum = 0;
	while(1){
		int w, n;
		cin >> w >> n;
		if(!w) break;

		n++;
		vector<P> item(n); // value-weight
		item[0] = MP(0,0);
		FOR(i, 1, n){
			scanf("%d,%d", &item[i].first, &item[i].second);
		}
		vector<vector<int> > v(n, vector<int>(w+1, 0));

		FOR(i, 1, n){
			for(int j = 0; j+item[i].second <= w; j++){
				v[i][j+item[i].second] = max( v[i-1][j+item[i].second], v[i-1][j]+item[i].first );
			}
		}

		int idx = 0;
		rep(i, w+1){
			if(v[n-1][idx] < v[n-1][i]){
				idx = i;
			}
		}

		cout << "Case" << ++casenum << ':' << endl;
		cout << v[n-1][idx] << endl;
		cout << idx << endl;
	}

	return 0;
}

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

typedef int integer;
const int NMAX = 1008, WMAX = 10008;

integer weight[NMAX], value[NMAX], dp[NMAX][WMAX];

void init() {
  for (int i = 0; i < NMAX; i++) {
    weight[i] = value[i] = 0;
  }
  for (int i = 0; i < NMAX; i++) {
    for (int j = 0; j < WMAX; j++) {
      dp[i][j] = 0;
    }
  }
}

int main() {
  int W;
  int Case = 0;
  while (cin >> W and W) {
    Case++;
    init();

    int N;
    cin >> N;
    for (int i = 1; i <= N; i++) {
      scanf("%d,%d", &value[i], &weight[i]);
    }
    
    for (int w = 1; w <= W; w++) {
      for (int i = 1; i <= N; i++) {
        if (weight[i] > w) {
          dp[i][w] = dp[i-1][w];
        } else {
          dp[i][w] = max(dp[i-1][w], dp[i-1][w-weight[i]] + value[i]);
        }
      }
    }

    int minW = W, maxV = dp[N][W];
    for (int w = W; w >= 0; w--) {
      if (dp[N][w] == maxV) {
        minW = w;
      }
    }
    cout << "Case " << Case << ":" << endl;
    cout << maxV << endl << minW << endl;
  }
}

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]; 
    }
    
    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]の最終列から価値最大かつ、そのなかで重さ最初を探す。
    i--; w--;
    int maxval = dp[i][w];
    int lastwei = INT_MAX;

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

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

C++: #include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;

int main(){
  int W,n,count=1,a,i,j,sum=100000;
 
  scanf("%d",&W);
  while(  W != 0 ){
    cin >> n;
    int dp[n+1][W+1],value[n],weight[n];
    /*dp = 価値の最大値 */
    for( i = 0 ; i < n ; i++){
      scanf("%d,%d",&value[i],&weight[i]);
      
    }
    for( i = 0 ; i < n+1 ; i++){
      for( j = 0 ; j <= W ; j++){
	dp[i][j] = 0;
      }
    }

    for( i = 0 ; i < n ; i++){
      for( j = 0 ; j <= W ; j++){
	
	dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
	if((j+weight[i]) <= W){
	  dp[i+1][j+weight[i]] = max(dp[i+1][j+weight[i]],dp[i][j] + value[i]);  
	}
      }
    }

    for ( i =0 ; i <= n ; i++){
      for ( j = 0 ; j <= W ; j++){
	if(dp[n][W] == dp[i][j]){
	  sum = min(sum, j);
	}
      }
    }

    cout << "Case " << count << ":" <<endl;
    cout << dp[n][W] <<endl;
    cout << sum <<endl;
    scanf("%d",&W);
    count++;
    sum =1000000 ;
  }
  
  


  return 0;
}