Pagini recente » Cod sursa (job #2669949) | Cod sursa (job #777439) | Cod sursa (job #880516) | Cod sursa (job #2759087) | Cod sursa (job #2036187)
#include <fstream>
#include <queue>
using namespace std;
ifstream in("transport.in");
ofstream out("transport.out");
queue<int> v;
int n, k, nr;
long long int step, verificationNr, nr_formed, best, total;
const int limit = 256000000;
bool verify(int volume){
queue <int>aux = v;
int cnt = 0;
long long int sum = 0;
for (int i = 1; i <= n; ++ i){
if (aux.front() > volume)
return true;
if (sum + aux.front() <= volume){
sum += aux.front();
}
else{
cnt ++;
sum = aux.front();
}
aux.pop();
}
if (sum > 0)
cnt ++;
return cnt > k;
}
int main(){
best = 256000000;
in >> n;
in >> k;
for (int i = 0; i < n; ++ i){
in >> nr;
total += nr;
v.push(nr);
}
step = 1;
step = step << 32 - __builtin_clz(total) - 1;
while (step > 0){
verificationNr = nr_formed + step;
if (verificationNr <= total && verify(verificationNr)){
nr_formed += step;
best = verificationNr;
}
step = step >> 1;
}
best += 1;
out << best;
return 0;
}