Pagini recente » Cod sursa (job #3200772) | Cod sursa (job #3197534) | Cod sursa (job #45555) | Cod sursa (job #3136518) | Cod sursa (job #1589776)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <math.h>
#include <string>
#include <vector>
#include <stdio.h>
#include <queue>
#define LL long long
#define pb push_back
#define lm 16005
#define MOD 1000000007
using namespace std;
int v[lm],s,Sum,m,n,rez,k;
bool check(int x)
{
int i,sum=0,t=1;
for(i=1;i<=n;i++)
if(sum+v[i]>x)
{
sum=0;
t++;
i--;
if(t>k)
return 0;
}
else sum+=v[i];
return 1;
}
int main()
{
freopen("transport.in", "r", stdin);
freopen("transport.out","w",stdout);
scanf("%d %d",&n,&k);
for(int i=1; i<=n; i++)
{
scanf("%d",&v[i]);
Sum+=v[i];
}
s=1;
while(s<=Sum)
{
m=(s+Sum)>>1;
//printf("\n%d ",m);
if(check(m))
{
rez=m;
Sum=m-1;
//printf("%d",rez);
}
else s=m+1;
}
printf("%d",rez);
return 0;
}