Pagini recente » Haba Matei Ionut | Cod sursa (job #1124146) | Cod sursa (job #2836995) | Cod sursa (job #1301595) | Cod sursa (job #3311863)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ifstream fin("transport.in");
ofstream fout("transport.out");
const int NMAX = 16000;
int n, k;
int a[NMAX + 1];
bool check(int c) {
int sum = 0, cnt = 1;
for(int i = 1; i <= n; i++) {
if(a[i] > c) {
return false;
}
sum += a[i];
if(sum > c) {
cnt++;
sum = a[i];
}
}
return cnt <= k;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
fin >> n >> k;
for(int i = 1; i <= n; i++) {
fin >> a[i];
}
int left = 1, right = 1e9, answer = -1;
while(left <= right) {
int mid = (left + right) / 2;
if(check(mid)) {
answer = mid;
right = mid - 1;
}
else {
left = mid + 1;
}
}
fout << answer << '\n';
return 0;
}