#include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
bool isvalid(int a[],int c,int n,int mid)
{ int total_pratha=0;
for (int i = 0; i < c; i++)
{ int time=0;
int j=1;
while (time<=mid )
{
time+=a[i]*j;
j++;
}
total_pratha+=(j-1);
}
return total_pratha>=n;
}
int main(){
int n;
cin>>n;
int c;
cin>>c;
int a[n];
for (int i = 0; i < c; i++)
{
cin>>a[i];
}
sort(a,a+c);
int start=0;
int end=0;
for (int i = 1; i <=n; i++)
{
end=end+(a[0]*i);
}
int ans=INT_MAX;
while (start<=end)
{
int mid=(start+end)/2;
if (isvalid(a,c,n,mid))
{
ans=min(ans,mid);
end=mid-1;
}
else
{
start=mid+1;
}
}
cout<<ans;
return 0;
}
What is wrong with my code
Have debugged your code here, and have also added comments for better understanding.
If this solves your doubt, please mark it as resolved.
hey please enlighten this more
(j * (j + 1)a[i]) <= 2mid
This is to handle this case that, A cook with a rank R can cook 1 prata in the first R minutes 1 more prata in the next 2R minutes, 1 more prata in 3R minutes and so on(he can only cook a complete prata)