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 <vector>
#include <algorithm>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() {return val;}
    bool operator!=(Iterator& itr) {return val < itr.val;}
    void operator++() {++val;}
  };
  Iterator i, n;
public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator& begin() {return i;}
  Iterator& end() {return n;}
};

template<class T> inline T at(const vector<T> &v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];}

template<class T> inline bool is_max(T &a, const T &b) {return a < b ? a = b, true : false;}
template<class T> inline bool is_min(T &a, const T &b) {return a > b ? a = b, true : false;}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    vector<int> p;
    for (int _ : range(n)) {
      int x;
      cin >> x;
      p.emplace_back(x);
    }
    p.emplace_back(0);
    vector<int> s;
    for (int i : p) for (int j : p) s.emplace_back(i + j);
    int res = 0;
    int i = 0, j = s.size() - 1;
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    while (i <= j) {
      if (s[i] + s[j] > m) {
        --j;
        continue;
      }
      is_max(res, s[i] + s[j]);
      ++i;
    }
    cout << res << endl;
  }
}

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

using namespace std;

int main(){

	int n,m;
	while(cin>>n>>m&&!(n==0&&m==0)){
		int scores[1001];
		int comb[10000001];
		int cn=0;

		for(int i = 0; i < n; i++)
			cin>>scores[i];
		scores[n]=0;
		for(int i = 0; i < n+1; i++){
			for(int j = 0; j < n+1; j++){
				int sum=scores[i]+scores[j];
				if(sum<=m){
					comb[cn]=sum;
					cn++;
				}
			}
		}
		sort(comb,comb+cn);

		int maxNum=0;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				int left = (scores[i]+scores[j]);
				if(left<=m&&comb[0]+left<=m){
					int *it = lower_bound(comb,comb+cn,m-left);
					if(it==comb+cn)
						maxNum=max(maxNum,comb[cn-1]+left);
					else{
						if(*it==(m-left))
							maxNum=m;
						else{
							it--;
							maxNum=max(maxNum,*it+left);
						}
					}
				}
			}
		}
		cout<<maxNum<<endl;
	}

	return 0;
}

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 = 2000000; 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;

void solve(int N, int M, int P[1000])
{
	int score = 0;
	sort(P, P + N, greater<int>());
	
	for(int j = 0; j < 4; j++){		
		int i = 0;
		while(true){
			score += P[i];
			if(score > M){
				score -= P[i];
				i++;
			}
			else break;
		}
	}
	
	cout << score << endl;
}


int main()
{
		int N, M;
		int P[1000];

	while(true){
		cin >> N;
		cin >> M;
		if(N == 0 && M == 0) break;

		for(int i = 0; i < N; i++){
			cin >> P[i];
		}

		solve(N, M, P);
	}

	return 0;
}

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

using namespace std;

int main(){
    while(1){
        int n,m;
        int i;
        int sum = 0;
        int darts = 4;
        cin >> n;
        cin >> m;
        if(!n && !m) break;
        vector<int> points(n);
        for(i=0;i<n;++i){
            cin >> points[i];
        }
        sort(points.begin(), points.end(), greater<int>() );
        for(i=0;i<n;++i){
            int p = points[i];
            if(m - sum >= p ){
                int amount = (m-sum)/p;
                if( amount > darts) amount = darts;
                sum += amount * p;
                darts -= amount;
                if(darts == 0) break;
            }
        }
        cout << sum << endl;
    }
    return 0;
}

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

using namespace std;

long long int gten[1000000];
long long int p[1111];

char bake[22222222];

long long int ans[1000];

int main()
{
  int ww = 0;
  while(1)
    {
      long long int n,m;
      scanf("%lld %lld",&n,&m);
      if(n == 0 && m == 0)
	{
	  break;
	}
      p[0] = 0;
      for(int i = 1; i <= n; i++)
	{
	  scanf("%lld",&(p[i]));
	}
      int nw = 0;
      for(int i = 0; i < 10; i++)
	{
	  for(int j = 0; j < 22222222; j++)
	    {
	      bake[j] = 0;
	    }
	  for(int j = 0; j <= n; j++)
	    {
	      for(int jj = 0; jj <= n; jj++)
		{
		  if(i * 22222222 <= p[j] + p[j] && p[j] + p[jj] < (i + 1) * 22222222)
		    {
		      bake[p[j] + p[jj] - i * 22222222] = 1;
		    }
		}
	    }
	  for(int j = 0; j < 22222222; j++)
	    {
	      if(bake[j] == 1)
		{
		  gten[nw] = i * 22222222 + j;
		  nw++;
		}
	    }
	}
      long long int max = 0;
      int ai = 0;
      for(int i = nw - 1; i >= 0; i--)
	{
	  while(1)
	    {
	      if(ai + 1 >= nw)
		{
		  break;
		}
	      if(gten[i] + gten[ai + 1] <= m)
		{
		  ai++;
		  if(max < gten[i] + gten[ai])
		    {
		      max = gten[i] + gten[ai];
		    }
		}
	      else
		{
		  break;
		}
	    }
	}
      ans[ww] = max;
      ww++;
    }
  for(int i = 0; i < ww; i++)
    {
      printf("%lld\n",ans[i]);
    }
  return 0;
}