#include
#include
using namespace std;
int sum_array(int a[],int n,int t)
{
int sum=0;
for(int i=0;i<n;i++)
sum+=a[i]*t;
return sum;
}
bool check(int a[],int time,int n,int k,int t)
{
int current_time=0;
int painter_used=1;
for(int i=0;i<n;i++)
{
current_time+=a[i]t;
if(current_time>time)
{
current_time=a[i]t;
painter_used++;
}
}
if(painter_used<=k)
return true;
else
return false;
}
int min_time(int a[],int n,int k,int t)
{
sort(a,a+n);
int start=ta[n-1];
int end=sum_array(a,n,t);
int mid,ans;
while(start<=end)
{
mid=(start+end)/2;
bool x=check(a,mid,n,k,t);
if(x)
{
ans=mid;
end=mid-1;
}
else
start=mid+1;
}
return ans;
}
int main()
{
int n,k,t;
cin>>n>>k>>t;
int a=new int[n];
for(int i=0;i<n;i++)
cin>>a[i];
int ans=min_time(a,n,k,t);
cout<<(ans%10000003);
delete []a;
return 0;
}
Please tell the problem with the code
@namangarg31
you cant sort the array.also the start of binary search will be the max element of array but find it during taking input of the array
but why is it necessary to find it during taking input,sorry i did not get your point