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<string>
#include<vector>
#include<algorithm>
#include<stdio.h>
using namespace std;

class treasure
{
public:
int value, weight;
bool have;
};

treasure tre[1000];
int N,W, VALUE = 0, WEIGHT = 999999;


void calcu()
{
	int sum_v = 0, sum_w = 0;
	for (int i = 0; i < N; i++)
	{
		if (tre[i].have == true)
		{
			sum_v += tre[i].value;
			sum_w += tre[i].weight;
		}
	}
	if (sum_v>VALUE)
	{
		WEIGHT = sum_w;
		VALUE = sum_v;
	}
	else if(sum_v==VALUE &&  WEIGHT>sum_w)
	{
		 WEIGHT = sum_w;
	}
}

void function(int n, int w)//0,W
{
	if (n >= N)	
	{
		calcu();
		//cout << VALUE << "," << WEIGHT << endl;
	}
	else if (tre[n].weight > w)	
	{
		tre[n].have=false;
		function(n + 1, w);
	}
	else
	{
		tre[n].have = true;	  
		function(n + 1, w - tre[n].weight);
		tre[n].have = false;   
		function(n + 1, w);
	}
}

int main()
{
	int W;
	for (int count = 1; cin >> W, W != 0; count++)
	{
		VALUE = 0;
		WEIGHT = 999999;
		cin >> N;
		for (int i = 0; i < N; i++)
		{
			scanf("%d,%d", &tre[i].value, &tre[i].weight);
		}
		function(0,W);
		cout << "case " << count << ":" << endl;
		cout << VALUE << endl;
		cout << WEIGHT << endl;
	}
	return 0;
}

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

void solve( int case_i, int W, int N ) {
	vector<int> v( N+1 ), w( N+1 );
	for( int i = 1; i <= N; ++i ) {
		char c;
		cin >> v[i] >> c >> w[i];
	}

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

	int rv = dp[N][W];
	int rw = W;
	for( int i = W-1; i > 0; --i ) {
		if( dp[N][i] == rv ) {
			rw = i;
		}
	}

	cout << "Case " << case_i << ":\n";
	cout << rv << '\n' << rw << '\n';
}

int main() {
	ios_base::sync_with_stdio( false );

	int W, N;
	for( int i = 1; cin >> W >> N; ++i ) {
		if( W == 0 ) {
			break;
		}

		solve( i, W, N );
	}

	return 0;
}

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


int main() {
  int dp[2][1001],n,k[1000],w[1000],i,j,h,kg,mk,mw,cn=0;
  while(true) {
	  cn++;
	  cin >> kg;
	  if (kg==0) break;
	  for (i=0;i<=kg;i++) {dp[0][i]=0; dp[1][i]=0;}
	  cin >> n;
	  for (i=0;i<n;i++) scanf("%d,%d",&k[i],&w[i]);
	  for (i=0,j=0;i<n;i++) {
		  j=1-j;
		  for (h=kg;h>0;h--)
			  if (h-w[i]>=0) dp[j][h-w[i]]=(dp[1-j][h]+k[i])>dp[1-j][h-w[i]] ? dp[1-j][h]+k[i] : dp[1-j][h-w[i]];
	  }
  mk=0;
  for (i=kg;i>=0;i--) if (mk<dp[j][i]) {mk=dp[j][i]; mw=i;}
  cout << "Case " << cn << ":" << endl;
  cout << mk << endl;
  cout << kg-mw << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define SUPmaxm 1001
#define SUPsupw 1001

int maxm;
int supw;
int vs[SUPmaxm];
int wos[SUPmaxm];
int dp[SUPmaxm][SUPsupw]; //m ika no item tukatte, w ika no omomi de, sum v no max
int rev,rew;

void solve(){
	rev=0;
	FOR(m,0,maxm+1) FOR(w,0,supw) dp[m][w]=0;
	FOR(m,1,maxm+1){
		FOR(w,0,supw){
			if(w<wos[m]) dp[m][w] = dp[m-1][w];
			else dp[m][w] = max(dp[m-1][w],dp[m-1][w-wos[m]]+vs[m]);
			rev=max(rev,dp[m][w]);
		}
	}
	rew=0;
	FOR(w,0,supw) FOR(m,0,maxm+1) if(dp[m][w]==rev) {rew=w;return;};
}

int main(){
	for(int casei=1;true;++casei){
		scanf("%d",&supw);			if(!supw) return 0;
		scanf("%d",&maxm);
		FOR(m,1,maxm+1) scanf("%d,%d",vs+m,wos+m);
		solve();
		printf("Case %d:\n%d\n%d\n",casei,rev,rew);
	}
}

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

using namespace std;

int dp[1000][1001],t[1000],w[1000];

int main(){
  for(int c=1;;c++){
    int n,q,i,j;
    scanf("%d",&q); if(q==0) break;
    scanf("%d",&n);

    for(i=0; i<n; i++)
      scanf("%d,%d",&t[i],&w[i]);

    for(i=0; i<=q; i++)
      dp[0][i] = (w[0]<i)?w[0]:0;

    for(i=1; i<n; i++)
      for(j=0; j<=q; j++){
	if(j+w[i]<=q)
	  dp[i][j+w[i]] = dp[i-1][j]+t[i];
	dp[i][j] = max(dp[i][j],dp[i-1][j]);
      }

    int mw=0,mt=0;
    for(j=0; j<=q; j++)
      if(dp[n-1][j]>mt){
	mt = dp[n-1][j];
	mw = j;
      }

    printf("Case %d:\n%d\n%d\n",c,mt,mw);
  }
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<list>
#define MAX(x,y) ((x>y)? x:y)

using namespace std;

FILE *wfp=fopen("output.txt","w");
FILE *rfp=fopen("input.txt","r");

int W;
int N;

int kati[10000];
int omosa[10000];

int DP[1005][10005];//[i][j]i番目までにjより小さくなるような価値
int DP_W[10005];//そのときの重さ

int CASE=0;
int main()
{
	///wfp=stdout;
	///rfp=stdin;
	while(true)
	{
		CASE++;
		fill(DP_W,DP_W+10005,0);
		for(int i=0;i<10000;i++)DP[0][i]=0;
		for(int i=0;i<1000;i++)DP[i][0]=0;
		fscanf(rfp,"%d",&W);
		if(W==0)break;
		fscanf(rfp,"%d",&N);
		for(int i=0;i<N;i++)
		{
			fscanf(rfp,"%d,%d",&kati[i],&omosa[i]);
		}
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<W;j++)
			{
				if(j+1-omosa[i]>=0)DP[i+1][j+1]=MAX(DP[i][j+1],DP[i][j+1-omosa[i]]+kati[i]);
				else DP[i+1][j+1]=DP[i][j+1];
			}
		}
		DP_W[0]=0;
		for(int i=1;i<=W;i++)
		{
			if(DP[N][i]==DP[N][i-1])DP_W[i]=DP_W[i-1];
			else DP_W[i]=i;
		}


		fprintf(wfp,"Case %d:\n",CASE);
		fprintf(wfp,"%d\n",DP[N][W]);
		fprintf(wfp,"%d\n",DP_W[W]);
	}
	fclose(rfp);
	fclose(wfp);
	return 0;
}