Pagini recente » Cod sursa (job #3236095) | Cod sursa (job #962370) | Cod sursa (job #2827853) | Cod sursa (job #1723332) | Cod sursa (job #2067749)
#include <fstream>
using namespace std;
int n,k,v[16001],nmax,s;
ifstream fin("transport.in");
ofstream fout("transport.out");
int calc(int nr)
{
int pasi=1,aux=nr;
for (int i=1; i<=n; i++)
{
if (aux>=v[i]) aux-=v[i];
else {pasi++; aux=nr-v[i];}
}
if (pasi>k) return 2;
else if (pasi<k) return 1;
else return 0;
}
int main()
{
fin>>n>>k;
int ps=0,pd=0,mid=0,c=0,sol=0;
bool gasit=false;
for (int i=1; i<=n; i++)
{
fin>>v[i];
if (v[i]>nmax) nmax=v[i];
s+=v[i];
}
ps=nmax; pd=s;
while (ps<=pd)
{
mid=(ps+pd)/2;
c=calc(mid);
if (gasit && c!=0) break;
if (c==2) ps=mid+1;
else if (c==1) pd=mid-1;
else {gasit=true; sol=mid; pd=pd-1;}
}
fout<<sol;
return 0;
}