Pagini recente » Cod sursa (job #1135001) | Cod sursa (job #1265032) | Cod sursa (job #2370487) | Cod sursa (job #857799) | Cod sursa (job #2890097)
#include <bits/stdc++.h>
using namespace std;
int count_transports(const int capacity, const vector<int> &v)
{
int ans = 1, current = 0;
for (const int &x : v)
if (current + x <= capacity)
current += x;
else {
current = x;
++ans;
}
return ans;
}
int main()
{
ifstream fin("transport.in");
ofstream fout("transport.out");
int n, k;
fin >> n >> k;
vector<int> v(n);
for (int &x : v)
fin >> x;
int step, i, mn = *max_element(v.begin(), v.end()), mx = accumulate(v.begin(), v.end(), 0);
for (step = 1; step <= mx; step <<= 1);
for (i = mx; step; step >>= 1)
if (i - step >= mn && count_transports(i - step, v) <= k)
i -= step;
fout << i << '\n';
}