Problem:
ダーツ
問題
あなたは以下のルールでダーツゲームをすることになった.
あなたは,矢を的(まと)に向かって 4 本まで投げることができる.必ずしも 4 本全てを投げる必要はなく,1 本も投げなくてもかまわない.的は N 個の部分に区切られていて,各々の部分に点数 P
1
,..., P
N
が書か   れている.矢が刺さった場所の点数の合計 S があなたの得点の基礎となる.S があらかじめ決められたある点数 M 以下の場合は S がそのままあなたの得点となる.しかし,S が M を超えた場合はあなたの得点は 0 点となる.
的に書かれている点数と M の値が与えられたとき,あなたが得ることのできる点数の最大値を求めるプログラムを作成せよ.
入力
入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．
1 行目には,整数 N (1 ≤ N ≤ 1000) と M (1 ≤ M ≤ 200000000 = 2 × 10
8
)がこの順に空白区切りで書かれている.2 行目以降の第 1 + i 行目 (1 ≤  i ≤ N ) には, P
i
(1 ≤ P
i
≤ 100000000 = 10
8
) が書かれている.
採点用データのうち, 配点の 20% 分は N ≤   100 を満たし,配点の 50% 分はN ≤ 300 を満たす.
N, M がともに 0 のとき入力の終了を示す. データセットの数は 10 を超えない．
出力
データセットごとにあなたが得ることのできる点数の最大値を１行に出力する.
入出力例
入力例
4 50
3
14
15
9
3 21
16
11
2
0 0
出力例
48
20
1つ目の例では,15 点の部分に 3 本,3 点の部分に 1 本の矢が刺さった場合にあなたの得点は最大になり,その得点は 48 点である.
2つ目の例では,16 点の場所に 1 本,2 点の場所に 2 本の矢が刺さった場合にあなたの得点は最大になり,その得点は 20 点である.
上記問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #define _GLGBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

int main()
{
    while (true) {
        int N, M, answer = 0;
        cin >> N >> M;

        if (N == 0 && M == 0)
            break;
    
        vector<int> P(N + 1), P_pair_score;
    
        for (int i = 0; i < N; i++)
            cin >> P.at(i);
    
        P.push_back(0);
    
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N + 1; j++)
                P_pair_score.push_back(P.at(i) + P.at(j));
    
        sort(P_pair_score.begin(), P_pair_score.end());
    
        for (int score: P_pair_score) {
            if (score <= M) {
                auto itr = upper_bound(P_pair_score.begin(), P_pair_score.end(), M - score);
    
                if (itr == P_pair_score.begin())
                    answer = max(answer, score);
                else {
                    itr--;
                    answer = max(answer, score + *itr);
                }
            } else
                break;
        }
    
        cout << answer << '\n';
    }

    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 6860
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include<utility>
#include<set>
#include<deque>
#include <unordered_map>
#include<string>
#include<map>
#include<cmath>

using namespace std;


int main() {
	while (true) {
		int N, M;
		cin >> N >> M;
		if (N == 0 and M == 0)break;
		vector<long long>s(N);

		for (int i = 0; i < N; i++)cin >> s[i];
		s.push_back(0);
		vector<int>V;
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j <= N; j++) {
				V.push_back(s[i] + s[j]);
			}
		}

		sort(V.begin(), V.end());
		int ans = 0;
		for (int i = 0; i < V.size(); i++) {
			int a = V[i];
			auto it = upper_bound(V.begin(), V.end(), M - a);

			int ind = it - V.begin();
			if (ind == 0)continue;
			ind -= 1;
			a += V[ind];
			ans = max(ans, a);
		}

		cout << ans << endl;
	}
}
accuracy: 1
time_ms: 410
memory_kb: 11212
final_rank: 10

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
vector<int>v; int a[2000], n, k, maxn;
int main() {
	while (true) {
		cin >> n >> k; if (n == 0)break; maxn = 0; for (int i = 0; i < n; i++)cin >> a[i]; a[n] = 0; v.clear();
		for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++)v.push_back(a[i] + a[j]); }
		sort(v.begin(), v.end());
		for (int i = 0; i < v.size(); i++) {
			int pos1 = upper_bound(v.begin(), v.end(), k - v[i]) - v.begin();
			if (pos1 >= 1)maxn = max(maxn, v[i] + v[pos1 - 1]);
		}
		cout << maxn << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 9116
final_rank: 8

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while(true){
        int N;
        long long M;
        cin >> N >> M;
        if(!cin || (N==0 && M==0)) break;

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

        // Build all sums of two throws (including throwing 0 darts via the 0 entry).
        int K = N+1; 
        vector<int> X;
        X.reserve((long long)K * K);
        for(int i=0;i<K;i++){
            for(int j=0;j<K;j++){
                X.push_back(P[i] + P[j]);
            }
        }

        sort(X.begin(), X.end());

        long long best = 0;
        // For each x in X (sorted ascending), find max y in X with x+y <= M
        for(long long x : X){
            if(x > M) break;            // since X sorted, all further x are too big
            long long rem = M - x;
            // upper_bound → first element > rem, step back one to get ≤ rem
            auto it = upper_bound(X.begin(), X.end(), (int)rem);
            if(it == X.begin()) continue;
            --it;
            best = max(best, x + (long long)*it);
        }

        cout << best << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 7700
final_rank: 7

code_snippet: // #pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>

using namespace std;
using i64=long long;
using u32=unsigned;
using u64=unsigned long long;
using u128=unsigned __int128;

constexpr int inf=numeric_limits<int>::max()/2;
constexpr i64 INF=numeric_limits<i64>::max()/2;

void solve(){
    while(true){
        int n,m;
        cin>>n>>m;
        if(n==m&&m==0){
            break;
        }
        vector<int> p(n);
        for(int i=0;i<n;i++){
            cin>>p[i];
        }
        sort(p.begin(),p.end());
        i64 ans=0;
        for(int i=0;i<n;i++){
            if(p[i]<=m){
                ans=p[i];
            }else{
                break;
            }
        }
        for(int i=0;i<n;i++){
            for(int j=i;j<n;j++){
                i64 x=p[i]+p[j];
                if(x<=m){
                    ans=max(ans,x);
                }else{
                    break;
                }
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<=i;j++){
                i64 x=p[i]+p[j];
                int l=0,r=j;
                while(l<r){
                    int mid=l+r+1>>1;
                    if(x+p[mid]<=m){
                        l=mid;
                    }else{
                        r=mid-1;
                    }
                }
                if(x+p[l]<=m){
                    ans=max(ans,x+p[l]);
                }
            }
        }
        vector<array<i64,2>> pre;
        for(int i=0;i<n;i++){
            for(int j=0;j<=i;j++){
                pre.push_back({p[i]+p[j],i});
            }
        }
        int t=pre.size();
        int c=0;
        map<i64,int,greater<i64>> cnt;
        for(int i=0;i<n;i++){
            while(c<t&&pre[c][1]<=i){
                cnt[pre[c++][0]]++;
            }
            for(int j=i;j<n;j++){
                i64 x=m-p[i]-p[j];
                auto it=cnt.lower_bound(x);
                if(it==cnt.end()){
                    break;
                }
                i64 y=it->first;
                ans=max(ans,p[i]+p[j]+y);
            }
        }
        cout<<ans<<"\n";
    }
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int test=1;
    // cin>>test;
    while(test--){
        solve();
    }
 
    
    
    return 0;
}
accuracy: 1
time_ms: 710
memory_kb: 41788
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

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

        vector<int> p(n);
        rep(i,n){
            cin >> p[i];
        }

        vector<int> twoSum={0};
        rep(i,n){
            twoSum.push_back(p[i]);
            for(int j=i;j<n;j++){
                twoSum.push_back(p[i]+p[j]);
            }
        }
        sort(twoSum.begin(),twoSum.end());

        int res=0;
        rep(i,twoSum.size()){
            int rem=m-twoSum[i];
            auto it=upper_bound(twoSum.begin(),twoSum.end(),rem);
            if(it!=twoSum.begin()){
                it--;
                res=max(res,twoSum[i]+*it);
            }
        }
        cout << res << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 7216
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#define endl '\n'
#define MAX 1000000
using namespace std;
typedef unsigned long long ull;
typedef long double db;
typedef long long ll;
typedef  __int128 i128;
typedef pair<int,int>P;
typedef pair<ll,ll>Pll;
mt19937 rnd(time(0));
const ll mod=1e9+7;
const ll INF=1e9;
const int N=1e3+5;
void solve(){
    ll n,m;
    while(cin >> n >> m){
        if(n==0&&m==0) break;
    vector<ll>a(n);
    for(int i=0;i<n;i++) cin >> a[i];
    sort(a.begin(),a.end());
    vector<ll>sum;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            ll A=a[i]+a[j];
            sum.push_back(A);
        }
    }
    for(int i=0;i<n;i++) sum.push_back(a[i]);
    ll ans=0;
    sort(sum.begin(),sum.end());
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            ll D=m-a[i]-a[j];
            int id=lower_bound(sum.begin(),sum.end(),D)-sum.begin();
            if(id==sum.size()) id--;
            if(id==0&&sum[id]>D) continue;
            if(sum[id]>D) id--;
            ans=max(ans,sum[id]+a[i]+a[j]); 
        }
    }
    for(int i=0;i<n;i++){
        ll D=m-a[i];
        int id=lower_bound(sum.begin(),sum.end(),D)-sum.begin();
        if(id==sum.size()) id--;
        if(id==0&&sum[id]>D) continue;
        if(sum[id]>D) id--;
        ans=max(ans,sum[id]+a[i]); 
    }
    cout << ans << '\n';
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(4);
    int T = 1;
    //cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 490
memory_kb: 18992
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    
    int N, M;
    
    while (true){
        cin >> N >> M;
        if (N == 0 && M == 0) break;
        
        vector<ll> p(N);
        for (auto &t : p) cin >> t;
        p.push_back(0);
        
        vector<ll> dbl;
        for (int i=0; i<N+1; i++){
            for (int j=0; j<N+1; j++){
                dbl.push_back(p[i] + p[j]);
            }
        }

        sort(dbl.begin(), dbl.end());
        
        ll max_score = 0;
        
        for (int i=0; i<(int)dbl.size(); i++){
            int itr = lower_bound(dbl.begin(), dbl.end(), M-dbl[i]) - dbl.begin();
            
            if (itr > 0) max_score = max(max_score, dbl[i] + dbl[itr-1]);
        }
        
        cout << max_score << endl;
    }
}
accuracy: 1
time_ms: 410
memory_kb: 19440
final_rank: 12

code_snippet: #line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  if (x < 0) { cout << (i64)(x); }
  else { cout << (u64)(x); }
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 2 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {}
  return 0;
}

int solve() {
  ll N, M;
  cin >> N >> M;
  if ((N == 0) and (M == 0)) return 1;
  vi P(N);
  rep(i, N) cin >> P[i];
  vi Q(P);
  Q.eb(0);
  Q.eb(inf);
  rep(i, N) rep(j, N) Q.eb(P[i]+P[j]);
  sort(all(Q));
  ll ans = 0;
  rep(i, len(Q)-1) {
    if (M < Q[i]) continue;
    ll j = LB(Q, M-Q[i]+1);
    chmax(ans, Q[i]+Q[j-1]);
  }
  print(ans);
  return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 18916
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (!n && !m) break;
    vector<long long> P(n);
    for (int i = 0; i < n; i++) {
      cin >> P[i];
    }
    P.push_back(0);
    vector<long long> S;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        long long tmp = P[i] + P[j];
        S.push_back(tmp);
      }
    }
    sort(S.begin(), S.end());
    long long ans = 0;
    int n2 = S.size();
    for (int i = 0; i < n2; i++) {
      if (m - S[i] >= 0) {
        int l = 0, r = n2;
        while (r - l > 1) {
          int mid = (r + l) / 2;
          if (S[mid] <= m - S[i]) {
            l = mid;
          } else {
            r = mid;
          }
        }
        ans = max(ans, S[i] + S[l]);
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 470
memory_kb: 19104
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (!n && !m) break;
    vector<long long> P(n);
    vector<int> S;
    for (int i = 0; i < n; i++) {
      cin >> P[i];
      S.push_back(P[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        long long tmp = P[i] + P[j];
        if (tmp <= m) {
          S.push_back(tmp);
        }
      }
    }
    P.push_back(0);
    sort(S.begin(), S.end());
    S.erase(unique(S.begin(), S.end()), S.end());
    long long ans = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        long long tmp = m - P[i] - P[j];
        auto itr = upper_bound(S.begin(), S.end(), tmp);
        if (itr == S.begin()) continue;
        itr--;
        ans = max(ans, P[i] + P[j] + *itr);
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 570
memory_kb: 11008
final_rank: 15

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

int main() {
	while (true) {
		int n, m;
		cin >> n >> m;

		if (n == 0 && m == 0) break;

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

		vector <int> s, t;
		for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) s.push_back(p[i] + p[j]), t.push_back(p[i] + p[j]);
		sort(s.begin(), s.end());
		sort(t.begin(), t.end());

		int max_score = 0;
		for (int i = 0; i < s.size(); i++) {
			if (m < s[i]) break;
			max_score = max(max_score, s[i] + *prev(upper_bound(t.begin(), t.end(), m - s[i])));
		}

		cout << max_score << '\n';
	}
}
accuracy: 1
time_ms: 660
memory_kb: 16444
final_rank: 19

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

int query() {
  int n, m;
  cin >> n >> m;
  if (n == 0 && m == 0) return -1;
  vector<int> p(n + 1);
  for (int i = 1; i <= n; i++) cin >> p[i];
  vector<int> t;
  for (int i: p) for (int j: p) if (i + j <= m) t.push_back(i + j);
  sort(t.begin(), t.end());
  int ans = 0;
  for (int i: t) {
    int x = lower_bound(t.begin(), t.end(), m - i) - t.begin();
    if (x <= t.size()) {
      int c = i + t[x - 1];
      if (c <= m) ans = max(ans, c);
    }
  }
  return ans;
}

int main() {
  while (true) {
    int ans = query();
    if (ans == -1) break;
    cout << ans << '\n';
  }
}
accuracy: 1
time_ms: 370
memory_kb: 11080
final_rank: 9

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

using namespace std;

using LL = long long;

int main() {
    while (true) {
        int n, m;
        cin >> n >> m;
        if (n == 0 && m == 0) return 0;
        vector<LL> p(n + 1);
        for (int i = 1; i <= n; ++i) cin >> p[i];
        vector<LL> q = {0};
        for (int i = 0; i <= n; ++i) {
            for (int j = i; j <= n; ++j) {
                if (p[i] + p[j] <= m) q.push_back(p[i] + p[j]);
            }
        }
        sort(q.begin(), q.end());
        LL max_points = 0;
        for (LL x : q) {
            size_t pos = lower_bound(q.begin(), q.end(), m - x) - q.begin();
            if (pos == 0) {
                max_points = max(max_points, x + q[0]);
            } else {
                max_points = max(max_points, x + q[pos - 1]);
            }
        }
        cout << max_points << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 11100
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using pl = pair<ll,ll>;
using vp = vector<pl>;
#define rep(i,n) for(ll i=0;i<(ll)(n);++i)
#define reps(i,s,n) for(ll i=(s);i<(ll)(n);++i)
#define rep1(i,n) for(ll i=1;i<=(ll)(n);++i)
template<typename T>inline bool chmax(T &a,T b){return a<b?a=b,true:false;}
template<typename T>inline bool chmin(T &a,T b){return a>b?a=b,true:false;}
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define be(v) (v).begin(),(v).end()
const long long INF = 1e18;
#ifdef DEBUG
#include <debug.hpp>
#endif

bool solve(){
	#ifdef DEBUG
	cout << "--- Input ---" << endl;
	#endif
	//cin.unsetf(ios::skipws);
	ll N,M;cin>>N>>M;
	if(N==0&&M==0)return false;
	vl P(N);rep(n,N)cin>>P.at(n);

	P.pb(0);
	vl v;
	//rep(i,N)rep(j,N){
	for(auto e:P)for(auto f:P){
		v.pb(e+f);
	}
	sort(be(v));
	#ifdef DEBUG
	show(N,M,P,v);
	cout << "--- Logic ---" << endl;
	#endif
	ll ans=0;
	for(auto e:v){
		auto it=lower_bound(be(v),M-e);
		if(it==v.end() && e<=M) chmax(ans,e);
		else if(e + *it<=M) chmax(ans,e + *it);
		else if(it>v.begin()) chmax(ans,e + *(it-1));
		else if(e<=M) chmax(ans,e);
	}
	#ifdef DEBUG
	cout << "--- Answer ---" << endl;
	#endif
	cout<<ans<<endl;
	return true;
}
int main(){
	#ifdef DEBUG
	cout << "--- Main ---" << endl;
	#endif
	//solve();
	while(solve());
	return 0;
}
//cout << fixed << setprecision(9);
accuracy: 1
time_ms: 390
memory_kb: 19208
final_rank: 11

code_snippet: #include <queue>
#include <vector>
#include <string>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long i64;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n, m;
	while (cin >> n >> m && !(n == 0 && m == 0)) {
		vector<int> a(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		
		vector<int> todo;
		for (int i = 0; i < n; i++) {
			todo.emplace_back(a[i]);
		}
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				todo.emplace_back(a[i] + a[j]);
			}
		}
		
		int ans = 0;
		for (int x : todo) if (x <= m) ans = max(ans, x);
		
		sort(todo.begin(), todo.end());
		
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				if (m < a[i] + a[j]) {
					continue;
				}
				auto it = upper_bound(todo.begin(), todo.end(), m - a[i] - a[j]);
				if (it != todo.begin()) {
					ans = max(ans, int(*(prev(it))) + a[i] + a[j]);
				}
			}
		}
		
		cout << ans << "\n";
	}
	
	return 0;
}
accuracy: 1
time_ms: 300
memory_kb: 6928
final_rank: 6

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

int main()
{
    int N, M;
    while (cin >> N >> M)
    {
        if(N==0&&M==0)
            break;
        vector<int> point(N + 1, 0);
        for (int i = 0; i < N; i++)
        {
            cin >> point.at(i + 1);
        }

        vector<int> dp((N+1) * (N+1));
        for (int i = 0; i < N+1; i++)
        {
            for (int j = 0; j < N+1; j++)
            {
                dp.at((N+1) * i + j) = point.at(i) + point.at(j);
            }
        }
        sort(dp.begin(), dp.end());

        int maxpoint = 0;
        for (int i = 0; i < N+1; i++)
        {
            for (int j = 0; j < N+1; j++)
            {
                int sum = point.at(i) + point.at(j);
                if (sum > M)
                    continue;
                else
                {
                    sum += *prev(upper_bound(dp.begin(), dp.end(), M - sum));
                    maxpoint = max(maxpoint, sum);
                }
            }
        }
        cout << maxpoint << endl;
    }
}
accuracy: 1
time_ms: 590
memory_kb: 7344
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
    // Your code here!
    int N,M;
    while(1){
        cin >> N >> M;
        if(N==0 && M==0){
            break;
        }
        vector<int> A(N+1);
        A[0]=0;
        for(int i=1;i<N+1;i++){
            cin >> A[i];
        }
        
        vector<int> B((N+2)*(N+1)/2);
        for(int i=0;i<N+1;i++){
            for(int j=i;j<N+1;j++){
                B.push_back( A[i]+A[j] );
            }
        }
        
        sort(B.begin(),B.end());
        
        long res,resMax=0;
        for(int i=0;i<B.size();i++){
            if(B[i]>M){
                break;
            }
            auto  iter=upper_bound(B.begin(),B.end(),M-B[i]);
            iter--;
            res=B[i]+*(iter);
            if(res>resMax){
                resMax=res;   
            }
        }
        cout << resMax<< endl;
    }
}
accuracy: 1
time_ms: 250
memory_kb: 9008
final_rank: 4

code_snippet: #include <iostream>
#include <stdlib.h>
using namespace std;

int compare_int(const void *a, const void *b)
{
    return *(int*)a - *(int*)b;
}

int lower(int a[],int s,int v){
    int l = 0;
    int r =s- 1;
    while (r-l>1)
    {
        int mid = l + (r - l) / 2;
        if (a[mid]<=v){
            l = mid ;
        }else{
            r = mid ;
        }
    }
    return l;
}


int main(void){
    // Your code here!
    int N,M;
    while(1){
        cin >> N >>M;
        if(N==0 && M==0){
            break;
        }
        int P[N+1];
        
        P[0]=0;
        for(int i=1;i<N+1;i++){
            cin >> P[i];
        }
        
        int P2[(N+1)*(N+1)];
        for(int i=0;i<N+1;i++){
            for(int j=0;j<N+1;j++){
                P2[i*(N+1)+j]=P[i]+P[j];       
            }
        }
        qsort(P2, (N+1)*(N+1), sizeof(int), compare_int);
        
        int sum1,sum2;
        int maxSum=0;
        int PMax=P2[(N+1)*(N+1)-1];
        //int startN=lower(P,M/4);
        for(int i=0;i<(N+1)*(N+1);i++){
            sum1=P2[i];
            if(sum1>M-PMax && sum1<=M){
                int res=lower(P2,(N+1)*(N+1),M-sum1);
                sum2=sum1+P2[res];
                if(sum2>maxSum) {
                    maxSum=sum2;
                }
            }
        }
        cout <<maxSum <<endl;
    }
}
accuracy: 1
time_ms: 700
memory_kb: 11124
final_rank: 18

code_snippet: #include <algorithm>
#include <cstdio>
using namespace std;

constexpr int MAX_N = 1000;
int ps[MAX_N + 1];
int qs[(MAX_N + 1) * (MAX_N + 1)];

int main() {
  for (;;) {
    int N, M;
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;
    ps[0] = 0;
    for (int i = 0; i < N; ++i)
      scanf("%d", &ps[i + 1]);

    int i = 0;
    for (int j = 0; j < N + 1; ++j)
      for (int k = 0; k < N + 1; ++k)
        qs[i++] = ps[j] + ps[k];
    int qs_size = (N + 1) * (N + 1);
    sort(&qs[0], &qs[qs_size]);

    int max_ = 0;
    for (int i = qs_size - 1; i >= 0; --i) {
      int a = qs[i];
      if (a < M / 2)
        break;
      auto it = upper_bound(&qs[0],  &qs[qs_size], M - a);
      if (it == &qs[0])
        continue;
      max_ = max(max_, a + *(it - 1));
    }
    printf("%d\n", max_);
  }
  return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 6716
final_rank: 5