Pagini recente » Cod sursa (job #2558258) | Cod sursa (job #575117) | Cod sursa (job #1101700) | Cod sursa (job #617090) | Cod sursa (job #283481)
Cod sursa(job #283481)
#include <cstdio>
#define FIN "transport.in"
#define FOUT "transport.out"
#define N 16010
int n, k, sum, v[N], c;
void read()
{
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i)
{
scanf("%d", &v[i]);
sum += v[i];
}
}
int calc(int x)
{
int s = 0, t = 0;
for (int i = 1; i <= n; ++i)
{
if (s + v[i] > x)
{
++t;
s = 0;
}
s += v[i];
}
return t + 1;
}
void binarysearch()
{
int p, u = sum, m, x;
p = (sum % k) ? sum / k + 1 : sum / k;
while (p < u)
{
m = (p + u) / 2;
x = calc(m);
if (x < k)
{
c = m;
u = m - 1;
}
else if (x > k)
p = m;
else
p = u = c = m;
}
while (calc(p - 1) <= k)
-- p;
c = p;
}
int main()
{
read();
binarysearch();
printf("%d\n", c);
}