Pagini recente » Cod sursa (job #735092) | Cod sursa (job #2032063) | Cod sursa (job #1280464) | Cod sursa (job #143614) | Cod sursa (job #1363574)
#include <iostream>
#include <fstream>
using namespace std;
int a[16001],n,k;
int verifica(int marime)
{
int nr=1,acum=0,i;
for (i=1;i<=n;i++)
{
if (acum+a[i]<=marime)
{
acum+=a[i];
}
else
{
acum=a[i];
nr++;
}
}
if (nr<=k)
{
return true;
}
else
{
return false;
}
}
int main()
{
ifstream in("transport.in");
ofstream out("transport.out");
int m=0,p,x=0,i;
in>>n;
in>>k;
for (i=1;i<=n;i++)
{
in>>a[i];
m+=a[i];
if (a[i]>x)
{
x=a[i];
}
}
int y=m;
int mij=(x+y)/2;
while (x<=y)
{
if (verifica(mij))
{
p=mij;
y=mij-1;
mij=(x+y)/2;
}
else
{
x=mij+1;
mij=(x+y)/2;
}
}
out<<p;
}