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 <vector>
#include <algorithm>
using namespace std;

int main(){
	int w;
	const int maxw = 1000;
	int count = 1;
	int cost[maxw + 1];
	int best[maxw + 1];
	for(int i = 0; i <= maxw; i++){
		cost[i] = 0;		
		best[i] = 0;
	}
	while(cin >> w){
		if(w == 0) break;
		int n;
		int values[maxw + 1];
		int weights[maxw + 1];
		cin >> n;
		for(int i = 0; i < n; i++){
			char c;
			cin >> values[i] >> c >> weights[i];
		}
		for(int j = 0; j < n; j++){
			for(int k = 1; k <= w; k++)
				if(k >= weights[j])
					if(cost[k] < cost[k - weights[j]] + values[j])
					{
						cost[k] = cost[k - weights[j]] + values[j];
						best[k] = j;
					}
		}
		cout << "Case" << " " << count++ << ":" << endl;
		for(;w>0; w--) if(cost[w] != cost[w-1]) break;
		cout << cost[w] << endl;
		int total = 0;
		for(int l = w; l - weights[best[l]] >= 0;){
			if(l - weights[best[l]] >= 0){
				total += weights[best[l]];
				l -= weights[best[l]];
			}
		}
		cout << total << endl;
		for(int i = 0; i <= w; i++){
			cost[i] = 0;
			best[i] = 0;
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

int W, N, value, weight, num = 1;
int v[1000], w[1000];
int dp[1000][1000]; //[n][w]
	
int main(){
	while(true){
		scanf("%d", &W);
		if(W == 0) break;
		scanf("%d", &N);
		for(int i = 0; i < N; i++){
			scanf("%d,%d", &v[i], & w[i]);
		}
		for(int i = 0; i < 1000; i++){
			for(int j = 0; j < 1000; j++){
				dp[i][j] = 0;
			}
		}
//		cout << "aa" <<endl;
		for(int i = N - 1; i >= 0; i--){
			for(int j = 0; 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]);
			}
		}
		/*for(int i = 0; i < N; i++){
				cout << v[i] << " " << w[i] << endl;
		}
		for(int i = 0; i <= N; i++){
			for(int j = 0; j < 109; j++){
				cout << dp[i][j] << " " ;
			}
			cout << endl;
		}*/
		value = 0;
		weight = 0;
		for(int j = 0; j < W; j++){
			if(value < dp[0][j]){
				value = dp[0][j];
				weight = j;
			}
		}
		cout << "Case " << num << ":" << endl;
		cout << value << endl << weight << endl;
		num++;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <random>
using namespace std;

#define REP(i,n) for(int (i)=0; (i)<(n); (i)++)
#define FOR(i,a,b) for(int (i)=(a); (i)<(b); (i)++)

int caseId = 1;

void solve42(int W) {
    cout << "Case " << caseId << ":" << endl;
    int N;
    cin >> N;
    REP(i,N) {
        int dp[1001] = {0}; // dp[i]: maxvalue under weight i
        int v, w;
        scanf("%d,%d", &v, &w);
        for(int j = W; j >= w; j--) {
            dp[j] = max(dp[j], dp[j-w] + v);
        }
    }
    int value = 0, weight = 0;
    REP(i,W) {
        if (value < dp[i]) {
            value = dp[i];
            weight = i;
        }
    }
    cout << value << endl << weight << endl;
}

int main() {
    int W;
    while (cin >> W, W) {
        solve42(W);
        caseId++;
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll dp[2][1005];

int main() {
	int W, N, wei[1005], cost[1005],t,s,r=0;
	while (cin >> W >> N, W) {
		r++;
		t = N  % 2;
		fill(dp[0], dp[0] + N, 0);
		fill(dp[1], dp[1] + N, 0);
		for (int i = 0; i < N; i++) {
			scanf("%d,%d", &wei[i], &cost[i]);
		}
		for (int i = 0; i < N; i++) {
			for (int w = 0; w <= W; w++) {

				if (w - wei[i] >= 0) dp[(i + 1) % 2][w] = max(dp[i][w], dp[i][w - wei[i]] + cost[i]);
				
			}
		}
		for (int i = 0; dp[t][W - i]==dp[t][W]; i++) {
			s = i;
		}
		cout << "Case " << r << ":" << endl;
		cout << dp[t][W] << endl;
		cout << W-s << endl;
	}
}

C++: #include<iostream>
#include<stdio.h>

std::pair<int,int> dp[1000][10000];
int W;
int n;
int v[1000], w[1000];
int now = 1;

int main()
{
	while( 1 ){
		std::cin >> W;
		if( !W ) break;
		std::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 <= W; j++ ){
				if( j < w[i] ){
				 	dp[i+1][j] = dp[i][j];
				}
				else{
				 	dp[i+1][j].first = std::max( dp[i][j].first, dp[i][j-w[i]].first + v[i] );
					
					if( dp[i][j].first > dp[i][j-w[i]].first + v[i] ){
						dp[i+1][j].second = dp[i][j].second;
					}
					else{
					     	dp[i+1][j].second = dp[i][j-w[i]].second + w[i];
					}
				}
			}
		}
		
		std::cout << "Case " << now << ":" << std::endl;
		std::cout << dp[n][W].first << std::endl;
		std::cout << dp[n][W].second << std::endl;
		
		now++;	
	}
	

 	return 0;
}

C++: #include<iostream>
#include<string>
int dp[1000][1000];
using namespace std;
int main(){
	int t,n;
	int w[2000],v[2000];
	int k=0;
	char z;
	while(true){
	cin>>t;
	if(t==0)
		break;
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<t;j++){
			dp[i][j]=-1;
		}
	}
	for(int i=0;i<n;i++){
		cin>>v[i]>>z>>w[i];
		dp[i][0]=0;
	}
	dp[0][w[0]]=v[0];
	for(int i=1;i<n;i++){
		for(int j=1;j<=t;j++){
			if(j-w[i]<0){
				if(dp[i-1][j]==-1)
					dp[i][j]=-1;
				if(dp[i-1][j]!=-1)
					dp[i][j]=dp[i-1][j];
			}
			if(j-w[i]>=0){
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]==-1)
					dp[i][j]=-1;
				if(dp[i-1][j]==-1&&dp[i-1][j-w[i]]!=-1)
					dp[i][j]=dp[i-1][j-w[i]]+v[i];
				if(dp[i-1][j]!=-1&&dp[i-1][j-w[i]]==-1)
					dp[i][j]=dp[i-1][j];
				if(dp[i-1][j]!=-1&&dp[i-1][j-w[i]]!=-1){
					if(dp[i-1][j]>=dp[i-1][j-w[i]]+v[i])
						dp[i][j]=dp[i-1][j];
					if(dp[i-1][j]<dp[i-1][j-w[i]]+v[i])
						dp[i][j]=dp[i-1][j-w[i]]+v[i];
				}
			}
		}
	}
	int c=0;
	for(int j=0;j<t;j++){
		if(dp[n-1][j]>c)
			c=dp[n-1][j];
	}
	k++;
	for(int j=0;j<t;j++){
		if(c==dp[n-1][j]){
			cout<<"Case "<<k<<":"<<endl;
			cout<<c<<endl;
			cout<<j<<endl;
		}
	}
	}
	return 0;
}