#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<complex>
#include<stack>
#include<cmath>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)


int main(){
	  
	while(1){
		int n;
		cin>>n;
		if(n==0)break;
		
		int in[5555];
		rep(i,n)cin>>in[i];
		
		
		int ans = -1000000000;
		reps(i,1,n+1){
			
			int sum = 0;
			rep(j,i)sum += in[j];
			
			ans = max(ans,sum);
			reps(j,i,n){
				sum-=in[j-i];
				sum+=in[j];
				ans = max(ans,sum);
			}
		}
		printf("%d\n",ans);
	}
	
}
#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=a;i<b;++i)
#define for_rev(i,a,b) for(int i=a;i>=b;--i)
#define allof(a) a.begin(),a.end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) (int)a.size()

int N, M;
int p[1010];

int main() {
	while (cin >> N >> M, N) {
		for_(i,0,N) cin >> p[i];
	
		vector<int> vp;
		vp.push_back(0);
		for_(i,0,N) vp.push_back(p[i]);
	
		vector<int> sum2;
		for_(i,0,N + 1) for_(j,0,N + 1) sum2.push_back(vp[i] + vp[j]);
	
		sort(allof(sum2));
		int s_size = size_of(sum2);
		int ans = 0;
		for_(i,0,s_size) {
			int sum = sum2[i];
			if (sum <= M) {
				int sub = M - sum;
				int lb = *(upper_bound(allof(sum2), sub) - 1);
				//cout << sub << " " << lb << endl;
				sum += lb;
				ans = max(ans, sum);
			}
		}
		cout << ans << endl;
	}
	return 0;
}