Pagini recente » Cod sursa (job #418456) | Cod sursa (job #597075) | Cod sursa (job #2422354) | Cod sursa (job #1400092) | Cod sursa (job #1358413)
# include <fstream>
using namespace std;
# define DIM 16001
ifstream in("transport.in");
ofstream out("transport.out");
int x[DIM];
int main()
{
int n,m,i,p,u,mij,k,maxi=0,s=0,nr,r;
in>>n>>k;
for (i=1;i<=n;i++)
{
in>>x[i];
if (x[i]>maxi) maxi=x[i];
s+=x[i];
}
p=maxi;
u=s;
while (p<=u)
{
mij=(p+u)/2;
m=0;
nr=0;
for (i=1;i<=n;i++)
{
m+=x[i];
if (m>mij)
{
nr++;
m=x[i];
}
}
if (nr<=k)
{
u=mij-1;
r=mij;
}
else
{
p=mij+1;
r=mij+1;
}
}
out<<r;
return 0;
}