Pagini recente » Cod sursa (job #388406) | Cod sursa (job #2529728) | Statistici Ungureanu Cosmin (cosmincelrau11) | Cod sursa (job #1879790) | Cod sursa (job #2537265)
#include <fstream>
#include <string>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
using namespace std;
#ifdef DEBUG
string name = "data";
#else
string name = "transport";
#endif
ifstream fin(name + ".in");
ofstream fout(name + ".out");
int n, k;
int a[16005];
int maxv = 0;
int transport(int v) {
if (v < maxv) {
return INF;
}
int t = 1;
int s = 0;
for (int i = 0; i < n; ++i) {
s += a[i];
if (s + a[i + 1] > v) {
t++;
s = 0;
}
}
return t;
}
int find(int low, int high) {
if (low < maxv) {
return INF;
}
int mid = (low + high) / 2;
auto t = transport(mid);
if (t <= k && transport(mid - 1) > k) {
return mid;
} else if (t == k) {
return find(low, mid - 1);
}
if (t < k) {
return find(low, mid - 1);
}
return find(mid + 1, high);
}
int main() {
fin >> n >> k;
for (int i = 0; i < n; ++i) {
fin >> a[i];
maxv = max(maxv, a[i]);
}
fout << find(maxv, maxv * n);
}