Problem:
Score : 200 points

Problem Statement

There are N children, numbered 1, 2, ..., N.

Snuke has decided to distribute x sweets among them.
He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets.
Snuke is trying to maximize the number of happy children by optimally distributing the sweets.
Find the maximum possible number of happy children.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.

Solution in C++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
//#define endl '\n'
//#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=55;
ll n;
ll yal[N][N];
vector <int> y[N];
ll par[N];
ll par2[N];
ll ans;
void dfsfindpar(ll v,ll p){
    par[v]=p;
    for (auto u : y[v]){
        if (u==p) continue;
        dfsfindpar(u,v);
    }
    return ;
}
vector <int> g[N];
vector <int> out[N];
void dfsfind(ll v,ll p){
    par2[v]=p;
    for (auto u : g[v]){
        if (u==p) continue;
        dfsfind(u,v);
    }
}
ll in[N];
ll jav;
ll vis2[N];
void dddfs(ll v,ll p,ll k){
    if (k!=v){
        out[k].pb(v);
        in[v]++;
    }
    for (auto u : g[v]){
        if (u==p) continue;
        dddfs(u,v,k);
    }
}
void ddfs(ll v,ll p){
    if (!vis2[v]) dddfs(v,p,v);
    for (auto u : g[v]){
        if (u==p) continue;
        ddfs(u,v);
    }
    return ;
}
void dfs(ll v,ll p){
    if (par[v]!=p){
        jav++;
        ddfs(v,p);

    }
    for (auto u : g[v]){
        if (u==p) continue;
        dfs(u,v);
    }
    return ;
}
ll vis[N];
void check(){
    memset(vis2,0,sizeof vis2);
    memset(vis,0,sizeof vis);
    queue <int> q;
    for (int i=1;i<=n;i++){
        if (!in[i]) q.push(i);
    }
    while(q.size()){
        ll v=q.front();
        q.pop();
        vis[v]=1;
        for (auto u : out[v]){
            in[u]--;
            if (in[u]==0) q.push(u);
        }
    }
    for (int i=1;i<=n;i++){
        if (!vis[i]) jav=1e9;
    }
    return ;
}
ll solve(ll v){
    jav=0;
    for (int i=1;i<=n;i++){
        out[i].clear();
        in[i]=0;
        g[i].clear();
        for (int j=1;j<=n;j++){
            if (yal[i][j]) g[i].pb(j);
        }
    }
    dfsfind(v,v);
    dfsfindpar(v,v);
    dfs(v,v);
    for (int t=1;t<=n;t++){
        for (int i=1;i<=n;i++){
            if (in[par[i]] || par2[par[i]]!=par[par[i]] || out[par[i]].size()){
                out[i].pb(par[i]);
                in[par[i]]++;
            }
        }
    }
   // cout << par2[par[4]] << " par " << par[par[4]] << endl;
    check();
    return jav;
}
ll Main(){
    ans=1e9;
    cin >> n;
    memset(yal,0,sizeof yal);
    for (int i=1;i<=n;i++) y[i].clear();
    for (int i=1;i<n;i++){
        ll u,v;
        cin >> u >> v;
        yal[u][v]=1;
        yal[v][u]=1;
    }
    for (int i=0;i<n-1;i++){
        ll u,v;
        cin >> u >> v;
        y[u].pb(v);
        y[v].pb(u);
    }
    for (int i=1;i<=n;i++){
        ll deg=0;
        ll id=0;
        for (int j=1;j<=n;j++){
            if (yal[i][j]){
                deg++;
                id=j;
            }
        }
        if (deg>1) continue;
        yal[i][id]=0;
        yal[id][i]=0;
        for (int j=1;j<=n;j++){
            if (i==j) continue;
            /// case ans=0 ;
            yal[i][j]=1;
            yal[j][i]=1;
            ans=min(ans,solve(i)+(j!=id));
       //     cout << i << " " << j << " " << ans << endl;
            yal[i][j]=0;
            yal[j][i]=0;
        }
        yal[i][id]=1;
        yal[id][i]=1;
    }
    if (ans>2*n) kill(-1);
    kill(ans);

}
int32_t main(){
    sync;
    ll t;
    cin >> t;
    while(t--) Main();
}