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 <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <string.h>

#define loop(lp) for(int lp2 = 0; lp2 < lp; lp2 ++)
#define ascent(i, a, b) for(int i = a; i < b; i ++)

#define pb push_back

using namespace std;

int dp[2000][20000];

int main() {
  int w_max, count = 0;
  while(cin >> w_max, w_max) {
    count ++;
    int n;
    cin >> n;
    
    memset(dp, 0, sizeof(dp));

    int v, w;
    pair<int, int> w_sum;
    w_sum = make_pair(0, 0);

    ascent(i, 1, n+1) {
      scanf("%d,%d", &v, &w);

      ascent(j, 1, w_max+1) {
	if(w <= j) {
	  if(dp[i-1][j] < dp[i-1][j-w] + v) {
	    dp[i][j] = dp[i-1][j-w] + v;

	    if(w_sum.first < dp[i][j] || w_sum.first == dp[i][j] && j < w_sum.second) {
	      w_sum = make_pair(dp[i][j], j); 

	      //cout << "i = " << i << " " << "wsum = " << w_sum.second << endl;
	    }
	  }
	  else {
	    dp[i][j] = dp[i-1][j];
	  }
	}
	else {
	  dp[i][j] = dp[i-1][j];
	}
      }

    }
    /*   
    ascent(i, 0, n+1) {
      ascent(j, 0, w_max+1) {
	printf("%3d ", dp[i][j]);
      }
      puts("");
    }
    */
    cout << "Case " << count << ":" << endl;
    cout << dp[n][w_max] << endl;
    cout << w_sum.second << endl;
    
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b){
    if(a<b) swap(a, b);
    if(b==0) return a;
    return gcd(b, a%b);
}

int W, N, cnt=1;
const int MAX_W = 1000;
const int MAX_N = 1000;
int dp[MAX_N+1][MAX_W+1];
int w[MAX_N]; int v[MAX_N];
int main() {
    while(cin >> W, W){
        cin >> N;
        for(int i=0; i<N; ++i){
            scanf("%d,%d", &v[i], &w[i]);
        }
        int sum_w=0;
        dp[0][0] = 0;
        for(int i=N-1; i>=0; --i){
            for(int j=0; j<=W; ++j){
               if(j<w[i]){
                   dp[i][j] = dp[i][j];
               }else{
                   dp[i][j] = max(dp[i+1][j], dp[i+1][j-w[i]]+v[i]);
               }
            }
        }
        for(int i=0; i<N; ++i){
            for(int j=0; j<=W; ++j){
                if(dp[i][j]==dp[0][W]){
                    sum_w = j;
                    break;
                }
            }
            if(sum_w) break;
        }
        cout << "Case " << cnt << ":" << endl;
        cout << dp[0][W] << endl;
        cout << sum_w << endl;
        cnt++;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int dp[1000][1000];
	int count=1,i,j,W,w,n;
	while(1)
	{
		memset(dp,0,sizeof(dp));
		cin >> W;
		if(W==0)break;
		cin >> n;
		int v[n],w[n];
		for(i=0;i<n;i++)
		{
			scanf("%d,%d",&v[i],&w[i]);
		}
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=W;j++)
			{
				if(i==0)
					dp[i][j]=0;
				else if(w[i]>j)
					dp[i][j]=dp[i-1][j];
				else
					dp[i][j] = max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
			}
		}
		for(i=W;i>=0;i--)
		{
			if(dp[n][W]>dp[n][i])break;
		}
		cout << "Case " << count << ':' << endl << dp[n][W] << endl;
		cout << i+1 << endl;
		count++;
	}
	return 0;
}

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

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int W, t = 0;
  while(cin >> W, W) {
    t++;
    int N; cin >> N;
    int v[1001], w[1001];
    rep(i, N) scanf("%d,%d", &v[i], &w[i]);

    int dp[1001] = {0};
    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]);
      }
    }

    int aw = 0;
    while(dp[aw] != dp[W]) aw++;

    printf("Case %d:\n%d\n%d\n", t, dp[W], aw);
  }

  return 0;
}

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

typedef int integer;
const int NMAX = 1008, WMAX = 10008;

integer weight[NMAX], value[NMAX], dp[NMAX][WMAX];

void init() {
  for (int i = 0; i < NMAX; i++) {
    weight[i] = value[i] = 0;
  }
  for (int i = 0; i < NMAX; i++) {
    for (int j = 0; j < WMAX; j++) {
      dp[i][j] = 0;
    }
  }
}

int main() {
  int W;
  int Case = 0;
  while (cin >> W and W) {
    Case++;
    init();

    int N;
    cin >> N;
    for (int i = 1; i <= N; i++) {
      scanf("%d,%d", &value[i], &weight[i]);
    }
    
    for (int w = 1; w <= W; w++) {
      for (int i = 1; i <= N; i++) {
        if (weight[i] > w) {
          dp[i][w] = dp[i-1][w];
        } else {
          dp[i][w] = max(dp[i-1][w], dp[i-1][w-weight[i]] + value[i]);
        }
      }
    }

    int minW = W, maxV = dp[N][W];
    for (int w = W; w >= 0; w--) {
      if (dp[N][w] == maxV) {
        minW = w;
      }
    }
    cout << "Case " << Case << ":" << endl;
    cout << maxV << endl << minW << endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>
#include <ctime>
#define INF 100000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;

/*---------------------------------------------------*/

int W,N;
int v[1005],w[1005];
int dp[1005][1005];

void DEBUG(){
  rep(i,W+1)cout<<i<<": "<<dp[N][i]<<endl;
}

void init(){
  rep(i,1005){
    rep(j,1005){
      dp[i][j]=-INF;
    }
  }
}

void DP(){
  for(int i=0;i<1005;i++)dp[0][i]=0;
  for(int i=1;i<=N;i++){
    for(int j=0;j<=W;j++){
      if(j>=w[i])dp[i+1][j]=max(dp[i][j-w[i]]+v[i],dp[i][j]);
      else dp[i+1][j]=dp[i][j];
    }
  }
}

int main(){
  int cnt=1;
  while(cin>>W){
    if(W==0)break;
    cin>>N;
    rep(i,N)scanf("%d,%d",&v[i],&w[i]);
    init();
    DP();
    //DEBUG();
    cout<<"Case: "<<cnt<<":"<<endl;
    cout<<dp[N][W]<<endl;
    for(int i=W;i>=1;i--)if(dp[N][i]!=dp[N][i-1]||i==1){cout<<i<<endl;break;}
    cnt++;
  }
  
  return 0;
}