Pagini recente » Cod sursa (job #523241) | Cod sursa (job #2495978) | Cod sursa (job #1262088) | Borderou de evaluare (job #2703669) | Cod sursa (job #211367)
Cod sursa(job #211367)
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int N, K, Sum;
vector <int> V;
int oK(int capacity)
{
vector <int> :: iterator it;
int t = 1, s = 0;
for ( it = V.begin(); it != V.end(); ++it )
{
if(s + *it <= capacity)
s += *it;
else
{
s = *it;
++ t;
}
if(t > K) return 0;
}
return 1;
}
void ReadData()
{
freopen("transport.in", "rt", stdin);
int x, i;
for ( scanf("%d %d", &N, &K), i = 1; i <= N; ++i )
{
scanf("%d", &x); Sum += x;
V.push_back(x);
}
fclose(stdin);
}
int Solve()
{
int step, i;
for ( step = 1; step <= Sum; step<<=1 );
for ( i = 0; step; step>>=1 )
if( !oK(i + step) ) i += step;
return i;
}
int main()
{
ReadData();
freopen("transport.out", "wt", stdout);
printf("%d", Solve() + 1);
fclose(stdout);
return 0;
}