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 <string>
#include <math.h>
#include <map>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <bitset>

using namespace std;



int main() {
    int furosiki;
    int case_count = 1;

    
    while(cin>>furosiki){
        if(furosiki==0){
            break;
        }
        
        
        
        int num;
        cin>>num;
        size_t bits;
        bits = num;
        bitset<1000> bs;
        
        
        int val[num];
        int wei[num];
        for(int i=0; i<num; i++)scanf("%d,%d",&val[i],&wei[i]);
        int binary_max=1;
        for(int i=0; i<num; i++)binary_max*=2;
        int max_val = 0;
        int max_wei = 0;
        for(int i=0; i<binary_max; i++){
            bs = i;
            int temp_val=0;
            int temp_wei=0;
            for(int j=0; j<num; j++){
                temp_val+=bs[j]*val[j];
                temp_wei+=bs[j]*wei[j];
            }
            if(temp_wei<=furosiki){
                if(max_val<temp_val){
                    max_val=temp_val;
                    max_wei=temp_wei;
                }else if(max_val==temp_val && max_wei>temp_wei){
                    max_val=temp_val;
                    max_wei=temp_wei;
                }
            }
        }
        cout<<"case"<<case_count<<":"<<endl;
        cout<<max_val<<endl;
        cout<<max_wei<<endl;
        case_count++;
    }
    
    
    return 0;
}

C++: #include <bits/stdc++.h>

#define SIZE 300005
#define MOD 1000000007LL
#define INF INT_MAX / 3
#define LLINF 1LL << 60
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define DOWN(i,b,a) for(int i=b;i>=a;i--)
#define SET(a,c) memset(a,c,sizeof a)
#define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FOREACH(i,c) for(auto (i) : (c))
#define BIT(i,j) ((i)>>(j))&1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end())
#define SQ(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef valarray<int> Array;
typedef pair<ll,ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i];if(i==a.size()-1)cout<<endl;else cout<<" ";}}
int gcd(int a,int b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);}
int lcm(int a,int b){return a==0||b==0?0:a*b/gcd(a,b);}

int W, N;
ll v[1010];
ll w[1010];
const int NMAX = 1000;
const int VMAX = 10000;
ll dp[NMAX*VMAX];

void solve()
{
  int NV = N*10000;
  fill_n(dp,NV,LLINF);
  dp[0] = 0;
  REP(i,N) DOWN(j,NV-1,v[i])
    dp[j] = min(dp[j],dp[j-v[i]]+w[i]);
  DOWN(i,NV-1,0) if(dp[i] <= W)
  {
    printf("%d\n%lld\n",i,dp[i]);
    return;
  }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  for(int i = 1; scanf("%d",&W), W; i++)
  {
    scanf("%d",&N);
    REP(i,N) scanf("%lld,%lld",&v[i],&w[i]);
    printf("Case %d:\n",i);
    solve();
  }
  return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
int dp[1000],N,v[100],w[100],W;
signed main(){
    for(int l=0;;l++){
        fill(dp,dp+100,0);
        fill(v,v+100,0);
        fill(w,w+100,0);
        cin>>W;
        if(W==0)break;
        cin>>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=W;j>=w[i];j--){
                dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
            }
        }
        for(int i=W;i>=0;i--){
            if(dp[W]!=dp[i]){
                cout<<"Case "<<l<<":"<<endl<<dp[W]<<endl<<i+1<<endl;
                break;
            }
        }
    }
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
void q42() {
	int w, n;
	int c = 0;
	for (; cin >> w >> n;) {
		char tmp[32];
		scanf("%c", &tmp[0]);
		c++;
		int value[1001];
		int weight[1001];
		int dp[1001];
		memset(dp, 0, sizeof(dp));
		for (int i = 0; i < n; i++) {
			int v_i, w_i;
			scanf("%d,%d", &v_i, &w_i);
			scanf("%c", &tmp[0]);
			value[i] = v_i;
			weight[i] = w_i;
		}
		for (int i = 0; i < n; i++) {
			for (int j = w; j >= weight[i]; j--) {
				dp[j] = max(dp[j], dp[j - weight[i]] + value[i]);
			}
		}
		int max_value = 0;
		int max_weight = 0;
		for (int i = 1; i <= w; i++) {
			if (dp[i] > max_value) {
				max_value = dp[i];
				max_weight = i;
			}
		}
		cout << "Case " << c << ":" << endl;
		cout << max_value << endl;
		cout << max_weight << endl;
	}
}

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