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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1000;
const int MAXW = 10000;

int val[MAXN],weight[MAXN];
int dp[MAXN][MAXW];
// dp[i][w] .. i番目までを重さwで行った時の最大の価値
// dp[i+1][w] =
//   max(dp[i][w-weight[i+1]] + val[i+1] , dp[i][w])
int main(){
  int N,W,casenum = 1;
  while(scanf("%d",&W) && W != 0){
    //input
    fill(*dp,*(dp+MAXN),0);
    scanf("%d",&N);
    for(int i = 0;i < N;i++){
      scanf("%d,%d",val+i,weight+i);
    }
    ///
    
    ///solve
    for(int i = 0; i < N;i++){
      for(int w = 0;w < W+1;w++){
        int use = (w - weight[i] >= 0
                   ? dp[i][w - weight[i]] + val[i]
                   : 0);
        int unuse = dp[i][w];
        dp[i+1][w] = max(use,unuse);
      }
    }
    
    //
    printf("Case %d:\n",casenum);
    for(int w = 0;w <= W;w++){
      if(dp[N][w] == dp[N][W]){
        printf("%d\n%d\n",dp[N][w],w);
        break;
      }
    }
    casenum++;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
using namespace std;
#define rep2(x,from,to) for(int x = (from); x < (to); ++(x))
#define rep(x,to) rep2(x,0,to)
#define MAX_N 1000
#define MAX_W 1000

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

int main() {
	int W;
	int count = 1;
	while(cin >> W && W) {
		int n;
		cin >> n;
		int v[MAX_N], w[MAX_N];
		rep(i,n) {
			string s;
			cin >> s;
			string t;
			auto it = s.begin();
			for(; *it != ','; ++it) {
				t += *it;
			}
			v[i] = atoi(t.c_str());
			t = "";
			++it;
			for(; it != s.end(); ++it) {
				t += *it;
			}
			w[i] = atoi(t.c_str());
		}
		rep(i, MAX_N + 1) {
			rep(j, MAX_N + 1) {
				dp[i][j] = 0;
			}
		}
		rep2(i,1,n+1) {
			rep(j,W+1) {
				if(j < w[i - 1]) dp[i][j] = dp[i - 1][j];
				else dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i - 1]] + v[i - 1]);
			}
		}
		int ret = W;
		while(1) {
			if(dp[n][ret] != dp[n][ret - 1]) break;
			ret--;
		}
		cout << "Case " << count << ":" << endl;
		cout << dp[n][W] << endl;
		cout << ret << endl;
		count++;
	}
	return 0;
}

C++: #include<cstdio>
int main(void){
	int ww;
	int n;
	int a[1001][1001];
	int w[1001];
	int p[1001];
	int max;
	int mm;
	for (int tt=1;;tt++){
		scanf("%d", &ww);
		if (ww == 0)break;
		scanf("%d", &n);
		for (int i = 0; i<1001; i++){
for(int j=0;j<1001;j++){
a[i][j]=0;
}
}
		for (int i = 1; i <= n; i++){
			scanf("%d,%d", &p[i], &w[i]);
for(int j=0;j<w[i];j++){
a[i][j]=a[i-1][j];
}
			for (int j = w[i]; j <= ww; j++){
				if (a[i - 1][j] > a[i - 1][j - w[i]] + p[i]){
					a[i][j] = a[i - 1][j];
				}
				else{
					a[i][j] = a[i - 1][j - w[i]] + p[i];
				}
			}
		}
		max = 0;
		mm = 0;
		for (int i = 0; i <= ww; i++){
			if (max < a[n][i]){
				max = a[n][i];
				mm = i;
			}
		}
		printf("Case %d:\n%d\n%d\n", tt, max, mm);
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int> P;

int main(){
	int W,N,cou=0;
	int tr[1001],we[1001];
	while(cin >> W , W){
		cou++;
		cin >> N;
		for(int i=1;i<=N;i++){
			scanf("%d,%d",&tr[i],&we[i]);
		}

		P dp[100][1001];
		for(int i=0;i<=N;i++){
			for(int j=0;j<=N;j++){
				dp[i][j] = P(0,0);
			}
		}
		dp[0][0] = P(0,0);
		for(int i=1;i<=N;i++){
			dp[i][0] = P(0,0);
			for(int j=0;j<i;j++){
				if(dp[i-1][j].second+we[i] <= W){
					P p;
					p = P(dp[i-1][j].first+tr[i],dp[i-1][j].second+we[i]);
					if(p.first == dp[i-1][j].first){
						if(p.second < dp[i-1][j].second) dp[i][j+1] = p;
						else dp[i][j+1] = dp[i-1][j];
					}
					else if(p.first > dp[i-1][j].first){
						dp[i][j+1] = p;
					}
					else dp[i][j+1] = dp[i-1][j];
				}
				else dp[i][j+1] = dp[i-1][j];
			}
		}
		int ans1=0,ans2=0;
		for(int i=0;i<=N;i++){
			if(dp[N][i].first == ans1){
				if(dp[N][i].second < ans2) ans2 = dp[N][i].second;
			}
			else if(dp[N][i].first > ans1){
				ans1 = dp[N][i].first;
				ans2 = dp[N][i].second;
			}
		}

		cout << "Case " << cou << ":" << endl;
		cout << ans1 << endl;
		cout << ans2 << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <map>
#include <cmath>
//#include <string>
//#include <sstream>
#include <iomanip>
//#include <complex>
#include <stdio.h>
//小数制度 	cout << fixed << setprecision(5) << tmp << endl;
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)

int dp[1010][1010];
int v[1010];
int w[1010];
int main(){
  int wei,n;
  int cnt = 1;
  while(cin >> wei,wei){
    cin >> n;
    REP(i,1010) REP(j,1010) dp[i][j] = 0;
    w[0]=0;v[0]=0;
    for(int i=1;i<n+1;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<=wei;j++){
        if(j-w[i]<0) dp[i+1][j] = dp[i][j];
        else  dp[i+1][j]=max(dp[i][j+1],dp[i][j-w[i]]+v[i]);
      }
    }
    /*for(int i=0;i<n+1;i++){
      for(int j=0;j<wei+1;j++){
        cout << dp[i][j] << " ";//l<<endl;
      }
      cout << endl;
    }*/
    int maxv=0;
    int ans=-1;
    for(int i=0;i<wei+1;i++){
      if(maxv < dp[n][i]){
        maxv = dp[n][i];
        ans = i;
      }
    }
    cout << "Case " << cnt << ":" <<endl;
    cout << maxv << endl << ans << endl;
    cnt++;
  }
}