Pagini recente » Cod sursa (job #2550812) | Cod sursa (job #3280152)
#include <bits/stdc++.h>
using namespace std;
ifstream fcin("transport.in");
ofstream fcout("transport.out");
const int N = 16e3 + 5;
int n, k, maxx;
int v[N];
bool f(int x)
{
int s, c;
s = 0;
c = 1;
for (int i = 1; i <= n; i++)
{
s += v[i];
if (s > x)
{
c++;
s = v[i];
}
}
return c <= k;
}
int cb()
{
int st, dr, p, m;
st = maxx;
dr = 5e8;
while (st <= dr)
{
m = (st + dr) / 2;
if (f(m))
{
p = m;
dr = m - 1;
}
else
st = m + 1;
}
return p;
}
int main()
{
fcin >> n >> k;
for (int i = 1; i <= n; i++)
{
fcin >> v[i];
maxx = max(maxx, v[i]);
}
fcout << cb();
return 0;
}