Note that we do not need to consider the numbers x ≥ k x≥k x≥k, we are only interested in the remainder of the division of x x x by k k k, and we simply add the value ⌊ x k \frac{x}{k} kx⌋ to the answer.
We get an array
a
a
a, where
a
i
<
k
a_i
#include
using namespace std;
int main()
{
int T;cin>>T;
while(T--)
{
int n,k;cin>>n>>k;
multiset<int> b;
long long ans=0;
for(int i=0;i<n;i++)
{
int x;cin>>x;
ans+=x/k;
b.insert(x%k);
}
while(!b.empty())
{
int x=*b.begin();
b.erase(b.begin());
auto it=b.lower_bound(k-x);
if(it!=b.end())
{
b.erase(it);
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}