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>
#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;
int dp[1001][1001];

void dfs(int weight, int value, int index){
	if(weight > W)return;
	if(dp[weight][index]>value)return;
	dp[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);

		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<cstdio>
#include<iostream>
using namespace std;

typedef struct{
  int g;
  int ye;
}SET;

int main(){
  
  int n,w;
  int cnt=1;
  while(1){
    
    scanf("%d",&w);
    if(w==0)break;
    scanf("%d",&n);
    SET data[1111]={0}; 
    int memo[1111][1111]={{0}};
    int max_y=0;
    int max_g;
    
    for(int i=0;i<n;i++){
      scanf("%d,%d",&data[i].ye,&data[i].g);
    }
    memo[0][data[0].g]=data[0].ye;
    for(int i=1;i<n;i++){
      if(data[i].ye>memo[i-1][data[i].g]){
	memo[i][data[i].g]=data[i].ye;
      }
      for(int j=1;j<w;j++){
	if(memo[i-1][j]>0){
	  if(memo[i-1][j]>memo[i][j]){
	    memo[i][j]=memo[i-1][j];
	  }
	  if(j+data[i].g<=w){
	    memo[i][j+data[i].g]=memo[i-1][j]+data[i].ye;
	  }
	}
	if(max_y<memo[i][j]){
	  max_y=memo[i][j];
	  max_g=j;
	}
      }    
    }
    
    printf("Case %d:\n%d\n%d\n",cnt,max_y,max_g);
    cnt++;
  }
}

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

void solve(int c,int n,int limit) {
	int i, j, t;
	int w[1000] = {}, v[1000] = {};
	static int dp[1001][1001];

	memset(dp, 0, sizeof(dp));

	for (i = 0;i < n;i++) {
		scanf("%d,%d", &v[i], &w[i]);
		//cin >> v[i] >> w[i];
	}

	for (i = 1;i <= n;i++) {
		for (j = 0;j <= limit;j++) {
			t = j - w[i-1];
			if (t >= 0&&dp[i-1][j]<dp[i-1][t]+v[i-1]) {
				dp[i][j] = dp[i - 1][t] + v[i - 1];
			}
			else {
				dp[i][j] = dp[i - 1][j];
			}
		}
	}

	for (i = 0;i <= limit;i++) {
		if (dp[n][i] == dp[n][limit]) {
			break;
		}
	}

	printf("Case %d:\n%d\n%d\n",c, dp[n][i], i);

	return;
}

int main(void) {
	int c, n, limit;

	c = 1;
	while (true) {
		cin >> limit;
		if (limit == 0) {
			break;
		}
		cin >> n;
		solve(c, n, limit);
		c++;
	}
	
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)

static const double PI=6*asin(0.5);
typedef complex<double> P;
typedef long long ll;


int m;
int w,n;
int dp;
char t;
vector<pair <int,int> > pa;
pair<int,int> tpa;
pair <pair <int,int> , int> MAX;

int foo(int w,int v,int c,int d){
	if(c==pa.size()){
		tpa=MAX.first;
		if(w<=m&&MAX.second>=d&&tpa.second==v){
			tpa.first=w;
			tpa.second=v;
			MAX.first=tpa;
			MAX.second=d;
		}
		else if(w<=m&&tpa.second<v){
			tpa.first=w;
			tpa.second=v;
			MAX.first=tpa;
			MAX.second=d;
		}
	}
	else{
		foo(w+pa[c].first,v+pa[c].second,c+1,d+1);
		foo(w,v,c+1,d);
	}
	return 0;
}

int main(){
	int e=1;
	while(cin>>m,m!=0){
		cout<<"Case "<<e++<<":\n";
		tpa.first=0;
		tpa.second=0;
		MAX.first=tpa;
		MAX.second=0;
		cin>>n;
		rep(i,n){
			int t1,t2;
			cin>>t1>>t>>t2;
			pair <int,int> tm;
			tm.first=t2;
			tm.second=t1;
			pa.push_back(tm);
		}
		//rep(i,pa.size())cout<<pa[i].first<<" "<<pa[i].second<<endl;
		foo(0,0,0,0);
		tpa=MAX.first;
		cout<<tpa.second<<endl<<tpa.first<<endl;
		pa.clear();
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<cstdio>
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a; i ++)
int main(void){
  int w;
  int n;
  int num=0;
  while(cin>>w,w){
    cout<<"Case "<<++num<<":"<<endl;
    cin>>n;
    vector<pair<int,int> > in(n);
    rep(i,n){
      int a,b;
      scanf("%d,%d",&a,&b);
      in[i]=make_pair(a,b);
   }    
    int resw = 0;
    int resm = 0;
    int last = 1<<n;
    for(int i = 1 ; i < last+1 ; i ++){
      //cout<<i<<endl;
      int nw = 0;
      int money = 0;
      rep(j,n){
	bool use = (i/(1<<j))%2;

	//cout<<use<<" ";
	if(use){
	  money += in[j].first;
	  nw += in[j].second;
	}
      }
      if(nw>=w){
	continue;
      }
      
      //cout<<endl;
      if(resm<money){
	//cout<<endl;
	  
	resm = money;
	resw = nw;
      }
      if(money==resm && resw>nw){
	resw = nw;
      }
      //cout<<i<<" "<<resw<<endl;
      //cout<<endl;
    }
    cout<<resm<<endl<<resw<<endl;

  }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int dp[1001][1001]={0};
int main()
{
    int c=0;
    while(1)
    {
        int W;
        cin>>W;if(!W)return 0;
        int n;
        cin>>n;
        int a[1001],w[1001];
        for(int i=1;i<=n;++i){
            cin>>a[i];
            cin.ignore();
            cin>>w[i];
        }
        for(int j=1;j<=n;++j){
            for(int i=0;i<=W;++i){
                if(i<w[j]){
                    dp[i][j]=dp[i][j-1];
                }else{
                    dp[i][j]=max(dp[i][j-1],dp[i-w[j]][j-1]+a[j]);
                }
            }
        }
        c++;
        cout<<"Case"<<" "<<c<<":"<<endl;
        cout<<dp[W][n]<<endl;
        bool flag=0;
        for(int j=1;j<=n;++j){
            for(int i=0;i<=W;++i){
                if(dp[i][j]==dp[W][n]){
                    cout<<i-1<<endl;
                    flag=1;
                    break;
                }
            }
            if(flag)break;
        }
    }
    return 0;
}