Pagini recente » Cod sursa (job #1564775) | Cod sursa (job #3167155) | Cod sursa (job #3037515) | Cod sursa (job #2506674) | Cod sursa (job #908614)
Cod sursa(job #908614)
#include <cstdio>
#define NMAX 16001
using namespace std;
int N,k,V[NMAX],Maxim;
long Smax;
inline void citesc(){
freopen("transport.in","r",stdin);
freopen("transport.out","w",stdout);
scanf("%d%d",&N,&k);
for(register int i=1;i<=N;++i){
scanf("%d",&V[i]),Smax+=V[i];
if(V[i] > Maxim)
Maxim = V[i];
}
}
inline long scot(long Sm){
long S=0;
int j=1;
for(register int i=1;i<=N;++i){
if(S + V[i] > Sm){
S = 0;
j++;
if(j > k) return 0;
}
S+=V[i];
}
return 1;
}
inline long binary(long x,long y){
long S=0,mijloc;
mijloc = (x+y)/2;
if(x == y)
return x;
if(scot(mijloc)) binary(x,mijloc);
else binary(mijloc+1,y);
}
inline void solve(){
if(k>=N) printf("%d",Maxim);
else if(k == 1) printf("%ld",Smax);
else printf("%ld",binary(Maxim,Smax));
}
int main(){
citesc();
solve();
return 0;
}