Painters problem

why this code is giving wrong answer in test case:
4
2
1
10
20
30
40

as answer should be 60 but code is giving 40 as ans.
also it is giving 2 test cases failed.

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

bool ansIsRight(ll a[],ll n,ll k,ll t,ll mid)
{
ll sum=0;
ll painter=k;
for(ll i=0;i<n;i++)
{
sum+=(a[i]*t);
if(sum>mid)
{
k–;
sum=(a[i]*t);
}
}
t–;
if(k>=0 && sum<=mid)
{
return true;
}
else
{
return false;
}
}

int main()
{
ll n;
cin>>n;
ll k;
cin>>k;
ll t;
cin>>t;
ll a[n];
ll sum=0;
ll max_val=0;
for(ll i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
max_val=max(max_val,a[i]);
}
if(k>=n)
{
cout<<(max_valt)%10000003<<endl;
}
else
{
ll s=max_val
t;
ll e=sumt;
ll ans=sum
t;
while(s<=e)
{
ll mid=(s+e)/2;
if(ansIsRight(a,n,k,t,mid))
{
ans=mid;
e=mid-1;
}
else
{
s=mid+1;
}
}
cout<<ans%10000003<<endl;
}

return 0;

}

hello @brinda_dabhi

pls go to this link -> https://ide.codingblocks.com/

paste ur code in the editor and press ctrl + s and then save

a url will be generated in ur search bar share that url with me

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.

check now->