Pagini recente » Cod sursa (job #2875842) | Cod sursa (job #2229737) | Cod sursa (job #1161785) | Cod sursa (job #2319128) | Cod sursa (job #2446796)
#include <fstream>
#include <iostream>
std::ifstream fin{"transport.in"};
std::ofstream fout{"transport.out"};
int N, K, v[16001];
bool test(int capacity){
int counter{1}, current_capacity{capacity};
for(int i = 1; i <= N; i++){
if(current_capacity < v[i]){
current_capacity = capacity;
counter++;
if(counter > K) return false;
}
current_capacity -= v[i];
}
return true;
}
int binarySearch(int first, int last){
int capacity, step;
for(step = 1; step < last; step = (step << 1));
for(capacity = last; step; step = (step >> 1))
if(capacity - step >= first)
if(test(capacity - step)) capacity -= step;
return capacity;
}
int main()
{
fin >> N >> K;
int max{1}, sum{0};
for(int i = 1; i <= N; i++){
fin >> v[i];
sum += v[i];
max = std::max(max, v[i]);
}
fout << binarySearch(max, sum);
}