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

const int MAXW = 1000;
const int MAXN = 1000;
int DPV[MAXN + 4][MAXW + 4];
int DPW[MAXN + 4][MAXW + 4];
int W[MAXN + 4], V[MAXN + 4];

void init() {
  for (int i = 0; i < MAXN + 4; i++)
    W[i] = V[i] = 0;

  for (int i = 0; i < MAXN + 4; i++)
    for (int j = 0; j < MAXN + 4; j++)
      DPV[i][j] = DPW[i][j] = 0;
}

void solveV(int n, int w) {
  for (int i = n; i > 0; i--) {
    for (int j = 0; j <= w; j++) {
      if (j < W[i]) {
        DPV[i][j] = DPV[i + 1][j];
        DPW[i][j] = DPW[i + 1][j];
      } else {
        if (DPV[i + 1][j] > DPV[i + 1][j - W[i]]  + V[i]) {
          DPV[i][j] = DPV[i + 1][j];
          DPW[i][j] = DPW[i + 1][j];
        } else if (DPV[i + 1][j] == DPV[i + 1][j - W[i]] + V[i]) {
          DPV[i][j] = DPV[i + 1][j];
          DPW[i][j] = min(DPW[i + 1][j], DPW[i + 1][j - W[i]] + W[i]);
        } else {
          DPV[i][j] = DPV[i + 1][j - W[i]] + V[i];
          DPW[i][j] = DPW[i + 1][j - W[i]] + W[i];
        }
      }
    }
  }
}

int main() {
  int w, Case = 1;
  while (cin >> w and w != 0) {
    init();

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
      scanf("%d,%d", &V[i], &W[i]);

    solveV(n, w);

    cout << "Case " << Case << ":" << endl;
    cout << DPV[1][w] << endl << DPW[1][w] << endl;

    Case++;
  }
}

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

using namespace std;

int MAX = 1000*1000;

void solve(int n, int W, vector<int> v, vector<int> w) {
	int **dp = new int*[n];
	for (int i=0; i<n; i++) {
		dp[i] = new int[MAX+2];
		for (int j=0; j<=MAX+1; j++)
			dp[i][j] = 0;
	}

	int ans = 0;

	dp[0][w[0]] = v[0];
	for (int i=1; i<n; i++) {
		for (int j = 0; j+w[i]<=W; j++) {
			dp[i][j] = max(dp[i][j], dp[i-1][j]);
			dp[i][j+w[i]] = max(dp[i][j+w[i]], dp[i-1][j]+v[i]);
		}
	}

	int val = 0, weight = 0;
	for (int j=0; j<=W; j++) {
		if (val < dp[n-1][j]) {
			val = dp[n-1][j];
			weight = j;
		}
	}

	cout << val << endl << weight << endl;

	for (int i=0; i<n; i++)
		delete [] dp[i];
	delete [] dp;
}

int main() {
	

	int n, W;
	int ver = 1;

	cin >> W;
	while (W!=0) {
		cin >> n;

		vector<int> v(n), w(n);
		for (int i=0; i<n; i++)
			scanf("%d,%d", &v[i], &w[i]);

		cout << "Case " << ver << ":" << endl;
		solve(n, W, v, w);

		cin >> W;
		ver++;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
 
#define rep(i, n) for(int i = 0; i < n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
 
using namespace std;
 
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
 
int main(void){
    for(int w, n, cnt = 0; cin >> w >> n, w;){
        vi dp(w + 1); fill(ALL(dp), -1);
        dp[0] = 0;

        char c;
        while(n --){
            int tv, tw; cin >> tv >> c >> tw;
            rep(i, w + 1) if(i + tw <= w && dp[i] >= 0) dp[i + tw] = dp[i] + tv;
        }
        
        vi::iterator it = max_element(ALL(dp));
        cout << "Case " << ++ cnt << endl << *it << endl << it - dp.begin() << endl;
    }
 
    return 0;
}

C++: #include <iostream>
#include <array>
#include <string>
#include <tuple>
#include <vector>
#include<stdio.h>
#include<string.h>
using namespace std;
//using ll = long long;
#define out(s) cout << (s) << endl;

int dp[1001][1001];

int main()
{
	int W, N, cnt = 1;
	cin >> W;
	while (W!=0)
	{
		cin >> N;
		memset(dp, -1, sizeof(dp));
		dp[0][0] = 0;
		for (size_t i = 0; i < N; i++)
		{
			int vi, wi;
			scanf("%d,%d", &vi, &wi);
			for (size_t n = 0; n <= W; n++)
			{
				if (dp[i][n] != -1) {
					dp[i + 1][n] = max(dp[i][n], dp[i + 1][n]);
					dp[i + 1][n + wi] = max(dp[i][n] + vi, dp[i][n]);
				}
			}
		}
		int dp_max = dp[N][W];
		int index = W;
		for (size_t i = W; i > 0; i--)
		{
			if (dp_max == dp[N][i])index = i;
		}
		cout << "Case " << cnt << ":" << endl << dp_max << endl << index << endl;
		cin >> W;
		cnt++;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
 
#define fir first
#define sec second
 
typedef pair <int, int> P;
vector <P> v(1028);
 
int dp[1028][2048];
 
int main(void)
{
    int cnt = 1;
    while (1){
        int w, n;
        cin >> w; if (w == 0) break;
        cin >> n;
        char a;
        for (int i = 0; i < n; i++){
            scanf("%d,%d", &v[i].fir, &v[i].sec);
        }
        for (int i = 0; i < n; i++){
            for (int j = 0; j <= w; j++){
                if (j < v[i].sec) dp[i+1][j] = dp[i][j];
                else dp[i+1][j] = max(dp[i][j], dp[i][j-v[i].sec]+v[i].fir);
            }
        }
        int res1 = 0, res2 = 0;
        for (int i = 1; i <= w; i++){
            if (res1 < dp[n][i]){
                res1 = dp[n][i];
                res2 = i;
            }
        }
        cout << "Case " << cnt << ":" << endl << res1 << endl << res2 << endl;
        cnt++;
    }
    return 0;
}

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

int main(){

	int cnt, N, W, value, weight, v[1001][1001], w[1001][1001];

	cnt = 0;

	while(1){
		cin >> W;
		if(W == 0) break;
		
		cnt++;
		cin >> N;

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

		cout << "Case " << cnt << ":" << endl;
		cout << v[N][W] << endl;
		cout << w[N][W] << endl; 
		
	}

	return 0;

}