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 <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/


void solve(int W,int N){
    vector<int> v(N),w(N);
    char c;
    rep(i,N) cin>>v[i]>>c>>w[i];

    vector<vector<int>> dp(N+10,vector<int>(W+10,0));
    for(int i=0;i<N;i++){
        // use v[i],w[i]
        for(int j=0;j<=W;j++){
            if(j+w[i]<=W){
                dp[i+1][j+w[i]] = max(dp[i+1][j+w[i]],max(dp[i][j]+v[i],dp[i][j+w[i]]));
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }else{
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }
        }
    }

    int mx=0;
    for(int i=0;i<=N;i++) for(int j=0;j<=W;j++) mx = max(mx,dp[i][j]);
    assert(mx==dp[N][W]);

    for(int i=0;i<=W;i++) if(dp[N][i]==dp[N][W]){
        printf("%d\n%d\n",dp[N][W],i);
        return;
    }
    assert(false);
}

int main(){
    int w,n;
    int cnt=0;
    while(1){
        cin>>w;
        if(w==0) break;
        cin>>n;
        printf("case %d:\n",++cnt);
        solve(w,n);
    }
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    int k = 0;
    for(;;){
        int w, n;
        cin >> w >> n;
        if(w == 0)
            return 0;

        vector<int> price(w+1, -1);
        price[0] = 0;
        for(int i=0; i<n; ++i){
            int a, b;
            char c;
            cin >> a >> c >> b;
            for(int j=w-b; j>=0; --j){
                if(price[j] != -1)
                    price[j+b] = max(price[j+b], price[j] + a);
            }
        }

        int ret1 = 0, ret2;
        for(int i=0; i<=w; ++i){
            if(price[i] > ret1){
                ret1 = price[i];
                ret2 = i;
            }
        }
        cout << "Case " << (++k) << ':' << endl;
        cout << ret1 << endl << ret2 << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[1001][1001],wait[1001],value[1001],waiting[1001][1001];
void Initialization()
{
    for(int i=0;i<1001;i++){
            for(int j=0;j<1001;j++){
                    dp[i][j]=-1;
                    waiting[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 co,int w)
{
    int num,wai=w;
    if(co==1)wai-=j;
    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,0,w);
    else num=max(serch(i+1,j,n,0,w),serch(i+1,j-wait[i],n,1,w)+value[i]);
    waiting[i][j]=wai;
    return dp[i][j]=num;
}
int waitserch(int w,int n)
{
    int num=0;
    for(int i=n;i>0;i--){
        for(int j=0;j<=w;j++)if(waiting[i][j]>num&&waiting[i][j]>0&&waiting[i][j]!=w)num=waiting[i][j];
    }
    if(num==0)num=w;
    return num;
}
int main()
{
    int n,w,point,num=1,wai=0;
    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,0,w);
        wai=waitserch(w,n);
        cout<<"Case "<<num<<":\n"<<point<<endl<<wai<<endl;
        num++;
    }
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(){
  int w,n,a,b,ca=0;
  
  while(scanf("%d",&w) && w != 0){
    scanf("%d",&n);
    int dp[n][w+1];
    fill_n(*dp,n*(w+1),0);
    for(int i = 0;i < n;i++){
      scanf("%d,%d",&a,&b);
      if(i == 0) dp[i][b] = a;
      else {
	for(int j = 0;j <= w;j++){
	  if(j < b) dp[i][j] = dp[i-1][j];
	  else dp[i][j] = max(dp[i-1][j],dp[i-1][j-b]+a);
	}
      }
    }
    ca++;
    printf("Case %d:\n",ca);
    printf("%d\n",dp[n-1][w]);
    for(int i = w-1;i >= 0;i--){
      if(dp[n-1][w] != dp[n-1][i]){
	printf("%d\n",i+1);
	break;
      }
    }
  }

  return (0);
}