Pagini recente » Cod sursa (job #1715425) | Cod sursa (job #1497357) | Cod sursa (job #2173428) | Cod sursa (job #1294077) | Cod sursa (job #1507869)
#include<fstream>
#define LM 16008
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
int V[LM],N,K,Left,Right,Mid;
int check(int als)
{
int s=0,c=0,i;
for(i=1;i<=N;++i)
{
if(s+V[i]<=als) s+=V[i];
else { s=0; s+=V[i]; c++; }
}
return (c+1<=K);
}
int main()
{
int i,Sol;
Left=1; Right=LM*LM;
fin>>N>>K;
for(i=1;i<=N;++i)
{
fin>>V[i];
}
while(Left<=Right)
{
Mid=(Left+Right)/2;
if(check(Mid)) { Right=Mid-1; Sol=Mid; }
else Left=Mid+1;
}
fout<<Sol<<"\n";
fin.close();
fout.close();
return 0;
}