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 <cstdio>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n, m, c[1009], s[1000009];
int main() {
	while (scanf("%d%d", &n, &m), n) {
		for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
		int b = 0;
		for (int i = 0; i <= n; i++) {
			for (int j = i; j <= n; j++) {
				if (c[i] + c[j] <= m) s[b++] = c[i] + c[j];
				else break;
			}
		}
		sort(s, s + b);
		int r = b - 1, ret = 0;
		for (int i = 0; i < b && i <= r; i++) {
			while (r > 0 && s[i] + s[r] > m) r--;
			ret = max(ret, s[i] + s[r]);
		}
		printf("%d\n", ret);
	}
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int N,M,P[1000];
int main() {
	while(scanf("%d %d",&N,&M),N|M) {
		for(int i=0;i<N;i++) scanf("%d",&P[i]);
		int ans=0;
		vector<int> X,Y;
		X.push_back(0);
		for(int i=0;i<4;i++) {
			for(int j=0;j<X.size();j++) {
				for(int k=0;k<N;k++) {
					int v=(X[j]+P[k])%M;
					ans=max(ans,v);
					Y.push_back(v);
				}
			}
			Y.erase(unique(Y.begin(),Y.end()),Y.end());
			X=Y;
		}
		printf("%d\n",ans);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n,m;
  while(cin>>n>>m,n){
    int p[n];
    for(int i=0;i<n;i++) cin>>p[i];
    vector<int> s;
    s.emplace_back(0);
    s.emplace_back(m+1);
    for(int i=0;i<n;i++)
      s.emplace_back(p[i]);
    for(int i=0;i<n;i++)
      for(int j=0;j<n;j++)
	s.emplace_back(p[i]+p[j]);
    sort(s.begin(),s.end());
    s.erase(unique(s.begin(),s.end()),s.end());
    int ans=0;
    auto latte=--s.end();
    for(int x:s){
      if(x>m) continue;
      while(latte!=s.begin()&&x+*latte>m) --latte;
      int y=*latte;
      if(x+y>m) continue;
      ans=max(ans,x+y);
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#define INF 1<<28
using namespace std;

int p[1001];
int n;

int solve(int s, int m){

  if(s < 0) return INF;
  if(!m) return s;

  int res = INF;
  for(int i=0;i<=n;i++) res = min(res, solve(s-p[i], m-1));

  return res;
}

int main(){

  int s;
  while(true){
    cin >> n >> s;
    if(!n && !s) break;

    for(int i=1;i<=n;i++) cin >> p[i];

    cout << s-solve(s, 4) << endl;

  }
}

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

using namespace std;

int main() {
    while (true) {
        int n, m;
        cin >> n >> m;
        if (n == 0 && m == 0) return 0;
        int p[n + 1];
        p[0] = 0;
        for (int i = 0; i < n; i++) {
            cin >> p[i + 1];
        }
        sort(p, p + (n + 1));
        int *bound = upper_bound(p, p + (n + 1), m);
        int result = 0;
        for (int *ptr = bound - 1; ptr >= p; ptr--) {
            int a = *ptr;
            int *bound2 = upper_bound(p, bound, m - a);
            for (int *ptr2 = bound2 - 1; ptr2 >= p; ptr2--) {
                int b = *ptr2;
                int *bound3 = upper_bound(p, bound2, m - a - b);
                for (int *ptr3 = bound3 - 1; ptr3 >= p; ptr3--) {
                    int c = *ptr3;
                    int *bound4 = upper_bound(p, bound3, m - a - b - c);
                    if ((bound4 - 1) < p) continue;
                    int d = *(bound4 - 1);
                    result = max(result, a + b + c + d);
                }
            }
        }
        cout << result << endl;
    }
}

C++: #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace::std;
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define endl "\n"
typedef long long lint;
#define rep(i, n) for(lint i = 0; i < (lint)(n); i++)
#define repi(i,a,b) for(lint i=lint(a);i<lint(b);i++)
#define rrep(i, n) for(lint i = (lint)(n-1); i >-1; i--)
#define output(v) do{bool f=0;for(auto i:v){cout<<(f?" ":"")<<i;f=1;}cout<<"\n";}while(0)
#define all(x) (x).begin(),(x).end()
#define INF (1LL<<60)
#define MOD 1000000007
inline bool chmin(auto& s,const auto& t){bool res=s>t;s=min(s,t);return res;}
inline bool chmax(auto& s,const auto& t){bool res=s<t;s=max(s,t);return res;}
#define output(v) do{bool f=0;for(auto i:v){cout<<(f?" ":"")<<i;f=1;}cout<<"\n";}while(0)
#define output2(v) for(auto i:v)output(i);
vector<lint> dx={-1,1,0,0,1,1,-1,-1};
vector<lint> dy={0,0,-1,1,1,-1,1,-1};
int main(){
    while(1){
        lint n,m;
        cin>>n>>m;
        if(n==0&&m==0)break;
        vector<lint> a(n);
        rep(i,n)cin>>a[i];
        vector<lint> list(all(a));
        rep(i,n)rep(j,i+1){
            list.push_back(a[i]+a[j]);
        }
        list.push_back(0);
        sort(all(list));
        lint ans=0;
        for(lint val:list){
            auto itr=upper_bound(all(list),m-val);
            if(itr!=list.begin())chmax(ans,val+*prev(itr,1));
        }
        cout<<ans<<endl;
    }
}

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

long long kazu[1002001];//1001^2
long long n,m,Max = 0,data[1001];

int main(){
	while(std::cin>>n>>m, n * m){
		if(n==0&&m==0)break;

		memset(kazu, 0, sizeof(kazu));
		memset(data, 0, sizeof(data));

		for(int i=0;i<n;i++){
			std::cin >> data[i+1];
		}
		for(int c=0;c<n+1;c++){
			for(int d=0;d<n+1;d++){
				kazu[c*n+d]=data[c]+data[d];
			}
		}
		std::sort(kazu,kazu+(n+1)*(n+1));

		long long* ptr;
		for(int i = 0; i < (n + 1) * (n + 1);++i){
			if(kazu[i] < m){
				ptr = std::upper_bound(kazu, kazu + (n + 1) * (n + 1), m - kazu[i]) - 1;
				Max = std::max(Max, *ptr + kazu[i]);
			}
		}


		std::cout << Max << std::endl;
		Max = 0;
	return 0;
}

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

#define rep(i, n) for (int i=0; i<n; i++)
typedef long long int ll;

int N, M;
ll P[1111];
ll PP[11111111];
int p;

int main(int argc, char *argv[])
{
  while(cin >> N >> M, N){
    int NN = N * N;
    rep(i, N) cin >> P[i];
    rep(i, N) rep(j, N) PP[i + j * N] = P[i] + P[j];
    sort(P,  P  + N );
    sort(PP, PP + NN);
    ll res = 0;
    res = max(res,  P[upper_bound(P,  P  + N,  M) -  P - 1]);
    res = max(res, PP[upper_bound(PP, PP + NN, M) - PP - 1]);    
    rep(i, N) res = max(res, PP[upper_bound(PP, PP + NN, M -  P[i]) - PP - 1] +  P[i]);
    rep(i, NN) if((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0) res = max(res, PP[p] + PP[i]);
    rep(i, NN) if((p = upper_bound(PP, PP + NN, M - PP[i]) - PP - 1) >= 0) res = max(res, PP[p] + PP[i]);
    cout << res << endl;
  }
  
  
  return 0;
}

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

using namespace std;

int main()
{
	for(int n,m;scanf("%d%d",&n,&m),n;){
		int p[1001];	p[0]=0;
		for(int i=1;i<=n;i++)	scanf("%d",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];
		sort(xp,xp+nn);

		int maxscore=0;
		for(int i=0;i<nn;i++){
			if(m-xp[i]>=0)
				maxscore=max(maxscore,xp[i]+upper_bound(xp,xp+nn,m-xp[i])[-1]);
		}
		printf("%d\n",maxscore);
	}

	return 0;
}