Book Allocation Program

#include
using namespace std;
int PagesAllocate(int a[],int mid,int students,int n)
{
int i,last=0,c=1;
for(i=0;i<n;i++)
{
if((last+a[i])<=mid)
last+=a[i];
else
{
last=a[i];
c++;
}
}
if(c==students)
return 1;
return 0;
}
void MinimizePages(int a[],int s,int e,int n,int students,int *ans)
{
if(s>e)
return;
int mid=(s+e)/2;
int k=PagesAllocate(a,mid,students,n);
if(k)
{
*ans=mid;
MinimizePages(a,s,mid-1,n,students,ans);
}
else
MinimizePages(a,mid+1,e,n,students,ans);
}
int main()
{
int t,j;
cin>>t;
for(j=0;j<t;j++)
{
int n,students;
cin>>n>>students;
int pages[n],i,sum=0;
int ans=0;
for(i=0;i<n;i++)
{
cin>>pages[i];
sum+=pages[i];
}
if(students>n)
cout<<"-1"<<endl;
else
{
MinimizePages(pages,pages[n-1],sum,n,students,&ans);
cout<<ans<<endl;
}
}
return 0;
}

what is wrong with my program?

hello @agarwal.priyanshu98
please save ur code here - https://ide.codingblocks.com/
and share link with me.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.