Help me in solving DIVIDE_GROUP problem

I have used binary search search to the problem and logic is correct but showing wrong answer.
My code:
include <bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t–){
long long n,k;
cin>>n>>k;
vectora(n);
for(int i=0;i<n;i++) cin>>a[i];
long long i=0,j=accumulate(a.begin(),a.end(),0)/k;
long long ans=0;
while(i<=j){
long long m=(i+j)/2;
long long e=0;
for(int c=0;c<n;c++) e+=min(a[c],m);
if(e>=(m*k)) ans=m,i=m+1;
else j=m-1;
}
cout<<ans<<endl;
}
return 0;
}
Problem link:DIVIDE_GROUP Problem - CodeChef