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