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 <iostream>
#include <climits>
using namespace std;

int main(void) 
{
  int dp[1001][1001];  // [takara][weight]
  int wei[1001];       // [takara]
  int val[1001];       // [takara]
  int i, w;
  int n, tw;
  int datcnt;
  char c;

  cin >> tw;
  for(datcnt = 1; tw != 0; datcnt++){
    cin >> n;
    for(i = 1; i <= n; i++){
      cin >> val[i];
      cin >> c;
      cin >> wei[i]; 
    }

    // initArrayDP
    for(i = 0; i <= n; i++) {
      for(w = 0; w <= tw; w++) {
	dp[i][w] = 0;
      }
    }
    cout << "--map";
    for(i = 1; i <= n; i++) {
      for(w = 1; w <= tw; w++) {
	int tem = w - wei[i];
	if(tem < 0) {
	  dp[i][w] = dp[i-1][w];
	} else {
	  dp[i][w] = max(dp[i-1][w], dp[i-1][tem] + val[i]);
	}
      }
    }

    //for(i = 1; i <= n; i++) {
    //  for(w = 1; w <= tw; w++) {
    //	printf("%5d", dp[i][w]);
    //  }
    //  printf("\n");
    //}

    
    // [takara]の最終列から価値最大かつ、そのなかで重さ最初を探す。
    int maxval  = INT_MIN;
    int lastwei = INT_MAX;

    for(w = 0; w <= tw; w++)
      maxval = max(dp[n][w], maxval);

    for(w = tw; w >= 0; w--)
      if(dp[n][w] == maxval) lastwei = min(lastwei, w);

    cout << "Case " << datcnt << ":" << endl;
    cout << maxval << endl << lastwei << endl;
    
    cin >> tw;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[1001][1001],wait[1001],value[1001],dpp[1001][15000];
void Initialization()
{
    for(int i=0;i<1001;i++)for(int j=0;j<1001;j++)dp[i][j]=-1;
    for(int i=0;i<1001;i++)for(int j=0;j<20000;j++)dpp[i][j]=-1;
}
int power(int n)
{
    int num=1;
    for(int i=1;i<=n;i++)num*=10;
    return num;
}
int serch(int i,int j,int n)
{
    int num;
    if(dp[i][j]>-1)return dp[i][j];
    else if(i==n)num=0;
    else if(j<wait[i])num=serch(i+1,j,n);
    else num=max(serch(i+1,j,n),serch(i+1,j-wait[i],n)+value[i]);
    return dp[i][j]=num;
}
int wserch(int i,int va,int ser,int n,int w)
{
    int num;
    if(dpp[i][va]>-1)return dpp[i][va];
    else if(i==n)num=w;
    else if(ser<va+value[i])num=wserch(i+1,va,ser,n,w);
    else if(ser==va+value[i])num=wait[i];
    else num=min(wserch(i+1,va,ser,n,w),wserch(i+1,va+value[i],ser,n,w)+wait[i]);
    return dpp[i][va]=num;
}
int main()
{
    int n,w,point,num=1,wai,wa;
    string str;
    while(cin>>w&&w!=0){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>str;
            value[i]=0;wait[i]=0;
            for(int j=0;;j++){
                if(str[j]==','){
                    for(int k=0;k<j;k++)value[i]+=power(j-k-1)*(str[k]-'0');
                    point=j;
                }
                if(str[j]=='\0'){
                    for(int k=point+1;k<j;k++)wait[i]+=power(j-k-1)*(str[k]-'0');
                    break;
                }
            }
        }
        Initialization();
        point=serch(0,w,n);
        wai=w;
        for(int i=0;i<n;i++){
            if(value[i]<point){
                wa=wserch(i+1,value[i],point,n,w)+wait[i];
                if(wa<wai)wai=wa;
            }
            else if(value[i]==point&&wai>wait[i])wai=wait[i];
        }
        cout<<"Case "<<num<<":\n"<<point<<endl<<wai<<endl;
        num++;
    }
}

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>
using namespace std;
int za[10001][10001],kati[10001],omo[1001];
int main(void)
{
	int w,n,i,j,x,p,max,mm;
	x=0;

	scanf("%d",&w);
	scanf("%d",&n);
	while(!(w==0)){
		for(i=0;i<=n;i++){
			for(j=0;j<=w;j++){
				za[i][j]=-1;
			}
		}
		for(i=0;i<n;i++){
			scanf("%d,%d",&kati[i],&omo[i]);
		}
		za[0][0]=0;
		for(i=0;i<n;i++){
			for(j=0;j<=w;j++){
				if(za[i][j]!=-1){
					if(za[i+1][j+omo[i]]<za[i][j]+kati[i] && j+omo[i]<=w){
						za[i+1][j+omo[i]]=za[i][j]+kati[i];
						
					}
					if(za[i+1][j]<za[i][j]){
						za[i+1][j]=za[i][j];
					}
				}
			}
		}
		/*for(i=0;i<=n;i++){
			for(j=0;j<=w;j++){
				printf("%8d",za[i][j]);
			}
			printf("\n");
		}*/
		max=-1;
		mm=-1;
		for(i=0;i<=w;i++){
			if(za[n][i]!=-1){
				if(za[n][i]>max){
					max=za[n][i];
					mm=i;
				}
			}
		}
		x++;
		printf("Case %d:\n",x);
		printf("%d\n",max);
		printf("%d\n",mm);
		scanf("%d",&w);
		scanf("%d",&n);
	}
	
	return 0;
}

C++: #include <iostream>
#include <utility>

auto main () -> int
{
    int index = 1;
    int w;
    while (std::cin >> w && w)
    {
        int n;
        std::cin >> n;
        int dp [n + 1] [w + 1];
        int values [n];
        int weights [n];
        for (auto i = 0; i < n; ++ i)
        {
            std::scanf ("%d,%d" , values + i , weights + i);
        }
        
        for (auto j = 0; j <= w; ++ j)
        {
            dp [0] [j] = 0;
        }
        for (auto i = 1; i <= n; ++ i)
        {
            for (auto j = w; j >= 0; -- j)
            {
                dp [i] [j] = std::max (dp [i - 1] [j] , j + weights [i - 1] < w ? dp [i - 1] [j + weights [i - 1]] + values [i - 1] : 0);
            }
        }
        auto res = std::make_pair (0 , 0);
        for (int j = 0; j <= w; ++ j)
        {
            if (res.first < dp [n] [j])
            {
                res.first = dp [n] [j];
                res.second = w - j - 1;
            }
        }
        
        std::cout << "Case " << index ++ << ":" << std::endl;
        std::cout << res.first << std::endl << res.second << std::endl;
    }
}

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

#define scanf scanf_s

int a[10001] = { 0 };
int b[10001] = { 0 };
int w, n, mv, mw;
void solve(int W, int f, int sum) {
	f = min(f, n) + 1;
	for (int i = f; i < n; i++) {
		if (W - b[i] < 1) {
			mw = max(w - W, mw);
			mv = max(sum, mv);
			return;
		}
		solve(W - b[i], f, sum + a[i]);
	}
}

int main() {
	int u = 0;
	while (cin >> w) {
		if (w == 0) break;
		cin >> n;

		mv = mw = 0; u++;
		for (int i = 0; i < n; i++) {
			scanf("%d,%d", &a[i], &b[i]);
		}

		solve(w, -1, 0);
		printf("Case %d:\n%d\n%d\n", u, mv, mw);
	}
	return 0;
}