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 <vector>
#include <algorithm>

#define TEST 1
typedef std::pair<int,int> pi;

struct Pred
{
    bool operator()(const pi& f,const pi& s)//<
    {
        return f.second < s.second;
    }
};
pi rec_check(const std::vector<pi> &data,int start,int remw)
{
    if(start >=data.size() )
        return pi(0,0);
    if(data[start].second > remw)
        return pi(0,0);//rec_check(data,start+1,remw);
    pi div0= rec_check(data,start + 1,remw - data[start].second);
    div0.first+=data[start].first;
    div0.second+=data[start].second;
    pi div1 = rec_check(data,start + 1,remw);
    if(div0.first > div1.first )
        return div0;
    if(div0.first < div1.first)
        return div1;
    if(div0.second <div1.second)
        return div0;
    return div1;
    
}
int main(void)
{
    int ccount=1;
    int w,n;
    using namespace std;
    vector<pair<int,int> > data;
    while(scanf("%d%d",&w,&n)==2 && w!=0)
    {
        data.assign(n,std::pair<int,int>(0,0));
        for(int i=0;i<n;i++)
        {
            scanf("%d%*[,]%d",&data[i].first ,&data[i].second );
        }
        Pred pred;
        sort(data.begin(),data.end(),pred);
        printf("Case %d:",ccount);
#if TEST
        for(int i=0;i<n;i++)
        {
            printf("val=%d,wei=%d\n",data[i].first,data[i].second);
        }
#endif
        pi result=rec_check(data,0,w);
        printf("%d\n%d\n",result.first,result.second);
        data.clear();
        ccount++;
    }
    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 <stdio.h>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int knap[1001];
int main(){
    int size, n, w, v, test = 0;
    while(1){
        cin >> size;
        if(size==0) break;
        test++;
        cin >> n;
        fill(knap, knap+size+1, 0);
        for(int i=0; i<n; i++){
            scanf("%d,%d", &v, &w);
            for(int j=size-w; j>=0; j--){
                knap[j+w] = max(knap[j+w], knap[j]+v);
            }
        }
        int ansW = 0;
        int ansV = 0;
        for(int i=0; i<=size; i++){
            if(knap[i] > ansV){
                ansV = knap[i];
                ansW = i;
            }
        }
        cout << "Case " << test << ":" << endl;
        cout << ansV << endl;
        cout << ansW << endl;
    }

}

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

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

using namespace std;
int N;
vector<int> w;
vector<int> v;
int dp[1001][1001];

void solve()
{
	int W;
	int count = 1;
	while(cin >> W, W)
	{
		memset(dp, 0, sizeof(dp));
		cin >> N;
		w.resize(N);
		v.resize(N);
		for(int i = 0; i < N; ++i)
		{
			scanf("%d,%d", &v[i], &w[i]);
		}
		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 pos = 0;
		for(int i = 0; i <= W; ++i)
		{
			if(dp[N][i] > dp[N][pos])
			{
				pos = i;
			}
		}
		cout << "Case " << count << ":" << endl;
		cout << dp[N][pos] << " " << pos << endl;
		++count;
	}
}

int main()
{
	solve();
	return(0);
}

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<stdio.h>
int dp[1001][1001];
int main(void)
{
	int i,j,k,l;
	int w,n;
	int max;
	int a[1001],b[1001];
	
	scanf("%d",&w);
	l=1;
	while(w!=0){
		max=0;
	scanf("%d",&n);
		for(i=0;i<=n;i++){
			for(j=0;j<=w;j++){
				dp[i][j]=-1;
			}
		}
		for(i=0;i<n;i++){
			scanf("%d,%d",&a[i],&b[i]);
		}
		for(i=0;i<n;i++){
			dp[i][0]=0;	
			for(j=0;j<=w;j++){
				if(dp[i][j]!=-1){
					if(j+b[i]<=w && dp[i][j]+a[i]>=dp[i+1][j+b[i]]){
						dp[i+1][j+b[i]]=dp[i][j]+a[i];
					}
					if(dp[i+1][j]<dp[i][j]){
						dp[i+1][j]=dp[i][j];
					}
				}
			}
		}
		for(j=0;j<=w;j++){
			if(dp[n][j]>max){
				max=dp[n][j];
				k=j;
			}
		}
		printf("Case %d:\n",l);
		printf("%d\n%d\n",max,k);
		scanf("%d",&w);
		l++;
	}
	return 0;
}