Cod sursa(job #2623736)
Utilizator | Data | 3 iunie 2020 17:26:04 | |
---|---|---|---|
Problema | Transport | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva de probleme | Marime | 0.91 kb |
#include <fstream>
using namespace std;
ifstream f("transport.in");
ofstream g("transport.out");
int main()
{
int n, k;
int a[16005];
int i, s, cmax, st, dr, mij, suma, cnt, sol;
f >> n >> k;
s = cmax = 0;
for(i = 1; i <= n; i++)
{
f >> a[i];
s += a[i];
cmax = max(a[i], cmax);
}
st = cmax;
dr = s;
while(st <= dr)
{
mij = (st + dr) / 2;
suma = 0;
cnt = 1;
for(i = 1; i <= n; i++)
{
suma += a[i];
if(suma > mij)
{
suma = a[i];
cnt++;
}
}
if(cnt > k) st = mij + 1;
else
{
sol = mij;
dr = mij - 1;
}
}
g << sol;
return 0;
}