CSES - Shared codeLink to this code:
https://cses.fi/paste/8f406e65ea0636015f4357/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define nl '\n'
#define all(v) v.begin(), v.end()
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n, x;
vector<int> w, dp;
int rec(int wt, int mask){
if(mask == (1 << n) - 1) return dp[mask] = 0;
if(dp[mask] != -1) return dp[mask];
int ans = 1e9;
for(int i=0; i<n; ++i)
if(!((mask >> i) & 1))
ans = ((wt + w[i] > x) ? min(ans, 1 + rec(w[i], mask | (1 << i))) : min(ans, rec(wt + w[i], mask | (1 << i))));
return dp[mask] = ans;
}
void solve(){
cin >> n >> x;
w.resize(n), dp.resize(1 << n, -1);
for(int i=0; i<n; ++i) cin >> w[i];
cout << 1 + rec(0, 0);
}
signed main(){
IOS
solve();
}