Painter Problem: SIr what is the error

#include<bits/stdc++.h>
using namespace std;

bool is_Answer(int * a,int n,int k,int ans){
int sum=0;
int no_of_painters=0;
for(int i=0;i<n;i++){
if(sum+a[i]<=ans){
sum+=a[i];
}
else{
no_of_painters++;
if(no_of_painters>k)
return false;
sum=a[i];
}
}
if(sum){
no_of_painters++;
if(no_of_painters>k)
return false;
}
return true;
}

int main() {
int k;
int n;
cin>>k>>n;
int a[n],sum=0,Max=0;
for(int i=0;i<n;i++){
cin>>a[i];
Max=max(Max,a[i]);
sum+=a[i];
}
int si=Max,ei=sum;
int ans=0;
while(si<=ei){
int mid=(si+ei)/2;
if(is_Answer(a,n,k,mid)){
ans=mid;
ei=mid-1;
}
else
si=mid+1;
}
cout<<ans<<endl;
return 0;
}


take type as long long to avoid overflow!!
Mark doubt as resolved if clear, else ask your query.

sir i am getting 5 as the output for the sample input but actually 50 should be there.

please can you check where it is wrong???

Submit it over here:
https://hack.codingblocks.com/app/practice/1/1057/problem
The code I updated gets accepted here!

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.