-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRATAS.cpp
58 lines (49 loc) · 1.04 KB
/
PRATAS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
bool isPossible(int p,int cook[],int n,int mid){
int cnt = 0;
for(int i=0;i<n;i++){
//for each cook count the pratas
int c = 0;
int time = mid;
int perpratas = cook[i]; // time taken to cook each pratas by ith cook
while(time > 0){
time = time - perpratas;
if( time >= 0){
c += 1;
perpratas += cook[i];
}
}
cnt += c;
if(cnt >= p)
return true;
}
return false;
}
int minTime(int p,int cook[],int n){
int s = 0, e = 10000007;
while(s<=e){
int mid = (s+e)/2;
if(isPossible(p,cook,n,mid)){
e = mid - 1;
}else{
s = mid + 1;
}
}
return s;
}
int main(){
int t;
cin>>t;
while(t--){
int p;
cin>>p;
int n;
cin>>n;
int cook[n];
for(int i=0;i<n;i++)
cin>>cook[i];
cout<<minTime(p,cook,n)<<endl;
}
return 0;
}