Pagini recente » Cod sursa (job #762113) | Cod sursa (job #504149) | Cod sursa (job #2940024) | Cod sursa (job #123997) | Cod sursa (job #2066373)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int n, k, x[16001], max = 0, i, s = 0, t = 0, gasit = 0, j, z=0;
ifstream f("transport.in");
ofstream g("transport.out");
f>> n >> k;
for (i = 1; i <= n; i++)
{
f >> x[i];
if (x[i] > max)
max = x[i];
}
do{ t = 0; gasit = 0; s = 0;
i = 1;
do
{
while (s < max && i <= n )
s = s + x[i++];
if (s == max)
{
t++;
s = 0;
}
else if(s > max){t++;
s = x[i-1];
}
else if (s < max && i==n)t++;
}while(i <= n && t <= k);
if (s != 0)
t++;
if (t <= k)
{g << max;
gasit = 1;}
else max++;
} while (gasit == 0);
f.close();
g.close();
return 0;
}