Cod sursa(job #908614)

Utilizator FayedStratulat Alexandru Fayed Data 9 martie 2013 20:42:34
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#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;
}