Pagini recente » Cod sursa (job #417898) | Cod sursa (job #181160) | Cod sursa (job #1877741) | Cod sursa (job #178558) | Cod sursa (job #2050948)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
long n,k,i,q1,mijl,l1,l2,q,max1,a[16001],tr,poz;
int main()
{
fin>>n>>k;
for (i=1;i<=n;i++)
{
fin>>a[i];
q=q+a[i];
if (a[i]>max1)
max1=a[i];
}
l1=max1;
l2=q;
while (l1<=l2)
{
mijl=(l1+l2)/2;
q1=0;
tr=0;
for (i=1;i<=n;i++)
{
if (a[i]+q1<=mijl)
q1=q1+a[i];
else
{
tr++;
if (a[i]>mijl)
{
tr++;
q1=0;
}
else
q1=a[i];
}
}
if (a[n]+a[n-1]<=mijl)
tr++;
if (tr>k)
l1=mijl+1;
else
{
poz=mijl;
l2=mijl-1;
}
}
fout<<poz;
return 0;
}