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>
using namespace std;

int main(){
	const int nmax = 100;
	char c;
	int dp[nmax+1][nmax+1];
	int n, w;
	int val[nmax+1], weight[nmax+1];
	while(cin >> w){
		if(w == 0) break;
		cin >> n;
		for(int i = 0; i <= n; i++){
			for(int j = 0; j <= w; j++){
				dp[i][j] = 0;
			}
		}
		for(int i = 1; 1 <= n; i++){
			cin >> val[i] >> c >> weight[i];
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j <= w; j++){
				if(j < weight[i]){
					dp[i+1][j] = dp[i][j];
				}else{
					dp[i+1][j] = max(dp[i][j], dp[i][j - weight[i]] + val[i]);
				}
			}
		}
		cout << dp[n][w] << endl;
	}
	return 0;
}

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

#define INF 1e6

using namespace std;

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

int main(void){
	int tem=1;
	int vmax,wmax;
	while(true){
		vmax=0;
		memset(dp,-1,sizeof(dp));
		cin >> W >> N;
		if(!W)
			break;
		dp[0]=0;
		for(int i=0;i<N;i++){
			scanf("%d,%d",&v,&w);
			for(int j=W-w;j>=0;j--){
				if(dp[j]>=0)
					dp[j+w]=max(dp[j+w],(dp[j]+v));
			}
		}
		for(int i=0; i<=W; i++){
			if(dp[i]>vmax){
			vmax=dp[i];
			wmax=i;
			}
		}
		cout << "Case "<<tem<<':'<<endl;
		cout <<  vmax << endl << wmax <<endl;
		tem++;
	}
	return 0;
}

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

int dp[1010][1010];

int main() {
	int w;
	int x = 1;
	while (cin >> w, w) {
		int n;
		cin >> n;
		int v[1005], m[1005];
		for (int i = 0;i < n;i++) {	scanf("%d,%d", &v[i], &m[i]);}

		for (int i = 0;i < n;i++)
			for (int j = 0;j < w;j++)dp[i][j] = -1;
		dp[0][0] = 0;

		for (int i = 0;i < n;i++) {
			for (int j = 0;j <= w;j++) {
				if (dp[i][j] !=-1) {
					dp[i + 1][j] = 0;
					if (j + m[i] <= w&&dp[i+1][j+m[i]]==-1)dp[i + 1][j + m[i]] = 0;
					dp[i + 1][j] += dp[i][j];
					dp[i + 1][j + m[i]] += dp[i][j] + v[i];
				}
			}
		}

		int k=0, aw=0;
		for (int i = w;i > -1;i--) {
			if (dp[n][i] >= k) {
				k = dp[n][i];
				aw = i;
			}
		}
		cout << "Case " << x << ":" << endl;
		cout << k << endl << aw << endl;
		x++;
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <utility>
#define mp make_pair
#define f first
#define s second
#define dm(x) cerr << #x << " = " << x << endl;
using namespace std;

#define rep(i,n) for(int i=0; i<(n); i++)
typedef pair<int,int> pp;

pp dp[1000][1000];
int p[1001],w[1001];
int W, N;

pp operator+(pp a,pp b)
{
  pp ret(a.f+b.f, a.s+b.s);
  return ret;
}

//i番目以降でj以下の重さで価値を最大化。 {最大の価値, その時の重さ}
pp calc(int i,int j)
{
  pp ret;
  if(dp[i][j].f>=0) return dp[i][j];
  if(i==N) ret=mp(0,0);
  else if(j<w[i]) ret=calc(i+1,j);
  else{
    pp a=calc(i+1,j);
    pp b=calc(i+1,j-w[i]) + mp(p[i],w[i]);
    if(a.f>b.f) ret = a;
    else if(a.f<b.f) ret = b;
    else{
      if(a.s > b.s) ret = b;
      else ret = a;
    }
  } 
  return dp[i][j] = ret;
}

int main()
{
  int n=1;
  while(scanf("%d", &W),W){
    scanf("%d", &N);
    rep(i,N) scanf("%d, %d",p+i, w+i);
    // rep(i,N) printf("%d: p:%d w:%d\n",i , p[i], w[i]);
    rep(i,N+1) rep(j,W+1) {dp[i][j]=mp(-1,-1);}
    pp ret=calc(0,W);
    // printf(","); rep(j,W+1) printf("%d,",j); printf("\n");
    // rep(i,N+1){
    //   printf("%d,",i);
    //   rep(j,W+1)
    //     printf("%d,",calc(i,j).f);
    //   printf("\n");
    // }
    // rep(i,N+1){
    //   printf("%d,",i);
    //   rep(j,W+1)
    //     printf("%d,",calc(i,j).s);
    //   printf("\n");
    // }
    
    printf("Case %d:\n%d\n%d\n", n++, ret.f, ret.s);
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define INF (1<<21)
int dp[1001][1001];
pair<int,int> solve(vector<int> value , vector<int> weight , int W , int N){
	for(int i=0;i<1001;i++)
		for(int j=0;j<1001;j++)
			dp[i][j] = 0;
	for(int num=1;num<=N;num++){
		for(int w=0;w <= W;w++){
			dp[num][w] = max( dp[num][w] , dp[num-1][w]);
			if(w >= weight[num])dp[num][w] = max( dp[num][w] , dp[num-1][w-weight[num]] + value[num]);
		}
	}	
	int ans1 = *max_element(dp[N],dp[N]+W+1);
	int ans2 = find(dp[N],dp[N]+W+1,ans1)-dp[N];
	return make_pair(ans1,ans2);
}

int main(){
	int n,w,CaseNum=0;
	while(cin >> w && w){
		cout << "Case " << ++CaseNum << ":" << endl;
		cin >> n;
		vector<int> value(1+n) , weight(1+n);
		for(int i=1;i<=n;i++){
			scanf("%d,%d\n",&value[i],&weight[i]);
		}
		pair<int,int> ans = solve(value,weight,w,n);
		cout << ans.first << endl << ans.second << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int W,N,w[1001],v[1001],ansW=0,ansV=0;
int memo[1001][1001],cou,ans2;
int func(int i,int j,int _v){
    //printf(" %d,%d,%d\n",i,j,_v);
    if(j>W)return 0;
    if(memo[i][j]>_v){
        return 0;
    }
    memo[i][j] = _v;
    if(i==N){
        if(ansV<_v){
            ansV = _v;
            ansW = j;
        }
        else if(ansV==_v && ansW>j){
            ansV = _v;
            ansW = j;
        }
        return _v;
    }
    //ans2=max(ans2,j);
    return max(func(i+1,j,_v),func(i+1,j+w[i],_v+v[i]));
    //ans2=max(ans2,j);
}
int main(){
    int tc=1;
    while(cin>>W,W){
        int ans=0,cou=0;
        cin>>N;
        for(int i=0;i<N;i++){
            scanf("%d,%d",&v[i],&w[i]);
        }
        func(0,0,0);
        cout<<"Case "<<tc<<":"<<endl<<ansV<<endl<<ansW<<endl;;
        tc++;
    }
}

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;
}