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 <vector>
#include <list>
#include <algorithm>
#include <string>
#include <stack>
#include <iomanip>
#include <numeric>
#include <queue>
#include <climits>
#include <cmath>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define INF 1LL << 59

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

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

        vector<int> v(N), w(N);
        char c;
        for (int i = 0; i < N; ++i)
            cin >> v[i] >> c >> w[i];

        int dp[1010][1010];
        for (int i = 0; i <= N; ++i)
        {
            for (int j = 0; j <= W; ++j)
            {
                dp[i][j] = 0;
            }
        }

        for (int i = 0; i < N; ++i)
        {
            for (int j = 0; j <= W; ++j)
            {
                if (j - w[i] >= 0)
                {

                    dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
                }
                else
                {

                    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
                }
            }
        }
        cout << "Case " << cnt << ":" << endl;
        int mv = 0, mw = 0;

        for (int i = 0; i <= W; ++i)
        {
            if (dp[N][i] > mv)
            {
                mv = dp[N][i];
                mw = i;
            }
        }
        cout << mv << "\n"
             << mw << endl;
    }

    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 < W && v[i-1][k] + value > v[i][j]){
						v[i][j] = v[i-1][k] + value;
						w[i][j] = w[i-1][k] + weight;
					}
				}
			}
		}

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

	return 0;

}

C++: #include<iostream>
#include<algorithm>
#include<cstring>

constexpr int MAX = 1000;

int dp[MAX][MAX + 1];
int W, n;
int v[MAX], w[MAX];

int main() {
	int cnt = 0;
	while (1) {
		std::cin >> W;
		if (W == 0)
			break;
		++cnt;
		std::cin >> n;
		char ch;
		for (int i = 0; i < n; ++i)
			std::cin >> v[i] >> ch >> w[i];
		std::memset(dp, 0, sizeof(dp));
		dp[0][w[0]] = v[0];
		for (int i = 1; i < n; ++i) {
			for (int j = 0; j <= W; ++j) {
				dp[i][j] = dp[i - 1][j];
				if (j >= w[i])
					dp[i][j] = std::max(dp[i][j], dp[i - 1][j - w[i]] + v[i]);
			}
		}
		int ans = 0, maxv;
		for (int i = W; i >= 0; --i) {
			if (ans <= dp[n - 1][i]) {
				ans = dp[n - 1][i];
				maxv = i;
			}
		}
		std::cout << "Case " << cnt << ':' << std::endl
			<< ans << std::endl << maxv << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 28)

template<typename T>
void remove(vector<T>& vector, unsigned int index){
vector.erase(vector.begin() + index);
}

typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int W, N;
int tres[1001];
int weig[1001];
int maxW, maxV;
map<P, int> dp;

void dfs(int weight, int value, int index){
	if(weight > W)return;
	if(dp[P(weight,index)]>value)return;
	dp[P(weight,index)] = value;
	if(index == N){
		if(maxV < value){
			maxW = weight;
			maxV = value;
		}else if(maxV == value){
			if(maxW > weight){
				maxW = weight;
				maxV = value;
			}
		}
		return;
	}

	dfs(weight, value, index+1);
	dfs(weight+weig[index], value+tres[index], index+1);
}


int main(void){
	int cnt = 0;
	while(cin >> W, W){
		cnt++;
		cin >> N;
		maxW = 0;
		maxV = 0;
		// fill_n(tres, 1001, 0);
		// fill_n(weig, 1001, 0);
		//fill_n(*dp, 1001*1001, -1);
		dp.clear();

		rep(i,N){
			int n, w;
			scanf("%d,%d", &n, & w);
			tres[i] = n;
			weig[i] = w;
		}

		dfs(0,0,0);
		cout << "Case " << cnt << ":" << endl;
		cout << maxV << endl << maxW << endl;
	}

	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include<stdio.h>
int i,j,l;
int w;
int n;
int kati,omosa;
long long int maxkati,maxomosa,casecnt=0;
long long int dp[1005][1005];//dp[お宝の総数][お宝の重さ]
int main(void){
	scanf("%d",&w);
	while(w!=0){
		for(i=0;i<=1000;i++){
			for(j=0;j<=1000;j++){
				dp[i][j]=0;
			}
		}
		scanf("%d",&n);
		scanf("%d,%d",&kati,&omosa);
		dp[0][omosa]=kati;
		for(j=1;j<=n;j++){
			dp[j][omosa]=dp[0][omosa];
		}
		for(i=1;i<n;i++){
			scanf("%d,%d",&kati,&omosa);
			for(j=i;j<=n;j++){
				if(dp[j][omosa]<kati){
					dp[j][omosa]=kati;
				}
			}
			for(j=1;j<=w;j++){
				if(dp[i-1][j]>0){
					dp[i][j+omosa]=dp[i-1][j]+kati;
					for(l=i;l<=n;l++){
						if(dp[i-1][j+omosa]>dp[l+1][j+omosa]);
						else {
							dp[l][j+omosa]=dp[i][j+omosa];
						}
					}
				}
			}
			for(j=i;j<=w;j++){
				if(dp[i][j+omosa]>dp[j+1][j+omosa]){
					dp[j+1][j+omosa]=dp[i][j+omosa];
				}
			}
		}
		/*for(i=0;i<=n;i++){
			for(j=1;j<=w;j++){
				printf("%lld ",dp[i][j]);
			}
			printf("\n");
		}*/
		maxomosa=0;
		maxkati=0;
		for(j=0;j<=w;j++){
			if(maxkati==dp[n][j]);
			else if(maxkati<dp[n][j] ){
				maxkati=dp[n][j];
				maxomosa=j;
			}
		}
		casecnt++;
		printf("case %lld:\n",casecnt);
		printf("%lld\n",maxkati);
		printf("%lld\n",maxomosa);
		scanf("%d",&w);
	}
	return 0;
}