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;

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