HTML & JS
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
int tc;
signed main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        vi a(n);
        for (auto &x : a)
            cin >> x;
        vi cntr(31), set(31);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 31; j++)
                if (a[i] & (1 << j))
                    cntr[j]++;
        }
        int available = k;
        for (int i = 30; i >= 0; i--)
        {
            int needed = n;
            int current = cntr[i];
            int required = needed - current;
            if (required <= available)
            {
                set[i] = 1;
                available -= required;
            }
        }
        int ans = 0;
        for (int i = 30; i >= 0; i--)
            if (set[i] || cntr[i] == n)
                ans |= (1 << i);
        cout << ans << '\n';
    }
    return 0;
}

Generated URL:

Output:

No Output
Report Bug