Pagini recente » Cod sursa (job #1847890) | Cod sursa (job #1808894) | Cod sursa (job #1256262) | Cod sursa (job #1746657) | Cod sursa (job #1002983)
#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];
inline void Read()
{
ifstream f(In);
f >> n >> k;
for(int i = 1;i <= n; ++i)
f >> 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 = 1,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;
}