forked from sunnyshahabuddin/Coding-Ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookaloocation.cpp
More file actions
67 lines (60 loc) · 801 Bytes
/
bookaloocation.cpp
File metadata and controls
67 lines (60 loc) · 801 Bytes
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
59
60
61
62
63
64
65
66
67
#include<iostream>
using namespace std;
bool succes(int a[],int m,int book,int student)
{
int pageread=0;
int count=1;
for(int i=0;i<book;i++)
{
if(pageread+a[i]<=m)
{
pageread=pageread+a[i];
}
else{
count++;
if(count>student)
{
return false;
}
pageread=a[i];
}
}
return true;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int b;
int student;
cin>>b;
cin>>student;
int p[b];
int tps=0;
for(int i=0;i<b;i++)
{
cin>>p[i];
tps=tps+p[i];
}
int s=p[b-1];
int e=tps;
int ans;
while(s<=e)
{
int m=(s+e)/2;
// cout<<m<<endl;
if(succes(p,m,b,student))
{
ans=m;
e=m-1;
}
else
{
s=m+1;
}
}
cout<<ans<<endl;
}
}