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 <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#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 repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w,n;
  int cnt = 1;
  char c;
  while(cin >> w){
    if(w == 0) break;
    cin >> n;
    vary(int,vs,n,0);
    vary(int,ws,n,0);
    vector<vector<pii>> dp(n+1,vector<pii>(w+1,make_pair(0,0)));
//     vector<vector<int>> dp2(n+1,vector<int>(w+1,0));
    rep(i,n){
      cin >> vs[i] >> c >> ws[i];
    }
    REP(i,0,n){
      rep(j,w+1){
        if(j <ws[i]){
          dp[i+1][j] = dp[i][j];
//           dp2[i+1][j] = dp2[i][j];
        }
        else{
          pii tmp;
          if(dp[i][j].fi < dp[i][j-ws[i]].fi+vs[i] && j != w){
            int k = dp[i][j-ws[i]].fi+vs[i];
            tmp = make_pair(k,j);
          }
          else{
            tmp = dp[i][j];
          }
          dp[i+1][j] = tmp;
//           dp2[i+1][j] = max(dp2[i][j],dp2[i][j-ws[i]]+vs[i]);
        }
      }
    }
    cout << "Case " + to_string(cnt) + ":"<<endl;
    cout << dp[n][w-1].fi <<endl;
    cout << dp[n][w-1].se <<endl;
    ++cnt;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 1001;

int dp[N];
int cnt;

int main(void){
    for(int ww; cin >> ww, ww;){
        int n; cin >> n;
        rep(i, N) dp[i] = -1;
        dp[0] = 0;
        while(n--){
            int p, w;
            char c; cin >> p >> c >> w;

            COUNT(i, N){
                int cur = N - i;
                if(~dp[cur] && cur + w <= ww) dp[cur + w] = max(dp[cur + w], dp[cur] + p);
            }
        }
        int ind = max_element(dp, dp + N) - dp;
        cout << "Case " << ++cnt << ":" << endl << dp[ind] << endl << ind << endl;
    }

    return 0;
}

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

int pri[1010],wai[1010],dp[2][1010];
int main(){
	int W,N,cas=0;
	while(true){
		cas++;
		cin>>W;
		if(W==0) break;
		cin>>N;
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&pri[i],&wai[i]);
		}
		fill(dp[0],dp[0]+W+1,0);
		fill(dp[1],dp[1]+W+1,0);
		int in=0,ou=1;
		for(int i=1;i<=N;i++){
			fill(dp[ou],dp[ou]+W+1,0);
			for(int j=0;j<=W;j++){
				if(j+wai[i]<=W){
					dp[ou][j+wai[i]]=max(dp[ou][j+wai[i]],dp[in][j]+pri[i]);
				}
			}
			swap(in,ou);
		}
		int res=0,cnt=-1;
		for(int i=0;i<=W;i++){
			if(res<dp[in][i]){
				res=dp[in][i];
				cnt=i;
			}
		}
		cout<<"Case "<<cas<<":"<<endl<<res<<endl<<cnt<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <utility>

using namespace std;

pair<int, int> napsacDP(int capacity, int n,
    const vector<pair<int, int>>& contents) {
  vector<vector<int>> dp(n+1, vector<int>(capacity+1));
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= capacity; ++j) {
      dp[i][j] = dp[i-1][j];
      if (j >= contents[i-1].second) {
        dp[i][j] = max(dp[i][j],
            dp[i-1][j-contents[i-1].second]
                + contents[i-1].first);
      }
    }
  }
  int maxval = 0;
  int maxweight = 0;
  for (int i = 0; i <= capacity; ++i) {
    if (maxval < dp[n][i]) {
      maxval = dp[n][i];
      maxweight = i;
    }
  }
  return make_pair(maxval, maxweight);
}

int main() {
  int count = 1;
  while(1) {
    int w,n;
    cin >> w;
    if (w == 0) break;
    cin >> n;
    vector<pair<int, int>> contents;
    for (int i = 0; i < n; ++i) {
      int value, weight;
      scanf("%d,%d", &value, &weight);
      contents.emplace_back(value, weight);
    }
    auto result = napsacDP(w, n, contents);
    cout << "Case " << count << ":" << endl;
    cout << result.first << endl;
    cout << result.second << endl;
    ++count;
  }
  return 0;
}

C++: #include <stdio.h>

int dp[1024][10010];

int main(void)
{
	int i, j;
	int n;
	int wei;
	int w[1024];
	int k[1024];
	int b;
	int max;
	int mw;
	
	b = 0;
	while (scanf("%d", &wei), wei != 0){
		scanf("%d", &n);
		for (i = 0; i <= n; i++){
			if (i < n){
				scanf("%d,%d", &k[i], &w[i]);
			}
			for (j = 0; j < 10000; j++){
				dp[i][j] = 0;
			}
		}
		for (i = 0; i < n; i++){
			for (j = 0; j <= wei; j++){
				if (dp[i + 1][j + w[i]] < dp[i][j] + k[i]){
					dp[i + 1][j + w[i]] = dp[i][j] + k[i];
				}
				if (dp[i + 1][j] < dp[i][j]){
					dp[i + 1][j] = dp[i][j];
				}
			}
		}
		max = 0;
		for (i = 0; i < wei; i++){
			if (max < dp[n][i]){
				max = dp[n][i];
				mw = i;
			}
		}
		b++;
		printf("Case %d:\n", b);
		printf("%d\n%d\n", max, mw);
	}
	return (0);
}