Cod sursa(job #1874261)

Utilizator LeVladzCiuperceanu Vlad LeVladz Data 9 februarie 2017 20:32:43
Problema Transport Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>

using namespace std;

ifstream fin("transport.in");
ofstream fout("transport.out");

int n,k,i,a[17000],minim,st,dr,mid,maxim,s,t,ok,max1,maxi;

int main()
{
    fin >> n >> k;
    for (i=1; i<=n; i++)
    {
        fin >> a[i];
        if (a[i] > maxim)
            maxim = a[i];
        s += a[i];
    }
    st = maxim;
    dr = s;
    s = 0;
    while (st <= dr)
    {
        mid = (st+dr)/2;
        i = 1;
        t = 0;
        max1 = 0;
        while (i <= n)
        {
            s = 0;
            while (s <= mid && i <= n)
            {
                s += a[i];
                i++;
            }
            if (i == n+1)
            {
                if (s > max1)
                    max1 = s;
                t++;
                break;
            }
            i--;
            s -= a[i];
            if (s > max1)
                max1 = s;
            t++;
        }
        if (t <= k)
        {
            dr = mid-1;
            maxi = max1;
        }
        else
            st = mid+1;
    }
    fout << maxi;
    return 0;
}