I am not getting right answer

#include
using namespace std;

bool student(int a[],int n,int s,int mid)
{
int k=0,i,sum=0,student=1;
for(i=0;i<n;i++)
{
if(sum+a[i]>mid)
{
student++;

       sum=a[i];
       if(student>s)
       return false;
   }

   else 
   sum+=a[i];

}

return true;
}

int main ()
{
int n,s,last,first,sum=0,i,mid,ans;
cin>>n>>s;
int a[n];

for(i=0;i<n;i++)
{
    cin>>a[i];
    sum=sum+a[i];
}

first=0;
last=sum;
while(first<=last)
{
    mid=(first+last)/2;

    if(student(a,n,s,mid))
    {
        ans=min(mid,ans);
        last=mid-1;
    }

    else 
    first=mid+1;

}
cout<<ans;
return 0;

}

hey, one mistake
first should be the maximum elements since that is the minimum pages that can be read
moreover, can u give me code after pasting it on ide.codingblocks.com
it would be easier to debug then
nonetheless,
u can have a look at my submitted code:
https://ide.codingblocks.com/s/223119