Pagini recente » Cod sursa (job #2874229) | Cod sursa (job #448224) | Cod sursa (job #571886) | Monitorul de evaluare | Cod sursa (job #1574330)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int n, k, ans;
vector<int> v;
bool good(int c) {
int routes = 0;
int i = 0;
while (i < n) {
int s = 0;
while (i < n && s + v[i] <= c) {
s += v[i];
++i;
}
++routes;
if (routes > k) {
return false;
}
}
return true;
}
int main()
{
ifstream cin("transport.in");
ofstream cout("transport.out");
cin >> n >> k;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v.push_back(x);
}
int lo = 1, hi = 16000 * 16000;
while (hi - lo > 1) {
int mid = lo + (hi - lo) / 2;
if (good(mid)) {
hi = mid;
ans = mid;
} else {
lo = mid;
}
}
cout << hi;
return 0;
}