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

import (
	"fmt"
	"sort"
)

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

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

		arr := make([]int64, N+1)
		for i := 1; i <= N; i++ {
			fmt.Scan(&arr[i])
		}
		sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })

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

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

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

int main(){
while(1){
	int n,m;
	vector<int> p,q;
	scanf("%d%d",&n,&m);
if(n==0&&m==0)break;
	q.push_back(0);
	for(int i=0;i<n;i++){
		int a;
		scanf("%d",&a);
		p.push_back(a);
		q.push_back(a);
	}
	for(int i=0;i<n;i++){
		for(int j=i;j<n;j++){
			q.push_back(p[i]+p[j]);
		}
	}
	sort(q.begin(),q.end());
	int ans=0;
	int f=0,s=q.size()-1;
	while(1){
		if(q[f]+q[s]>ans&&q[f]+q[s]<=m)ans=q[f]+q[s];
		if(q[f]+q[s]<=m)f++;
		if(q[f]+q[s]>m)s--;
		if(f>s)break;
	}
	printf("%d\n",ans);
}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define int long long

#define rep(i,x) for(int i=0;i<x;++i)
#define all(a) begin(a),end(a)
#define PB push_back

int P[1010];

signed main()
{
    int N, M;
    
    while (cin >> N >> M, N) {
        rep(i, N) {
            cin >> P[i];
        }

        vector<int> arr;

        rep(i, N) {
            arr.PB(P[i]);

            rep(j, i+1) arr.PB(P[i] + P[j]);
        }

        arr.PB(0);
        sort(all(arr));

        int ma = 0;

        rep(i, arr.size()) {
            int a = arr[i];
            int b = *(lower_bound(all(arr), M - a + 1) - 1);
            if (a + b <= M) ma = max(ma, a + b);
        }

        cout << ma << endl;
    }
}

C++: #include<iostream>
#include<set>
#include<vector>
using namespace std;
typedef unsigned int uint;
uint maxv;
void solve(uint now,uint m,uint *a,int n){
  int r=n-1,l=0,mid;
  while(l <=r){
    mid = (l+r)/2;
    if ( a[mid]+now>m)r=mid-1;
    else if ( a[mid]+now <m)l=mid+1;
    else break;

  }
  if ( now+a[mid]>maxv&& now+a[mid]<m)maxv=now+a[mid];
}


main(){
  int n;
  uint m;
  while(scanf("%d%d",&n,&m) && n){
    set<uint> S;    
    uint one[n];
    uint two[n*n];
    int p=0;
    for(int i=0;i<n;i++)scanf("%d",&one[i]);
    for(int i=0;i<n;i++){
      for(int j=i;j<n;j++){
	//if ( S.find(one[i]+one[j]) == S.end())S.insert(one[i]+one[j]),two[p++]=one[i]+one[j];
	two[p++]=one[i]+one[j];
      }
    }
    sort(one,one+n);
    sort(two,two+p);
    maxv=0;
    for(int i=0;i<p;i++){
      if ( two[i]+one[0]>m)break;
      if ( two[i]+two[0]<=m)solve(two[i],m,two,p);
      if ( two[i]+one[0]<=m)solve(two[i],m,one,n);
    }
    printf("%d\n",maxv);
  }
  return false;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int n,m;
    for(;;){
        scanf("%d%d",&n,&m);
        if(n==0&&m==0)break;
        vector<int> ns(n+1);
        for(int i=0;i<n;i++){
            scanf("%d",&ns[i]);
        }
        ns[n]=0;
        vector<int> ntwos;
        for(int i=0;i<n+1;i++){
            for(int j=i;j<n+1;j++){
                ntwos.push_back(ns[i]+ns[j]);
            }
        }
        sort(ntwos.begin(),ntwos.end());
        int ma=0;
        for(int i=0;i<ntwos.size();i++){
            vector<int>::iterator it;
            if((it=upper_bound(ntwos.begin(),ntwos.end(),m-ntwos[i]))!=ntwos.begin()){
                ma=max(ma,ntwos[i]+*(it-1));
            }
        }
        printf("%d\n",ma);
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a; i<n; i++)
#define repq(i,a,n) for(int i=a; i<=n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)

typedef long long int ll;
typedef pair<int, int> pii;

int N, M, p[1010];
int main() {
    while(cin >> N >> M, N || M) {
        vector<int> v;
        rep(i,0,N) cin >> p[i];
        rep(i,0,N) v.push_back(p[i]);
        rep(i,0,N) rep(j,0,N) v.push_back(p[i] + p[j]);

        sort(v.begin(), v.end());
        int S = v.size();
        int ans = 0;
        rep(i,0,S) {
            int val = v[i];
            int rest = M - val;

            int near = upper_bound(v.begin(), v.end(), rest) - v.begin() - 1;
            if(near < 0) continue;
            ans = max(ans, val + v[near]);
        }
        cout << ans << endl;
    }
    return 0;
}

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

int m, n, p[1000], pp[1000*1000];

int main(){
    while( true ){
        cin >> n >> m;
        if( m == 0 && n == 0 ) break;
        for(int i = 0; i < n; i++){
            cin >> p[i];
        }
        sort(p, p+n);
        for(int i = 0; i < n*n; i++){
            pp[i] = p[i/n] + p[i%n];
        }
        sort(pp, pp + n*n);
        int c = n/p[n-1];
        if( c >= 4 ){
            cout << p[n-1]*4;
            continue;
        }
        int cand = 0;
        if( c <= 3 ){
            int j = n*n-1;
            for(int i = 0; i <= n*n/2 &&; i++){
                while( pp[i] + pp[j] > m && j > 0 ) j--;
                cand = max(cand, pp[i] + pp[j]);
            }
        }
        if( c <= 2 ){
            int j = n*n-1;
            for(int i = 0; i <= n/2; i++){
                while( p[i] + pp[j] > m && j > 0 ) j--;
                cand = max(cand, p[i] + pp[j]);
            }
        }
        if( c <= 1 ){
            int j = n-1;
            for(int i = 0; i <= n/2; i++){
                while( p[i] + p[j] > m && j > 0 ) j--;
                cand = max(cand, p[i] + p[j]);
            }
        }
        int k;
        for(k = n-1; p[k] > m && k > 0; k--);
        cand = max(cand, p[k]);
        cout << cand << endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
int n,m;
int a[1001];
int b[510000];
bool f[1001][1001];
int i,j;
int ans;
int c;
 
int max(int a,int b){
    if(a>=b)return a;
    else return b;
}
 
int comp(const void *a,const void *b){
    int p=*(int*)a;
    int q=*(int*)b;
    return p-q;
}
 
int search(int num){
    int r=0;
    int l=c;
    int mid;
    for(int k=0;k<25;k++){
        mid=(int)(r+l)/2;
        if(num-b[mid+1]>=0&&num-b[mid]>num-b[mid+1])mid=mid+1;
        if(b[mid]>num)l=mid;
        else r=mid;
    }
    return mid;
}
 
int main(){
    while(1){
        scanf("%d %d",&n,&m);
        if(n==0&&m==0)return 0;
        ans=0;
        c=0;
        for(i=0;i<n;i++)scanf("%d",&a[i]);
        a[n]=0;
        for(i=0;i<n+1;i++)for(j=0;j<n+1;j++)f[i][j]=false;
        for(i=0;i<n+1;i++)for(j=0;j<n+1;j++){
            if(!f[j][i]&&a[i]+a[j]<=m){
                b[c++]=a[i]+a[j];
                f[i][j]=true;
            }
        }
        qsort(b,c,sizeof(int),comp);
        for(i=0;i<c;i++){
            if(m-b[i]>=0){
                ans=max(ans,b[search(m-b[i])]+b[i]);
            }
        }
        printf("%d\n",ans);
    }
}