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 <stdio.h>
#include <string.h>

struct treasure {
	int w;
	int v;
	void operator+=(treasure t){
		*this = *this + t;
	}
	treasure operator+(treasure t){
		treasure tt;
		tt.v = this->v + t.v;
		tt.w = this->w + t.w;
		return tt;
	}
};

struct treasure t[1000];
struct treasure dp[1001][1001];
int n;

treasure max(treasure a, treasure b){
	if(a.v == b.v){
		return a.w <= b.w ? a : b;
	}else{
		return a.v >= b.w ? a : b;
	}
}

treasure calc(int i, int w){
	if(dp[i][w].v >=0){ return dp[i][w]; }
	struct treasure res;
	if(i == n){
		res.v = 0;
		res.w = 0;
	}else if(w < t[i].w){
		res += calc(i+1, w);
	}else{
		res = max(calc(i+1, w), calc(i+1, w-t[i].w) + t[i]);
	}
	return dp[i][w] = res;
}

int main(void){
	int w, c=1;
	struct treasure res;
	while(true){
		scanf("%d", &w);
		if(w == 0){ break; }
		scanf("%d", &n);
		for(int i=0; i<n; ++i){
			scanf("%d,%d", &(t[i].v), &(t[i].w));
		}
		memset(dp, -1, sizeof(dp));

		res = calc(0, w);
		printf("Case %d:\n", c++);
		printf("%d\n", res.v);
		printf("%d\n", res.w);
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int main()
{
	int sz,n;
	int w,v,dp[1001];
	for(int k=1;scanf("%d%d",&sz,&n),sz;k++){
		memset(dp,0,sizeof(int)*1001);

		for(int i=0;i<n;i++){
			scanf("%d,%d",&v,&w);
			for(int j=sz;j>=0;j--){
				if( (j==0||dp[j]>0) && j+w<=sz )
					dp[j+w]=max(dp[j+w],dp[j]+v);
			}
		}

		int vmax=0,wmin=sz;
		for(int i=0;i<=sz;i++)
			if(vmax<dp[i] || (vmax==dp[i] && wmin>i))
				vmax=dp[i],wmin=i;
		printf("Case %d:\n%d\n%d\n",k,vmax,wmin);
	}

	return 0;
}

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

int item_w[1001];
int item_p[1001];
int n;
int w;
int dp[1001];

pair<int,int> check(int i,int ww,int pp)
{
  pair<int,int> tmp;
  pair<int,int> tmp2;
  if(ww>w)
    return make_pair(0,0);
  if(i>n)
    return make_pair(ww,pp);
  if(dp[ww]>pp)
    return make_pair(ww,dp[ww]);
  dp[ww]=pp;
  tmp=check(i+1,ww,pp);
  tmp2=check(i+1,ww+item_w[i],pp+item_p[i]);
  if(tmp.first>tmp2.first ||(tmp.first==tmp2.first &&tmp.second<tmp2.second))
    {
      return tmp;
    }
  else
    {
      return tmp2;
    }
}

int main()
{
  int casenum = 1;
  for(;cin>>w,w;)
    {
      cin>>n;
      memset(dp,-1,sizeof(dp));
      for(int i=0;i<n;i++)
	{
	  char tmp;
	  cin>>item_p[i]>>tmp>>item_w[i];
	}
      int wa=0;
      int ans=0;
      pair<int,int> ans2=check(0,0,0);
      for(int i=0;i<=w;i++)
	{
	  //	  cout<<"test: i,dp[i]="<<i<<","<<dp[i]<<endl;
	  if(ans<dp[i])
	    {
	      ans=dp[i];
	      wa=i;
	    }
	}
      //      cout<<"Case "<<casenum++<<":"<<endl<<ans.second<<endl<<ans.first<<endl;
      cout<<"Case "<<casenum++<<":"<<endl<<ans<<endl<<wa<<endl;
    }
}

C++: #include<bits/stdc++.h>
#define MAX_N 1001
#define MAX_W 1001
using namespace std;


int dp[MAX_N][MAX_W];
int n,W;
int w[MAX_N];
int v[MAX_N];

void solve(){
  memset(dp,0,sizeof(dp));
  cout<<"Case 1:"<<endl;
  for(int i=0;i<n;i++){
    for(int j=0;j<=W;j++){
      if(j>=w[i]){
	dp[i+1][j]=max(dp[i][j],dp[i][j-w[i]]+v[i]);
      }else{
	dp[i+1][j]=dp[i][j];
      }
    }
  }
  int tmp=0;
  int weight;
  for(int i=0;i<=W;i++){
    if(dp[n][i]>tmp){
      tmp=dp[n][i];
      weight=i;
    }
  }
cout << tmp << endl;
  cout << weight << endl;
}

int main(){
  while(cin>>W,W){
    cin>>n;
    for(int i=0;i<n;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    solve();
  }

  return 0;
}

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

using namespace std;

int dp[1001][1001] = {0};
int main() {
	int W, N;
	int t = 1;
	while ( cin >> W, W ) {
		cin >> N;
		vector<int> v(N), w(N);
		for (int i = 0; i < N; ++i) {
			scanf("%d,%d", &v[i], &w[i]);
		}
		fill(&dp[0][0], &dp[0][0]+1001*1001, 0);
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j <= W; ++j) {
				if (w[i] + j <= W) {
					dp[i+1][w[i] + j] = max(dp[i+1][w[i] + j], dp[i][j] + v[i]);
				}
				dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
			}
		}
		int aw = 0, av = 0;
		for (int i = 0; i <= W; ++i) {
			if (dp[N][i] > av) {
				av = dp[N][i], aw = i;
			}
		}
		cout << "Case " << t << ":" << endl;
		cout << av << endl;
		cout << aw << endl;
		++t;
	}
}

C++: #include <iostream>
#include <vector>
#include <string.h>

using namespace std;

int main() {
    int count = 1;
    while (true) {
        int limit;
        cin >> limit;
        if (limit == 0)break;
        int n;
        cin >> n;

        vector<int> dp(limit + 1, -1145141919);
        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            char s[15];
            cin >> s;
            int v = stoi(strtok(s, ","));
            int w = stoi(strtok(NULL, ","));
            for (int j = limit; j >= 0; j--) {
                if (j + w > limit)continue;
                dp[j + w] = max(dp[j + w], dp[j] + v);
            }
        }

        int maxvalue = 0;
        int minweight = 0;
        for (int i = 0; i <= limit; i++) {
            if (maxvalue < dp[i]) {
                maxvalue = dp[i];
                minweight = i;
            }
        }

        cout << "Case " << count << ":" << endl;
        cout << maxvalue << endl;
        cout << minweight << endl;
        count++;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int dp[1001][1001] = {{0}};
int main(){
  int w,n;
  char ch;
  int P[1001]={0},W[1001]={0},t = 1;
  while(cin >> w,w ){
    cin >> n;
    for(int i = 0 ; i < n ; i++ ){
      cin >> P[i] >> ch >> W[i];
    }
    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][j],dp[i][j-W[i]] + P[i]);
	}else dp[i+1][j] = dp[i][j];
      }
    }
    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<iostream>
#include<stdio.h>
using namespace std;
int dp[1001][1001];
int main(){
  int W,N,v[1000],w[1000];
  int n=1;
  int lastj;
  char jyama;
  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<N-1;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]);
	  if(dp[i+1][j]!=dp[i][j]) lastj=j;
	}
      }
    }
    cout<<"Cace "<<n<<":"<<endl;
    cout<<dp[N-1][W]<<endl;
    cout<<lastj<<endl;
  }
}