Go: ///
// File:  0529.go
// Author: ymiyamoto
//
// Created on Wed Feb 28 20:39:28 2018
//
package main

import (
	"fmt"
	"sort"
)

func max(n, m int) int {
	if n < m {
		return m
	} else {
		return n
	}
}

func main() {
	for {
		var N, M int
		fmt.Scan(&N, &M)
		if N == 0 && M == 0 {
			break
		}

		arr := make([]int, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Ints(arr)

		sums := make([]int, len(arr)*len(arr))
		for i := 0; i < len(arr); i++ {
			for j := 0; j < len(arr); j++ {
				sums[i+j] = arr[i] + arr[j]
			}
		}
		sort.Ints(sums)

		m := 0
		for i := 0; i < len(sums); i++ {
			rem := M - sums[i]
			if rem >= 0 {
				index := sort.Search(len(sums), func(i int) bool { return sums[i] > rem }) - 1
				if index >= 0 {
					m = max(m, sums[i]+sums[index])
				}
			}
		}
		fmt.Println(m)
	}
}

C++: 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	int ans[4];
	int temp;
	double energy,energy_min,energy_now;
	int M, N;
	int* p;
	int i, j;
	double T=100;
	int ret;
	while (1) {
		scanf("%d%d", &N, &M);
		p = (int*)malloc((N+1) * sizeof(int));
		for (i = 0; i < N; i++) {
			scanf("%d", &p[i]);
		}
		p[N] = 0;
		if (N == M && N == 0) {
			break;
		}
		energy_min = pow(10, 300);
		for (j = 0; j < 4; j++) {
			ans[j] = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
		}
		
		for (T = 1000000; T > 100; T--) {
			for (j = 0; j < 4; j++) {
				temp = p[(int)((double)(rand() + 1) / (RAND_MAX + 2)*(N+1))];
				for (j = 0; j < 4; j++) {
					ret = ans[0] + ans[1] + ans[2] + ans[3];
					ret += temp - ans[j];
					energy = M - ret;
					if (M < ret)energy += 100000000;

					if (energy < energy_min) {
						energy_min = energy;
						ans[j] = temp;
					}
					else if ((double)(rand() + 1) / (RAND_MAX + 2) < exp((energy_min - energy) / T*10)) {
						energy_min = energy;
						ans[j] = temp;
					}
				}

			}
		}
		ret=ans[0]+ +ans[1] + ans[2] + ans[3];
		printf("%d\n", ret);
	}
    return 0;
}

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

using namespace std;

int main()
{
	for(int n,m;cin>>n>>m,n;){
		int p[1001];	p[0]=0;
		for(int i=1;i<=n;i++)	cin>>p[i];

		int nn=0;
		static int xp[1001*1001];
		for(int i=0;i<=n;i++)for(int j=i;j<=n;j++)	xp[nn++]=p[i]+p[j];
		n=nn;
		sort(xp,xp+n);

		int maxscore=0;
		for(int i=0;i<n;i++){
			if(m-xp[i]>=0)
				maxscore=max(maxscore,xp[i]+upper_bound(xp,xp+n,m-xp[i])[-1]);
		}
		cout<<maxscore<<endl;
	}

	return 0;
}

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

using namespace std;

int m,n;
int a[1002];

int ans,Max;

void bin_search(int *aa,int x){
	int l=0,r=n*n;
	Max=0;
	
	while (l<=r) {
		int i=(l+r)/2;
		if (aa[i]<x&&aa[i]>Max) {
			Max=aa[i];
		}
		if(aa[i]<x)l=i+1;
		else r=i-1;
	}
}

void solve(){
	int aa[1002*1002]={0};
	for(int c=0;c<n;c++){
		for (int d=0; d<n; d++) {
			aa[c*n+d]=a[c]+a[d];
		}
	}
	sort(aa,aa+n*n);
	
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			if (m-a[i]-a[i]>=0) {
				bin_search(aa,m-a[i]-a[j]);
				if (Max+a[i]+a[j]>ans) {
					ans=Max+a[i]+a[j];
				}
			}
		}
	}
	
}

int main(){
	
	while (1) {
		scanf("%d%d",&n,&m);
		ans=0;
		if (n==0&&m==0) {
			break;
		}
		for (int i=0; i<n; i++) {
			scanf("%d",&a[i]);
		}
		a[n]=0;
		n++;
		solve();
		printf("%d\n",ans);
	}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

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

typedef vector<int> vi;

int main() {
	while (true) {
		int n, m, score[1001];
		vi v;
		cin >> n >> m;
		if (n == 0 && m == 0) break;
		REP(i, n) scanf("%d", &score[i]);
		score[n++] = 0;

		REP(i, n) FOR(j, i, n) v.push_back(score[i] + score[j]);
		sort(v.begin(), v.end(), greater<int>());

		int ans = 0;
		REP(i, n) FOR(j, i, n) {
			vi::iterator it = lower_bound(v.begin(), v.end(), m - score[i] - score[j], greater<int>());
			if (it == v.end()) continue;
			ans = max(ans, *it + score[i] + score[j]);
		}

		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

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

typedef vector<int> vi;

int main() {
	while (true) {
		int n, m, score[1001];
		vi v;
		cin >> n >> m;
		if (n == 0 && m == 0) break;
		REP(i, n) scanf("%d", &score[i]);
		score[n++] = 0;

		REP(i, n) FOR(j, i, n) v.push_back(score[i] + score[j]);
		sort(v.begin(), v.end());
		unique(v.begin(), v.end());

		int ans = 0;
		REP(i, v.size()) {
			vi::iterator it = upper_bound(v.begin(), v.end(), m - v[i]);
			if (it != v.begin())
				ans = max(ans, *(it - 1) + v[i]);
		}

		cout << ans << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
int t[1001*1001];
using namespace std;
int main(void)
{	
	int  n,p[1001],s,m,i,j,k,x,high,low,mid,max,suu,flg,mx;
	while(1) {
		scanf("%d %d",&n,&m);
		if(n==0 && m==0) break;
		p[0]=0;
		for(i=1;i<=n;i++) scanf("%d",&p[i]);
		x=0;
		for(i=0;i<=n;i++){
			for(j=0;j<=n;j++) {
				t[x]=p[i]+p[j];
				x++;
			}
		}
		sort(t,t+x);
		max=-1;
		mx=-1;
		for(i=0;i<x;i++) {
			flg=0;
			low=0;
			high=x;
			while(low<=high) {
				mid=(low+high)/2;
				if(t[mid]==m-t[i]) {
					flg=1;
					break;
				}
				if(t[mid]<m-t[i]) low=mid+1;
				else if(t[mid]>m-t[i]) high=mid-1;
			}
			if(flg==1) break; 
			else {
				if(t[high]+t[i]>m){}
				else max=t[high]+t[i];
			}
		}
		if(flg==1) printf("%d\n",m);
		else printf("%d\n",max);
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
	
	long long n;
	unsigned long long m;
	while(cin>>n>>m,n!=0||m!=0){
		int k=0;
	unsigned long long  s=0;
		vector<unsigned long long> y(n);
		for(long long int i=0;i<n;i++)
			cin>>y[i];
		sort(y.begin(),y.end());
		while(n>0&&k!=4){
			if(s+y[n-1]<m){
				s=s+y[n-1];
				k++;
			}
			else
				n--;
			
		}
		cout<<s<<endl;
	}
	
}

C++: #include <iostream>
#include <algorithm>
#include <set>

#define ef(e, a, b) ((e) ? (a) : (b))

using namespace std;

int main ( void )
{
	int n, m;
	int p[4][1000];

	while ( cin >> n >> m, (n | m) )
	{
		int ans = 0;
		
		for (int i = 0; i < n; ++i)
		{
			cin >> p[0][i];
			
			ans = max(ans, p[0][i]);
			
			for (int j = 1; j < 4; ++j) { p[j][i] = 0; }
		}
		
		for (int i = 1; i < 4; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				for (int k = 0; k < n; ++k)
				{
					int t = p[i-1][j] + p[0][k];
					p[i][j] = max(p[i][j], ef(t > m, 0, t));
					ans = max(ans, p[i][j]);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int sum[1001100];

int main(){
  int n,d;
  int m[1111];
  while(cin >> n >> d , n){
    memset(sum,0,sizeof(sum));
    for(int i=1;i<=n;i++){
      cin >> m[i];
    }
    m[0] = 0;
    n++;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	sum[i*n+j] = m[i] + m[j];
      }
    }
    sort(sum,sum+n*n);
    
    int res = 0;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	int ab = m[i] + m[j];
	if(ab <= d){
	  int h = d - ab;
	  int tmp = sum[upper_bound(sum,sum+n*n,h)-sum-1] + ab;
	  if(tmp <= d){
	    res = max(res,tmp);
	  }
	}
      }
    }
    cout << res << endl;
  }
}

C++: #include <iostream>
#include <queue>

using namespace std;

int main()
{
    for(;;) {
        int n, m;
        cin >> n >> m;
        if ( n == 0 && m == 0 )
            break;

        priority_queue<int> q;

        for( int i=0; i<n; ++i ) {
            int in;
            cin >> in;
            q.push( in );
        }

        std::size_t result = 0;
        while( !q.empty() ) {
            if ( result + q.top() <= m ) {
                result += q.top();
            } else {
                q.pop();
            }
        }

        cout << result << endl;
    }
}