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 <bits/stdc++.h>

using namespace std;

const int MAX_N = 1000;
const int MAX_W = 1000;
int dp[MAX_N + 1][MAX_W + 1];

int main() {
    int id = 1;
    int W, N;
    while (scanf("%d", &W), W) {
        cin >> N;

        int w[MAX_N + 1], v[MAX_N + 1];
        for (int i = 0; i < N; i++) {
            scanf("%d,%d", &v[i], &w[i]);
        }

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

        for (int i = N - 1; i >= 0; i--) {
            for (int j = 0; j <= W; j++) {
                if (j < w[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]);
                }
            }
        }

        int max_value = 0, max_weight;
        for (int i = 0; i <= W; i++) {
            if (dp[0][i] <= max_value) continue;
            max_value = dp[0][i];
            max_weight = i;
        }

        printf("Case %d:\n", id++);
        printf("%d\n%d\n", max_value, max_weight);
    }

    return 0;
}

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

int main(void){
  int n,mw,cnt = 1;
  while(cin>>mw,mw!=0){
    cin>>n;
    printf("Case %d:\n",cnt++);
  int w[100],v[100];
  int dp[100][10000];
  int dp1[100][10000];
  for(int i = 0 ; i < n ; i ++)
    scanf("%d,%d",&v[i],&w[i]);
  for(int j = w[0] ; j < mw + 1 ; j ++)
    dp[0][j] = v[0],dp1[0][j] = w[0];
  for(int i = 1 ; i < n ; i ++){
    for(int j = 0 ; j < w[i] ; j ++){
      dp[i][j] = dp[i-1][j];
      dp1[i][j] = dp1[i-1][j];
    }
    for(int j = w[i] ; j < mw + 1 ; j ++){
      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]){
	// update
	dp1[i][j] = dp1[i-1][j-w[i]] + w[i];
      }else{
	dp1[i][j] = dp1[i-1][j];
      }
    }
  }
  int res = -1,resw = -1;
  for(int i = 0 ; i < mw+1 ; i ++){
    if(res < dp[n-1][i]){
      res = dp[n-1][i];
      resw = dp1[n-1][i];
    }
  }
  /*
  for(int i = 0 ; i < n  ; i ++){
    for(int j = 0 ; j < mw + 1; j ++)
      cout<<dp1[i][j]<<" ";
    cout<<endl;
  }
  //*/
  cout<<res<<endl<<resw<<endl;
  }
}

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){
    if( W == 0 ) break;
    cin >> N;
    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]);
        dp[i+1][j] = max(dp[i][j], dp[i+1][j]);
      }
    }

    int ansi = 0;
    int ans = 0;
    for(int i=0; i<=W; i++){
      if( ans < dp[N][i] ){
        ans = dp[N][i];
        ansi = i;
      }
    }

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

    l++;
  }




  return 0;
}

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

typedef pair<int, int> Pair;

int w;
int n;
int vs[1001];
int ws[1001];
int dp[1001][1001];

int main() {
  for (int Case=1; ; Case++) {
    scanf("%d", &w);
    if (w == 0) return 0;

    scanf("%d", &n);
    for (int i=0; i<n; i++) {
      scanf("%d,%d", &vs[i], &ws[i]);
    }

    for (int j=0; j<=w; j++) dp[0][j] = 0;
  
    for (int i=0; i<n; i++) {
      for (int j=0; j<=w; j++) {
        dp[i+1][j] = dp[i][j];
      }
  
      for (int j=0; j<=w-ws[i]; j++) {
        dp[i+1][j+ws[i]] = max(dp[i+1][j+ws[i]], dp[i][j] + vs[i]);
      }
    }
  
    Pair ans(0, 0);
    for (int j=0; j<=w; j++) {
      ans = max(ans, Pair(dp[n][j], j));
    }
  
    printf("Case %d:\n%d\n%d\n", Case, ans.first, ans.second);
  }
}

C++: #include <stdio.h>
#include <string.h>
#define max(a,b) ((a)>(b)?(a):(b))

int W,n;
int s[1000],g[1000],dp[1001];
int i,j,k,c;
int p,q;
main(){
    for(;scanf("%d%d",&W,&n),W;){
        for(i=0;i<n;i++) scanf("%d,%d",s+i,g+i);
        memset(dp,-1,sizeof(dp));
        dp[0]=0;

        for(j=0;j<n;j++){
            for(i=W-g[j];i>=0;i--){
                if(dp[i]>=0) dp[i+g[j]] = max(dp[i+g[j]],dp[i]+s[j]);
            }
        }

        p=q=0;
        for(i=0;i<=W;i++){
            if(dp[i]>p){
                p=dp[i];
                q=i;
            }
        }

        printf("Case %d:\n%d\n%d\n",++c,p,q);
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <string.h>

#define loop(lp) for(int lp2 = 0; lp2 < lp; lp2 ++)
#define ascent(i, a, b) for(int i = a; i < b; i ++)

#define pb push_back

using namespace std;

int dp[1010][10010];

int main() {
  int w_max, count = 0;
  while(cin >> w_max, w_max) {
    count ++;
    int n;
    cin >> n;
    
    memset(dp, 0, sizeof(dp));

    int v, w;
    pair<int, int> w_sum;
    w_sum = make_pair(0, 0);

    ascent(i, 1, n+1) {
      scanf("%d,%d", &v, &w);

      ascent(j, 1, w_max+1) {
	if(w <= j) {
	  if(dp[i-1][j] < dp[i-1][j-w] + v) {
	    dp[i][j] = dp[i-1][j-w] + v;

	    if(w_sum.first < dp[i][j] || w_sum.first == dp[i][j] && j < w_sum.second) {
	      w_sum = make_pair(dp[i][j], j); 

	      //cout << "i = " << i << " " << "wsum = " << w_sum.second << endl;
	    }
	  }
	  else {
	    dp[i][j] = dp[i-1][j];
	  }
	}
	else {
	  dp[i][j] = dp[i-1][j];
	}
      }

    }
    /*   
    ascent(i, 0, n+1) {
      ascent(j, 0, w_max+1) {
	printf("%3d ", dp[i][j]);
      }
      puts("");
    }
    */
    cout << "Case " << count << ":" << endl;
    cout << dp[n][w_max] << endl;
    cout << w_sum.second << endl;
    
  }
  return 0;
}

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

int main(){
  int n,weight;
  int v[1000],w[1000];
  int dp[1001];
  int m,mw;
  int cnt = 0;

  while(1){
    cnt++;
    cin >> weight;
    if(!weight)break;

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


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

    m = dp[0];
    mw = 0;
    for(int i=1;i<=weight;i++){
      if(m < dp[i]){
	m = dp[i];
	mw = i;
      }
    }
    cout << "Case " << cnt << ":\n";
    cout << m  << endl;
    cout << mw << endl;
  } 
}