Pagini recente » Cod sursa (job #216155) | Cod sursa (job #1912454) | Cod sursa (job #498162) | Cod sursa (job #495115) | Cod sursa (job #1049845)
#include <fstream>
using namespace std;
int main()
{
ifstream fin ("transport.in");
ofstream fout ("transport.out");
int n,k,end=0,start=0,count,p=0,temp,mid=0;
fin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
{
fin>>a[i];
end+=a[i];
if(start<a[i]) start=a[i];
}
while(1)
{
mid=(start+end)/2;
temp=mid;
count=0;
for(int i=0;i<n;i++)
{
temp-=a[i];
if(temp<=0)
{
if(temp<0)temp=mid-a[i];
else temp=mid;
count++;
}
if(temp>=0&&i==n-1) count++;
}
if(count>k)
{
start=mid+1;
}
else if(count==k)
{
p=mid;
end=mid;
}
else if(count<k)
{
end=mid;
}
if(start>=end) break;
}
fout<<p;
}