Cod sursa(job #2552223)

Utilizator PatriciaCretoiuCretoiu Patricia PatriciaCretoiu Data 20 februarie 2020 18:02:10
Problema Transport Scor 90
Compilator cpp-64 Status done
Runda nusemaipoate Marime 0.98 kb
#include <fstream>

using namespace std;

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

const int N = 16000;
int n, i, k, sum, maxx, mij;
int a[N+2], st=1, dr=N * N + 2;

int verif(int val)
{
    int x=0, drum=0;
    for(int i=1; i<=n; i++)
    {
        x += a[i];
        if (x > val)
            drum += 1, x = a[i];
        else if(x == val)
            drum += 1, x = 0;
    }
    if(x != 0)
        drum += 1;

    if (drum <= k)
        return 1;
    else
        return 0;
}

int main()
{
    in >> n >> k;
    for (i=1; i<=n; i++)
    {
        in >> a[i];
        sum += a[i];
        maxx = max(maxx, a[i]);
    }
    if(k == 1)
        out << sum;
    else if(k >= n)
        out << maxx;
    else
    {
        while(st<dr)
        {
            mij = (st + dr)/2;
            if(verif(mij))
                dr = mij;
            else
                st = mij + 1;
        }
        out << dr;
    }
    return 0;
}