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 <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>
#include <ctime>
#define INF 100000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#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 all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;

/*---------------------------------------------------*/

int W,N;
int v[1005],w[1005];
int dp[1005][1005];

void DEBUG(){
  rep(i,W+1)cout<<i<<": "<<dp[N][i]<<endl;
}

void init(){
  rep(i,1005){
    rep(j,1005){
      dp[i][j]=-INF;
    }
  }
}

void DP(){
  for(int i=0;i<1005;i++)dp[0][i]=0;
  for(int i=0;i<=N;i++){
    for(int j=0;j<=W;j++){
      if(j>=w[i])dp[i+1][j]=max(dp[i][j-w[i]]+v[i],dp[i][j]);
      else dp[i+1][j]=dp[i][j];
    }
  }
}

int main(){
  int cnt=1;
  while(cin>>W){
    init();
    if(W==0)break;
    cin>>N;
    rep(i,N)scanf("%d,%d",&v[i],&w[i]);
    DP();
    //DEBUG();
    cout<<"Case: "<<cnt<<":"<<endl;
    cout<<dp[N][W]<<endl;
    for(int i=W;i>=1;i--)if(dp[N][i]!=dp[N][i-1]||i==1){cout<<i<<endl;break;}
    cnt++;
  }
  
  return 0;
}

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

using namespace std;

int main(void){
    int weigh;
    int w[1001],v[1001];
    int dp[1001][1001];
    int n;
    int c=0;
    
    while(cin>>weigh,weigh!=0){
        for(int i=0;i<1001;i++){
            memset(dp[i],0,1001);
        }
    
        cin>>n;
        for(int i=0;i<n;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
    
        for(int i=0;i<n;i++){
            for(int j=0;j<=weigh;j++){
                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]);
                }
            }
        }
        printf("Case %d:\n",++c);
        printf("%d\n",dp[n][weigh]);
        for(int i=0;i<=weigh;i++){
            if(dp[n][weigh]==dp[n][i]){
                printf("%d\n",i);
                break;
            }
        }
    }
    return(0);
}

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

int parse(string a){
int s=0;
for(char b:a){
	s*=10;
	s+=b-48;
}
return s;
}
vector<int>split(string a){
	vector<int>b;
	string c="";
	for(char d:a){
		if(d==','){b.push_back(parse(c));c="";}
		else c+=d;
	}
	b.push_back(parse(c));
	return b;
}
int main() {
	int u=1;
 int a,b;
 while(cin>>b,b){
 	cin>>a;
 vector<vector<int>>dp(a+1,vector<int>(b+1));
 for(int c=0;c<a;c++){
 	string q;
 	cin>>q;
 	int d,e;
 	d=split(q)[0];
 	e=split(q)[1];
 	for(int f=0;f<b+1;f++){
 		if(f<e)dp[c+1][f]=dp[c][f];
 		else dp[c+1][f]=max(dp[c][f],dp[c][f-e]+d);
 	}
 }
 cout<<"Case"<<u<<":"<<endl;
 int y=0,z=0;
 for(int x=0;x<b+1;x++){
 	if(dp[a][x]>y){y=dp[a][x];z=x;}
 }
 cout<<y<<endl<<z<<endl;
 u++;
 }
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int dp[1001][1001];
  int n,W;
  int v[1000],w[1000];
  int num=0;
  char c;
  int cnt=0;
  while(1){
    cin>> W;
    if(W==0) break;
    cin>> n;
    cnt++;
    for(int i=0;i<=n;i++){
      for(int j=0;j<=W;j++){
	dp[i][j]=0;
      }
    }
    for(int i=1;i<=n;i++){
      scanf("%d,%d",&v[i],&w[i]);
    }

    for(int i=1;i<=n;i++){
      for(int j=1;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]);
	}
      }
    }

    int ansv=0,answ=0;
    for(int i=1;i<=W;i++){
      if(ansv<dp[n][i]){
	ansv=dp[n][i]; answ=i;
      }
    }
    printf("Case %d:\n%d\n%d\n",cnt,ansv,answ);
  }
}

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;
	}
	if(dp[weight][index+1]<value)dfs(weight, value, index+1);
	if(weight+weig[index] > W)return;
	if(dp[weight+weig[index]][index+1]<value+tres[index])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();
}