Pagini recente » Cod sursa (job #2728696) | Cod sursa (job #2586002) | Cod sursa (job #2775537)
#include <fstream>
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
int n, k, saltele[16001], maxim, sum;
bool Corect(int mij){
int sumElm = 0, i = 1, t = 1;
for(int i = 1; i <= n; ++i){
sumElm += saltele[i];
if(sumElm > mij){
sumElm = saltele[i];
t ++;
if(t > k)
return false;
}
}
return t <= k;
}
int CautBin(){
int st = maxim, dr = sum, rez;
while(st <= dr){
int mij = (st + dr) / 2;
if(Corect(mij))
rez = mij, dr = mij - 1;
else
st = mij + 1;
}
return rez;
}
int main(){
fin >> n >> k;
for(int i = 1; i <= n; ++i){
fin >> saltele[i];
maxim = max(saltele[i], maxim);
sum += saltele[i];
}
fout << CautBin();
fin.close();
fout.close();
return 0;
}