Whats wrong in this

#include
using namespace std;
bool isPossible(long int a[],long int N,long int K,long int T,long int mid)
{
if(K>=N)
{
if(mid-(T*a[N-1])>=0)
{
return true;
}
}
else
{
mid=mid-(a[K-1]*T);
long int d=N-K;
for(long int i=d+1;i<N;i++)
{

        mid=mid-(a[i]*T);
    }
    if(mid>=0)
    {
        return true;
    }
}
return false;

}
int min_time(long int a[],long int N,long int K,long int T)
{
long int sum=0;
for(int i=0;i<N;i++)
{
sum+=a[i];
}
long int s=a[N-1]T;
long int e=sum
T;
long int ans;
while(s<=e)
{
long int mid=(s+e)/2;
if(isPossible(a,N,K,T,mid))
{
ans=mid;
e=mid-1;
}
else
{
s=mid+1;
}
}
return ans%(10000003);
}
int main()
{
long int N,K,T;
cin>>N>>K>>T;
long int a[N];
for(long int i=0;i<N;i++)
{
cin>>a[i];
}
cout<<min_time(a,N,K,T);
}

hello @guptanikhil898
a) array is not sorted so u cannot use a[n-1] for maximum value of array.
b) your is possible function is incorrect becuase it is possible that one painter can paint multiple continuos fences.

pls refer this code->

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.