Pagini recente » Cod sursa (job #63627) | Cod sursa (job #1075895) | Cod sursa (job #1185786) | Cod sursa (job #2489397) | Cod sursa (job #3257366)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("transport.in");
ofstream g("transport.out");
int N, K;
int v[16001];
bool possible(int capacity) {
int trips = 0;
long long current_load = 0;
for (int i = 1; i <= N; i++) {
if (v[i] > capacity) {
return false;
}
if (current_load + v[i] <= capacity) {
current_load += v[i];
} else {
trips++;
current_load = v[i];
}
}
if (current_load > 0) {
trips++;
}
return trips <= K;
}
int main() {
int max=-1;
long long sum=0;
f>>N>>K;
for(int i = 1; i<=N; i++) {
f >> v[i];
if (v[i] > max) {
max = v[i];
}
sum += v[i];
}
int left = max;
int right = sum;
while(left < right){
int mid = left + (right-left)/2;
if(possible(mid)){
right = mid;
}
else{
left = mid+1;
}
}
g<<left;
return 0;
}