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;

long long dp[1000];
int main(){
	long long w, n, ans_w = 0, ans_val = 0;
	for (int dataset = 1; cin >> w, w; dataset++){
		long long t_w, t_val;
		cin >> n;
		for (long long i = 0; i < w; i++) dp[i] = -1;
		dp[0] = 0;
		for (long long i = 0; i < n; i++){
			scanf("%lld,%lld", &t_val, &t_w);
			for (long long j = w - 1; j >= 0; j--){
				//printf("%d ", dp[j]);
				if (dp[j] == -1 || j + t_w >= w || dp[j + t_w] > dp[j] + t_val)
					continue;
				dp[j + t_w] = dp[j] + t_val;
			}
			//puts("");
		}
		for (long long i = 0; i < w; i++){
			//printf("%d ", dp[i]);
			if (ans_val < dp[i]){
				ans_val = dp[i];
				ans_w = i;
			}
		}
		//puts("");
		printf("Case %d:\n%lld\n%lld\n", dataset, ans_val, ans_w);
	}
	return 0;
}

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

#define W 1000
#define N 1000

int w, n, vi, wi, maxv, maxw;
int dp[N+1][W+1];
int values[N+1];
int weights[N+1];

int main(){
    int caseNum = 1;
    while(1){
        scanf("%d", &w);
        if(w == 0)  break;

        scanf("%d\n", &n);
        memset(dp, 0, sizeof(dp));
        memset(values, 0, sizeof(values));
        memset(weights, 0, sizeof(weights));

        for(int i = 0; i < n; i++){
            scanf("%d,%d\n", &values[i+1], &weights[i+1]);
        }

        for(int i = 1; i <= n; i++){
            for(int j = 0; j <= w; j++){
                if(j >= weights[i]){
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j-weights[i]]+values[i]);
                }else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }

        maxv = -1;
        maxw = -1;
        for(int i = 0; i <= w; i++){
            if(maxv < dp[n][i]){
                maxv = dp[n][i];
                maxw = i;
            }
        }

        printf("Case %d:\n%d\n%d\n", caseNum, maxv, maxw);
        caseNum++;
    }
    
    return 0;
}

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

const int MAX_N = 1001;
const int MAX_W = 1001;

int t;
int n, W;
int v[MAX_N], w[MAX_N];

int dp[MAX_N][MAX_W];

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

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

#define rep(i,n) for(int i=0; i<n; i++)
int dp[1001][1001];

int main(){
	int W, cnt=1;
	while( cin >> W, W ){
		int n, v[1000], w[1000];
		memset( dp, -1, sizeof(dp) );
		cin >> n;
		rep(i,n){
			scanf("%d,%d", &v[i], &w[i]);
		}
		dp[0][0] = 0;
		rep(i,n){
			rep(j,W+1){
				if( j<w[i] )
					dp[i+1][j] = dp[i][j];
				else
					dp[i+1][j] = max( dp[i][j], dp[i][j-w[i]]+v[i] );
			}
		}
		int mw = 0;
		rep(i,W){
			if( dp[n][i]>dp[n][mw] )	mw = i;
		}
		cout << "Case " << cnt << ":" << endl;
		cnt++;
		cout << dp[n][mw] << endl;
		cout << mw << endl;
	}
}

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 <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fr first
#define sc second
using namespace std;

int w,n;
typedef pair<int,int> P;
P dp[1001][1001];
bool chg[1001][1001];
vector <P> dat;

P rec(int idx,int wi){
    P p;
    if(chg[idx][wi]) return dp[idx][wi];
    if(idx == n){
        p = P(0,wi);
    }
    else{
        if(wi - dat[idx].sc >= 0){
            p = rec(idx+1,wi-dat[idx].sc);
            p.first += dat[idx].fr;
            p = max(p,rec(idx+1,wi));
        }
        else p = rec(idx+1,wi);
    }
    chg[idx][wi] = true;
    return dp[idx][wi] = p;
}

int main(){
    while(cin >> w,w){
        int a = 0;
        memset(dp,-1,sizeof(dp));
        memset(chg,false,sizeof(chg));
        cin >> n;
        for(int i=0;i<n;i++){
            int t,m; scanf("%d,%d",&t,&m); dat.push_back(P(t,m));
        }
        P p = rec(0,w);
        cout << "Case " << ++a << ":\n" << p.fr << endl << w - p.sc << endl;
        dat.clear();
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);++i)
#define rep(i,n) REP(i, 0, n)

int dp[1001];
int main() {
    int W, N; int T = 0;
    while(~scanf("%d%d", &W, &N) && W) {
        memset(dp, 0, sizeof(dp));
        rep(i, N) {
            int v, w; scanf("%d,%d", &v, &w);
            for(int j = W; j - w >= 0; --j)
              dp[j] = max(dp[j], dp[j - w] + v);
        }
        pair<int, int> ans(0, 0);
        rep(i, W + 1) ans = max(ans, make_pair(dp[i], -i));
        printf("Case %d:\n%d\n%d\n", ++T, ans.first, -ans.second);
    }
    return 0;
}

C++: const int MAX_N = 1000; // n????????§???
const int MAX_W = 5000; // W????????§???

// ??\???
int n, W;
int w[MAX_N], v[MAX_N];

// DP????????????
// dp[i][j]???i????????\?????????????????????????????????j??\?????????????????????????????¨??????????????????????????§????????¨??????
int dp[MAX_N + 1][MAX_W + 1];

void solve_dp2() {
  for (int j = 0; j <= W; j++) {
    dp[n][j] = 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 + 1][j];
      else
        dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
    }
  }
  cout << dp[0][W] << endl;
}