Pagini recente » Cod sursa (job #1674125) | Cod sursa (job #1012429) | Cod sursa (job #939479) | Cod sursa (job #849677) | Cod sursa (job #641986)
Cod sursa(job #641986)
# include <cstdio>
# include <algorithm>
using namespace std;
int n, k, i, j, st, dr, sum, aux, aux1, ct, mij, aux2;
int a[16005];
int main()
{
freopen("transport.in","r",stdin);
freopen("transport.out","w",stdout);
scanf("%d%d",&n,&k);
for (i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
sum += a[i];
}
st = 1; dr = sum; aux = sum;
while (ct != k)
{
ct = 1; aux1 = (st + dr) / 2;
aux2 = aux1;
for (i = 1; i <= n; i++)
if (a[i] <= aux1) aux1 -= a[i];
else
{
ct++;
aux1 = aux2;
aux1 -= a[i];
}
if (ct == k) break;
if (ct > k) st = aux2 + 1;
else dr = aux2 - 1;
}
for (i = aux2; ct == k; --i)
{
ct = 1; aux1 = i;
for (j = 1; j <= n; j++)
if (a[j] <= aux1) aux1 -= a[j];
else
{
ct++;
aux1 = i;
aux1 -= a[j];
}
if (ct != k) aux2 = i + 1;
}
printf("%d", aux2);
return 0;
}