Book alocation problem

question https://hack.codingblocks.com/contests/c/512/1263
#include
using namespace std;
#define ll long long int
bool valid(ll books[],ll n,ll k,ll ans){
ll sum=0;
ll student =1;
for(int i=0;i<n;i++){
if(sum + books[i]>ans){
sum = books [i];
student ++;
if(student>k){
return false;
}
}
else{
sum += books[i];
}
}
return true;
}
int main(){
int t;
cin>>t;
ll books[10000005];
ll k;
for (int i=0;i<t;i++){
int n;
cin>>n;
cin>>k;
for (int i=0;i<n;i++){
cin>>books[i];
}
ll sumofbooks =0;
ll s=0;
ll e=0;
for (int i=0;i<n;i++){
sumofbooks += books[i];
}
s =books[0];
e = sumofbooks;
ll res =0;

while(s<=e){
    ll mid = (s+e)/2;
         if(valid(books,n,k,mid)){
             res = mid;
             e = mid -1;
         }
         else{
             s = mid+1;
         }
}
cout<<res<<endl;
 }

return 0;

}
showing wrong ans;

Hi Sarthak, you just need to add one more condition in the valid function.
if(books[i]>ans)
return false;
Rest of the code works fine.
You can refer to the code here https://ide.codingblocks.com/s/51610

1 Like