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 <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define MAX_N 5001
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<string, ll> PS;
typedef vector<ll> V;

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;


int co = 1;
char k;
int W, N, v[10000], w[10000];

ll dp[1010][10000];//i???????????§???????????§?????????j??\?????¨?????????????????????????????§???

int main(){
	while (cin >> W&&W){
		cin >> N;
		REP(i, N)cin >> v[i] >> k >> w[i];
		REP(i, N){
			REP(j, W + 1){
				if (j >= w[i]){
					dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + v[i]);
				}
				else{
					dp[i + 1][j] = dp[i][j];
				}
			}
		}
		int max = -1, ans = -1;
		REP(i, W + 1){
			if (max < dp[N][i]){
				max = dp[N][i];
				ans = i;
			}
		}
		printf("Case %d:\n", co++);
		cout << max << endl;
		cout << ans << endl;
		REP(i, N + 1)REP(j, W + 1)dp[i][j] = 0;
	}
}

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

#define MAX_N 1001
#define MAX_W 10001

int dp[MAX_W][MAX_N], W, N, value[MAX_N], w[MAX_N];

int main(){
    int Case = 0;
    
    while(true){
        for(int i = 0; i < MAX_N; ++i)
            memset(dp[i], 0, MAX_W);
        //initialize
        
        ++Case;
        scanf("%d%d", &W, &N);
        if(!W)
            break;
        for(int i = 0; i < N; ++i)
            scanf("%d,%d", &value[i], &w[i]);
        //input
        
        for(int i = 0; i < N; ++i){
            for(int j = 0; j <= W; ++j){
                if(j < w[i])
                    dp[i + 1][j] = dp[i][j];
                else
                    dp[i + 1][j] = std::max(dp[i][j - w[i]] + value[i], dp[i][j]);
            }
        }
        //solve
        
        int ans = dp[N][W];
        
        printf("Case %d:\n%d\n", Case, ans);
        
        int i = 0;
        while(true){
            if(ans != dp[N][W - i]){
                printf("%d\n", W - i + 1);
                break;
            }
            ++i;
        }
    }
    return 0;
}

C++: #include<stdio.h>
int a,b,c[1001][1001],d[1001],e[1001],m,mm,i,j;
int main(void)
{
	scanf("%d",&a);
	while(a!=0){
		m=0;
		mm=0;
		scanf("%d",&b);
		for(i=0;i<=b;i++){
			for(j=0;j<=a;j++){
				c[i][j]=-1;
				d[i]=0;
				e[i]=0;
			}
		}
		for(i=0;i<b;i++){
				scanf("%d,%d",&d[i],&e[i]);
		}
		c[0][0]=0;
		for(i=0;i<=b;i++){
			for(j=0;j<=a;j++){
				if(c[i][j]>=0 && c[i][j]+d[i]>c[i+1][j+e[i]] && j+e[i]<=a){
					c[i+1][j+e[i]]=c[i][j]+d[i];
				}
				if(c[i][j]>=0 && c[i+1][j]<c[i][j]){
					c[i+1][j]=c[i][j];
				}
			}
			
		}
		for(j=0;j<=a;j++){
			if(c[b][j]>m){
				m=c[b][j];
				mm=j;
				//printf("c=%d j=%d\n",c[b][j],j);
			}
		}
		/*for(i=0;i<=b;i++){
			for(j=0;j<=a;j++){
				printf("%4d",c[i][j]);
			}
			printf("\n");
		}
		printf("%d\n",c[b][49]);*/
		printf("%d\n%d\n",m,mm);

		scanf("%d",&a);
	}	
	return 0;
}

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

int main() {
	int w_lim;
	for (int t = 1; cin >> w_lim && w_lim; t++) {
		int n;
		cin >> n;
		int v[n], w[n];
		char c;
		for (int i = 0; i < n; i++) {
			cin >> v[i] >> c >> w[i];
		}
		int dp[n + 1][w_lim + 1]{};	//dp[i][j] := 先頭i個見て重さがjの時の価値の最大値
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= w_lim; j++) {
				dp[i + 1][j] = dp[i][j];
				if (0 <= j - w[i]) {
					dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - w[i]] + v[i]);
				}
			}
		}
		cout << "Case " << t << ':' << endl;
		pair<int, int> ans(0, 0);
		for (int i = 0; i <= w_lim; i++) {
			if (ans.first < dp[n][i]) {
				ans = {dp[n][i], i};
			}
		}
		cout << ans.first << endl;
		cout << ans.second << endl;
	}
	return 0;
}