Book allocation problem?

pls. go through my code. can’t find what is wrong

#include <bits/stdc++.h>
#define ll long long
#define endl “\n”
using namespace std;

bool check(ll a[],ll m,ll n,ll k)
{
ll i=0,s=0,c=1;
for(i=0;i<n;i++)
{
if(s+a[i]>m)
{
c++;
s=a[i];
if(c>k) return false;
}
else{
s+=a[i];
}
}
return true;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;cin>>t;
while(t–)
{
ll n,k;cin>>n>>k;
ll a[n],s,e=0;
for(ll i=0;i<n;i++) { cin>>a[i]; e+=a[i]; }
s=0;
ll ans=e;
// cout<<e<<endl;
while(s<=e)
{
ll m=(s+e)/2; //cout<<m<<" “;
if(check(a,m,n,k))
{ // cout<<0<<” “;
ans=min(ans,m);
e=m-1;
}
else { //cout<<1<<” ";
s=m+1;
}
}
cout<<ans<<endl;

}
return 0;

}

@mehulbhandari358, please send your code using ide.codingblocks.com or any other ide so it will be easier for me to debug

can you pls copy paste it ? i don’t have the code anymore

@mehulbhandari358, you have not considered the case when a[i] is greater than m ,in that case you are increasing the count but your function should return false
refer this :- https://ide.codingblocks.com/s/247468

okay ! got it.
Can you tell me how to know if binary search is to be applied or not in a problem ?