please tell where my code is incorrect as the test case is not getting passed
code is as follows:-
#include
using namespace std;
bool isvalid(int books[],int n,int m,int mid){
int pages=0;
int students=1;
for(int i=0;i<n;i++){
if(pages+books[i]>mid){
pages=books[i];
students++;
if(students>m){
return false;
}
}
else{
pages+=books[i];
}
}
return true;
}
int main() {
int t;
cin>>t;
for(int y=0;y<t;y++){
int n,m;
cin>>n>>m;
// initialisation and declaration of the array named books
int books[n];
for(int i=0;i<n;i++){
cin>>books[i];
}
// to calc sum of pages
int sumPages=0;
for(int i=0;i<n;i++){
sumPages+=books[i];
}
int s=0;
int e=sumPages;
int finalans=0;
// binary search
while(s<=e){
int mid=(s+e)/2;
if(isvalid(books,n,m,mid)){
finalans=mid;
e=mid-1;
}
else{
s=mid+1;
}
}
cout<<finalans<<endl;
}
return 0;
}