Pagini recente » Cod sursa (job #571772) | Cod sursa (job #1378274) | Cod sursa (job #2961257) | Cod sursa (job #1125010) | Cod sursa (job #2786454)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in ("transport.in");
ofstream out ("transport.out");
int numarare (int x, int n2, vector<int> z) {
int ans = 0;
for (int y = 1; y <= n2; y ++) {
if (z[y] < x && z[y] != 0) {
int S = z[y], l = y;
while (S <= x) {
l ++;
S += z[l];
}
S -= z[l];
l --;
for (int y2 = y; y2 <= l; y2 ++)
z[y2] = 0;
ans++;
}
else
if (z[y] == x)
ans ++;
}
return ans;
}
int main()
{
int n, k, Max, s = 0;
in>> n >> k;
vector<int> v(n + 1, 0);
in>> v[1];
Max = v[1];
s += v[1];
for (int i = 2; i <= n; i++) {
in>> v[i];
if (v[i] > Max)
Max = v[i];
s += v[i];
}
int c1 = Max, c2 = s, m;
while (c2 - c1 > 1) {
m = (c1 + c2) / 2;
if (k >= numarare(m, n, v))
c2 = m;
else
c1 = m;
}
if (numarare(c1, n, v) == k)
out<< c1;
else
out<< c2;
return 0;
}