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;
int dp[1001][1001],wait[1001],value[1001],waiting[1001][1001];
void Initialization()
{
    for(int i=0;i<1001;i++){
            for(int j=0;j<1001;j++){
                    dp[i][j]=-1;
                    waiting[i][j]=-1;
            }
    }
}
int power(int n)
{
    int num=1;
    for(int i=1;i<=n;i++)num*=10;
    return num;
}
int serch(int i,int j,int n,int co,int w)
{
    int num,wai=w;
    if(co==1)wai-=j;
    if(dp[i][j]>-1)return dp[i][j];
    else if(i==n)num=0;
    else if(j<wait[i])num=serch(i+1,j,n,0,w);
    else num=max(serch(i+1,j,n,0,w),serch(i+1,j-wait[i],n,1,w)+value[i]);
    waiting[i][j]=wai;
    return dp[i][j]=num;
}
int waitserch(int w,int n)
{
    int num=0;
    for(int i=n;i>0;i--){
        for(int j=0;j<=w;j++)if(waiting[i][j]>num&&waiting[i][j]>0&&waiting[i][j]!=w)num=waiting[i][j];
    }
    return num;
}
int main()
{
    int n,w,point,num=1,wai=0;
    string str;
    while(cin>>w&&w!=0){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>str;
            value[i]=0;wait[i]=0;
            for(int j=0;;j++){
                if(str[j]==','){
                    for(int k=0;k<j;k++)value[i]+=power(j-k-1)*(str[k]-'0');
                    point=j;
                }
                if(str[j]=='\0'){
                    for(int k=point+1;k<j;k++)wait[i]+=power(j-k-1)*(str[k]-'0');
                    break;
                }
            }
        }
        Initialization();
        point=serch(0,w,n,0,w);
        wai=waitserch(w,n);
        cout<<"Case "<<num<<":\n"<<point<<endl<<wai<<endl;
        num++;
    }
}

C++: #include <cstdio>
#include <algorithm>
#define MAX_N 1000
#define MAX_W 1000
using namespace std;
int W;
int N; 

int w[MAX_N]; 
int v[MAX_N]; 
int dp[MAX_N + 1][MAX_W + 1];
bool input() 
{
  scanf("%d", &W);
  if(!W) return false;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) 
  {
    scanf("%d,%d", &v[i], &w[i]);
  }
  return true;
}
void solve() 
{

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

int main() 
{

  int cnt = 0;

  while (input())
  {
    solve();
    int i;
    for (i = W; i >= 0; i--) 
    {
      if (dp[N][W] != dp[N][i]) break;
    }
    printf("Case %d:\n", ++cnt);
    printf("%d\n", dp[N][W]);
    printf("%d\n", i + 1);
  }

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int main(){
	int W,n,temp,num=1,q,p;
	int dp[2000];
	int w[1005],v[1005];
	while(cin>>W,W){
		cin>>n;
		for(int i=0;i<n;i++)
		scanf("%d,%d",&v[i],&w[i]);
		for(int i=0;i<=W;i++)
		dp[i]=0;
		for(int i=0;i<n;i++){
			for(int j=W;j>=w[i];j--){
				dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
			}
		}
		temp=W;
		while(dp[temp]==dp[W])
			temp--;
		cout<<"Case "<<num<<':'<<endl;
		cout<<dp[W]<<endl;
		cout<<temp+1<<endl;
		num++;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define  req(i,r,k) for(int i=k;i<r;i++)
#define  rep(i,r) req(i,r,0)

long long int dp[1001];

int main() {

    for(int a=1;1;a++){
        int w,n;
        scanf(" %d",&w);
        if(w==0) break;
        scanf(" %d",&n);
        pair<int,int> t[n];
        char c;
        rep(i,n){
            scanf(" %d%c%d",&t[i].first,&c,&t[i].second);
        }

        rep(i,w){
            dp[i] = 0;
        }

        dp[0] = 0;
        for(int i=0;i<n;i++) {
            for (int j = w; j != 0; j--) {
                if(j > t[i].second) {
                    dp[j] = max(dp[j], dp[j - t[i].second] + t[i].first);
                }
            }
        }
        long long int maxvalue=0,thenweight=0;

        rep(i,w+1){
            //cout << dp[i] << " " << i << " " << thenweight << endl;
            if(maxvalue < dp[i]){
                maxvalue = dp[i];
                thenweight = i;
            }
        }

        printf("Case %d:\n%lld\n%lld\n",a,maxvalue,thenweight);
    }
    return 0;
}

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

#define MAX 1001

int dp[MAX][MAX];
int v[MAX], w[MAX];

int main() {
	int W, n, u = 0;
	char t;
	while (cin >> W) {
		if (W == 0) break;
		cin >> n;
		u++;

		dp[MAX + 1][MAX + 1] = { 0 };
		for (int i = 0; i < n; i++) {
			cin >> v[i] >> t >> w[i];
		}

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

		int mv = 0, mw;
		for (int i = 0; i <= W; i++) {
			if (dp[n][i] > mv) {
				mv = dp[n][i];
				mw = i;
			}
		}

		cout << "Case " << u << ":" << endl;
		cout << mv << endl;
		cout << mw << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
main(){
  int b,c,N,K[1010],P[1010],i,m,j,W,dp[1010],h=1,y,g;
  char d;
  while(1){
    y=0;
    b=0;
    cin >> W;
    if(W==0){
      break;
    }
    for(i=0;i<=W;i++){
      dp[i]=0;
    }
    cin >> N;
    for(i=0;i<N;i++){
      cin >> P[i] >> d >> K[i];
      c=K[i];
      dp[K[i]]=P[i];
      for(j=W;j>=1;j--){
	if(j!=c){
	if(dp[j]!=0){
	  if(dp[c+j]!=0){
	    if(dp[c+j]<dp[c]+dp[j]){
	      dp[c+j]=dp[c]+dp[j];
	    }
	    else{
	      continue;
	    }
	  }
	  if(dp[c+j]==0){
	    dp[c+j]=dp[c]+dp[j];
	  }
	}
      }
    }
    }
    for(i=1;i<=W;i++){
      cout << dp[i] << endl;
      if(dp[i]>b){
	y=i;
	b=dp[i];
      }
      else{
	continue;
      }
    }
    cout << "Case" << " " << h << ":" << endl;
    cout << b << endl << y << endl;
    h++;
  }
}