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 <cstring>
using namespace std;

#define INF 10000000

int W,N;
int v[1001],w[1001];

// dp[i][j] : iテァツ閉ェテァツ崢ョテ、ツサツ・テゥツ卍催」ツ?ョテ」ツつづ」ツ?ョテ」ツ?凝」ツつ嘉ゥツ?催」ツ?陛ッツスツ甘、ツサツ・テ、ツクツ凝」ツ?ォテ」ツ?ェテ」ツつ凝」ツつ暗」ツ??」ツ?ォテゥツ?クテ」ツつ禿」ツ??ヲツ卍づ」ツ?ョテヲツ慊?・ツ、ツァテ・ツ?、
int dp[1002][1002];

int rec(int i, int j){
  if(dp[i][j] >= 0){
    return dp[i][j];
  }
  int res;
  if(i == N){
    res = 0;
  }else if( j < w[i] ){
    res = rec(i + 1 , j);
  }else{
    res = max( rec(i + 1,j) , rec(i+1, j-w[i]) + v[i] );
  }
  return dp[i][j] = res;
}

int main(){
  int x = 1;
  while(1){
   
    cin >> W;
    if(W == 0) break;

    cin >> N;
    memset(dp,-1,sizeof(dp));
    for(int i = 0; i < 1001; i++){
      w[i] = INF;
      v[i] = -1;
    }
 
    for(int i = 0 ; i < N ; i++){
      char tmp;
      cin >> v[i] >> tmp >> w[i];
      //  cout << v[i] <<" "<< w[i] << endl; 
    }
    
    for(int i = 0; i < N ; i++){
      for(int j = 0; j <= W ; j++){
	rec(i,j);
      }
    }
    
    cout <<"Case " << x << ":" << endl;
    cout <<  dp[0][W] << endl;
    int t;
    for( t = 0; dp[0][W] != dp[0][t] ;t++){}
    cout << t << endl; 
    x++;
  }
  return 0;

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
#include <cmath>
//#include <string>
//#include <sstream>
#include <iomanip>
//#include <complex>
#include <stdio.h>
//小数制度 	cout << fixed << setprecision(5) << tmp << endl;
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)

int dp[1010][1010];
int v[1010];
int w[1010];
int main(){
  int wei,n;
  int cnt = 1;
  while(cin >> wei,wei){
    cin >> n;
    REP(i,1010) REP(j,1010) dp[i][j] = 0;
    w[0]=0;v[0]=0;
    for(int i=1;i<n+1;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<=wei;j++){
        if(j-w[i]<0) dp[i+1][j] = dp[i][j];
        else  dp[i+1][j]=max(dp[i][j+1],dp[i][j-w[i]]+v[i]);
      }
    }
    /*for(int i=0;i<n+1;i++){
      for(int j=0;j<wei+1;j++){
        cout << dp[i][j] << " ";//l<<endl;
      }
      cout << endl;
    }*/
    int maxv=0;
    int ans;
    for(int i=0;i<wei+1;i++){
      if(maxv < dp[n][i]){
        maxv = dp[n][i];
        ans = i;
      }
    }
    cout << "Case " << cnt << ":" <<endl;
    cout << maxv << endl << ans << endl;
    cnt++;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

#define WMAX 1000
#define NMAX 1000
int main(void)
{
    int W, n;
    int cyc = 0;
    while (cin >> W && W && cin >> n) {
        cyc++;
        int dp[WMAX+1][NMAX+1] = {};
        for (int i = 0; i < n; i++) {
            int v, w;
            scanf("%d,%d", &v, &w);
            for (int j = 0; j < W+1; j++) 
                dp[j][i+1] = max(dp[j][i], (j-w >= 0 ? dp[j-w][i] + v : 0));
        }
        cout << "Case " << cyc << ":" << endl;
        for (int j = 0; j < W+1; j++) {
            if (dp[j][n] == dp[W][n]) {
                cout << dp[j][n] << endl << j << endl;
                break;
            }
        }
    }
}

C++: #include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int sum_w[1001][1001];
int sum_v[1001][1001];
void vmax(int,int,int[],int[]);
int main(){
	int count=0;
	while(1){
		int i,j,lim=0,N=0,v[1001],w[1001],k=0,l=0;
		for(i=0;i<1001;i++){
			for(j=0;j<1001;j++){
				sum_v[i][j]=0;
				sum_w[i][j]=0;
			}
		}
		count++;
		cin>>lim;
		if(lim==0)break;
		cin>>N;
		for(i=0;i<N;i++){
			scanf("%d,%d",&v[i],&w[i]);
		}
		for(i=0;i<N;i++){
			for(j=0;j<=lim;j++){
				vmax(i,j,v,w);
			}
		}
		for(i=0;i<N;i++){
			if(k<sum_v[N-1][i]){
				k=sum_v[N-1][i];
				l=i;
			}
		}
		cout<<"Case"<<count<<endl;
		cout<<sum_v[N-1][l]<<endl;
		cout<<sum_w[N-1][l]<<endl;

	}
	
	return 0;
}
void vmax(int i,int j,int v[],int w[]){
	if(sum_v[i-1][j]<sum_v[i-1][j-w[i]]+v[i]){
		sum_v[i][j]=sum_v[i-1][j-w[i]]+v[i];
		sum_w[i][j]=sum_w[i-1][j-w[i]]+w[i];
	}
}

C++: #include<iostream>
#include<iomanip>
#include<vector>
#include<queue>
#include<list>
#include<stack>
#include<algorithm>
#include<cmath>
#include<memory>

using namespace std;

int main()
{
  int maxw;
  int x = 0;
  
  while ( cin >> maxw && maxw != 0 )
    {
      int n;
      cin >> n;

      char c;
      vector<int> v(n, 0);
      vector<int> w(n, 0);
      vector<vector<int>> dp(n+1, vector<int>(maxw+1, 0));

      for ( auto i = 0; i < n; ++i )
        {
          cin >> v[i];
          cin >> c;
          cin >> w[i];
        }

      for ( auto i = 0; i < n; ++i )
        for ( auto j = 0; j <= maxw; ++j )
          if ( w[i] <= j )
            dp[i+1][j] = max( dp[i][j], dp[i][j-w[i]] + v[i]);
          else
            dp[i+1][j] = dp[i][j];

      int mw = maxw;
      while ( dp[n][mw] == dp[n][maxw] ) --mw;
        
      cout << "Case " << ++x << ":" << endl;
      cout << dp[n][maxw] << endl;
      cout << mw + 1 << endl;
    }
  
  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))
            {
                res.first = dp [n] [j];
                res.second = w - j;
            }
        }
        
        std::cout << "Case " << index ++ << ":" << std::endl;
        std::cout << res.first << std::endl << res.second << std::endl;
    }
}