Whats wrong in the approach

#include
#include
#include
using namespace std;
bool time(int a[],int k,int n,int mid)
{
int boards_painted=0;
int painter=1;
for(int i=0;i<n;i++)
{
if(boards_painted+a[i]>mid)
{
painter++;
boards_painted=boards_painted+a[i];
if(painter>k)
{
return false;
}
}
else
{
boards_painted=boards_painted+a[i];
}
}
return true;
}
int main()
{
int t,s,sum=0;
cin>>t;
while(t–)
{
int k,n;
int a[1000];
cin>>k;
cin>>n;

	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	sort(a,a+n);
	s=a[n-1];
   	for(int i=0;i<n;i++)
	{
	sum+=a[i];
	}
int e=sum;
int ans=INT_MAX;
while(s<=e)
{
	int mid=(s+e)/2;
	if(time(a,k,n,mid))
	{
		ans=min(ans,mid);
		e=mid-1;
    }
    else
    {
	s=mid+1;
    }
}
cout<<ans;

}
return 0;
}