Book allocation problem all text cases are coming out to be wrong

#include
using namespace std;
bool issafe(int *cumarr,int num,int students,int &maxnum,int lastindex)
{
maxnum=0;
int i=-1,j=0;int flag=1;
while(students>0)
{
if(cumarr[j]-cumarr[i]>=num)
{
if(maxnum<=cumarr[j]-cumarr[i])
{
//cout<<"max value updating for “<<num<<” at "<<cumarr[j]-cumarr[i]<<endl;
maxnum=cumarr[j]-cumarr[i];
}
i=j;
j++;
students–;

	}
	else
	j++;
	if(students>0 && j>lastindex)
	{
		flag=0;
		break;
	}
}
if(flag)
return true;
else
return false;

}
int bookdistribution(int *cumarr,int lastindex,int students)
{
int low=0;
int high=cumarr[lastindex];int ans=0;
while(high>=low)
{int maxnum=0;
int num=low+(-low+high)/2;
if(issafe(cumarr,num,students,maxnum,lastindex)) //(int *cumarr,int num,students,int &maxnum,int lastindex)
{
low=num+1;

			ans=maxnum;
		
	}
	else
	high=num-1;
}
return ans;

}
int main()
{
int t;
cin>>t;
while(t–)
{

	int n;
	cin>>n;
	int *arr=new int[n];
	arr[-1]=0;
	int students;
	cin>>students;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		arr[i]=a+arr[i-1];
	}
cout<<	bookdistribution(arr,n-1,students)<<endl;  //(int *cumarr,int lastindex,int students)
}
return 0;

}

hey, it is hard to read the code here, i would recommend for u to post ur code in ide.codingblocks.com
nonetheless,
u can have a look at my submitted code:
https://ide.codingblocks.com/s/223119


please help debug

listing down the problems(since for the correct code u can refer to the link above)

  1. low should be arr[lastindex] this is the minimum no of pages to be read, u cannot divide any book so somebody has to read the largest book
  2. highest is sum of all pages of all the books, what is there was only one student
  3. if the config is safe, u need to find a minimum better ans not larger, so there high will go to mid-1 not low to mid+1, correspondingly the else will change
    4)if(cumarr[j]-cumarr[i]>=num), i is initialised at -1, so wrong indexing
  4. the logic inside the if statement makes no sense to me whatsoever, corr to the problem
    what u have to do is keep on adding pages until mid, if the number is greater than mid, increment the num of students needed, if ultimately no of students needed is greater than number of students available the config is not valid otherwise it is that is all