Cod sursa(job #1589776)

Utilizator lraduRadu Lucut lradu Data 4 februarie 2016 13:35:29
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#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;
}