用map存下所有数的值和次数,然后直接遍历map即可
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#define x first
#define y second
#define int long long
using namespace std;
int t, n, m;
int w[200005];
signed main()
{
cin >> t;
while (t--) {
int mmax = 0;
map<int, int>mp;
cin >> n>>m;
for (int i = 1; i <= n; i++) {
cin >> w[i];
mp[w[i]]++;
}
for (auto tt:mp)
{
int a = min(tt.y, m / tt.x);
int next = tt.x + 1;
int b = 0;
if (mp.count(next))
{
b = mp[next];
}
for (int i = 0; i <= a; i++)
{
int j = (m - i * tt.x) / next;
j = min(j, b);
mmax = max(mmax, i * tt.x + j * next);
}
}
cout << mmax << endl;
}
return 0;
}