poj Monthly Expense

时间:2023-03-10 03:06:54
poj Monthly Expense

http://poj.org/problem?id=3273

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 100010
using namespace std; int n,s;
int a[maxn]; int main()
{
while(scanf("%d%d",&n,&s)!=EOF){
int high=,low=;
for(int i=; i<n; i++)
{
scanf("%d",&a[i]);
high+=a[i];
low=max(low,a[i]);
}
int mid;
while(low<=high)
{
mid=(high+low)/;
int ans=,sum=;
for(int i=; i<n; i++)
{
sum+=a[i];
if(sum>mid)
{
ans++;
sum=a[i];
}
}
if(sum!=) ans++;
if(ans>s)
{
low=mid+;
}
else high=mid-;
}
printf("%d\n",mid);
}
return ;
}