Code giving wrong answer for book allocation problem

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int isval(ll a[],ll n,ll k,ll m)
{
ll std=1,cp=0;
for(int i=0;i<n;i++)
{
if(cp+a[i]<=m)
cp+=a[i];
else
{
std++;
//cp=0;
if(std>k)
return 0;
}

}
return 1;

}
ll bs(ll a[],ll n,ll k)
{
ll su=0;
for(ll i=0;i<n;i++)
su+=a[i];
ll s=0;ll e=su,m=0;
//ll ans=a[n-1];
while(s<=e)
{
m=ceil((s+e)/2);
if(isval(a,n,k,m))
e=m-1;
else
s=m+1;

}
return m;

}
int main() {

ll t,n;
cin>>t;
while(t--)
{
	ll k;
	cin>>n>>k;
	ll a[n];
	for(ll i=0;i<n;i++)
		cin>>a[i];
	cout<<bs(a,n,k)<<"\n";
}
return 0;

}