Pagini recente » Cod sursa (job #2763772) | Cod sursa (job #72947) | Cod sursa (job #609052) | Cod sursa (job #3173164) | Cod sursa (job #1522035)
#include <fstream>
#define In "transport.in"
#define Out "transport.out"
#define MAX 2000000000
#define Nmax 16004
using namespace std;
int n, k, sol, a[Nmax], _max;
inline void Read()
{
ifstream f(In);
f >> n >> k;
for(int i = 1;i <= n; ++i)
{
f >> a[i];
_max = max(_max,a[i]);
}
f.close();
}
inline bool Verif(const int capacity)
{
int s,i,cnt;
for(s = 0,i = cnt = 1; i <= n && cnt <= k ;++i)
{
s += a[i];
if(s>capacity)
{
s = a[i];
++cnt;
}
}
return cnt <= k;
}
inline void Binary_search()
{
int Left = _max,Right = MAX, Middle;
while(Left <= Right)
{
Middle = (Left+Right)>>1;
if(Verif(Middle))
{
sol = Middle;
Right = Middle - 1;
}
else
Left = Middle + 1;
}
}
inline void Write()
{
ofstream g(Out);
g<<sol<<"\n";
g.close();
}
int main()
{
Read();
Binary_search();
Write();
return 0;
}