#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef	long long ll;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define fi first
#define se second
#define pi 3.141592653589793
#define mod 998244353
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
#define sleep for (int i = 1, a;i < 100000000;i++, a = a * a)
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map;

ll modpow(ll a, ll b) {
	ll p = 1;
	while (b > 0) {
		if (b & 1)
			p = p * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return p;
}

ll modinv(ll a) {
	ll m = mod, y = 0, x = 1;
	while (a > 1) {
		ll q = a / m, t = m;
		m = a % m;
		a = t;
		t = y;
		y = x - q * y;
		x = t;
	}
	return x < 0 ? x + mod : x;
}

int main() {
	fio;
	ll n, m;
	cin >> n >> m;
	ll dp[n][m];
	for (int i = 0;i < m;i++)
		dp[0][i] = 1;
	vl s(n), minv(m + 1), mpm1j(m, 1);
	s[0] = m;
	mpm1j[0] = 0;
	ll mpmi = 1;
	for (int j = 1;j <= m;j++)
		minv[j] = modinv(j);
	for (int i = 1;i < n;i++) {
		mpmi = mpmi * m % mod;
		for (int j = 0;j < m;j++) {
			mpm1j[m - 1 - j] = mpm1j[m - 1 - j] * (m - 1 - j) % mod;
			dp[i][j] = (s[i - 1] + (mpmi * j + mpm1j[m - 1 - j]) % mod * minv[j + 1]) % mod;
		}
		for (int j = 0;j < m;j++)
			s[i] = (s[i] + dp[i][j]) % mod;
	}
	cout << s[n - 1] << '\n';
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define drep(i,b,a) for(int i = (b)-1; i >= (a); i--)
#define bit(n) (1LL << (n))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(v) sort(v.begin(),v.end());
#define RSORT(v) sort(v.rbegin(),v.rend());
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vd> vvd;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
const int inf = 1 << 30; const ll infl = 1LL << 60; const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool  odd(T x) { return x & 1; }
template<class T> inline bool even(T x) { return !(x & 1); }
template<typename A, size_t N, typename T> 
void Fill(A (&array)[N], const T &val) {
    std::fill( (T*)array, (T*)(array+N), val );
}
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << endl; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; }
//----------------------------------------------------------------------------------------------


int main() 
{
    int n; ll mx;
    cin >> n >> mx;
    vector<ll> a(n);
    rep(i,0,n) cin >> a[i];

    vector<ll> pre,pos;
    rep(i,0,n/2) pre.push_back(a[i]);
    rep(i,n/2,n) pos.push_back(a[i]);

    vector<ll> v1,v2;
    rep(S,0,bit(sz(pre))) {
        bitset<25> bs(S);
        ll tmp = 0;
        rep(i,0,sz(pre)) if(bs[i]) tmp += pre[i];
        if(tmp > mx) continue;
        v1.push_back(tmp);
    }
    rep(S,0,bit(sz(pos))) {
        bitset<25> bs(S);
        ll tmp = 0;
        rep(i,0,sz(pos)) if(bs[i]) tmp += pos[i];
        if(tmp > mx) continue;
        v2.push_back(tmp);
    }
    SORT(v2);
    ll res = 0;
    for(auto x : v1) {
        auto it = upper_bound(all(v2), mx-x);
        if(it == v2.begin()) continue;
        it--;
        chmax(res, x + *it);
    }
    cout << res << endl;
    return 0;
}