Pagini recente » Cod sursa (job #887832) | Cod sursa (job #3230041) | Cod sursa (job #1765105) | Cod sursa (job #1811327) | Cod sursa (job #2066358)
#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);
if (s != 0)
t++;
if (t <= k)
{g << max;
gasit = 1;}
else max++;
} while (gasit == 0);
f.close();
g.close();
return 0;
}