-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVA11413.cpp
62 lines (61 loc) · 1.02 KB
/
UVA11413.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
59
60
61
62
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll a[1007];
ll n;
ll m;
bool getvalue(int l)
{
int i,ans=1;
int v=0;
for(i=0;i<n;i++)
{
if(v+a[i]<=l){
v+=a[i];
}
else {
ans++;
v=a[i];
}
}
if(ans>m) return false;
else return true;
}
int binarysearch(int n,int ma)
{
int l=ma,mid,ans;
int h=n;
while(l<=h)
{
mid=(l+h)/2;
bool v=getvalue(mid);
if(getvalue(mid))
{
ans=mid;
h=mid-1;
}
else
{
l=mid+1;
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int i,j,d;
ll ma=0;
while(cin>>n>>m){
ma=0;
ll sum=0;
for(j=0;j<n;j++){
cin>>a[j];
sum+=a[j];
if(a[j]>ma) ma=a[j];
}
ll v=binarysearch(sum,ma);
printf("%d\n",v);
}
}