Cod sursa(job #2580070)

Utilizator PatriciaCretoiuCretoiu Patricia PatriciaCretoiu Data 13 martie 2020 11:57:51
Problema Transport Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("transport.in");
ofstream out("transport.out");

const int N = 16005;
int n, k, res, v[N];
int check(int x)
{
    int ans = x, trs = k;

    for(int i = 1; i <= n; i++)
    {
        if(v[i] <= ans)
            ans -= v[i];
        else
        {
            trs--;
            ans = x - v[i];
        }
    }

    return (trs > 0);
}

int main()
{
    ios::sync_with_stdio(false);
    in.tie(0);

    in >> n >> k;

    int sum = 0, maxx = 0;

    for(int i = 1; i <= n; i++)
    {
        in >> v[i];
        sum += v[i];
        maxx = max(maxx, v[i]);
    }

    if(k == 1)
        out << sum << '\n';
    else if(k >= n)
        out << maxx << '\n';
    else
    {
        int st = maxx, dr = N;
        while(st <= dr)
        {
            int mij = st + (dr - st) / 2;
            if(check(mij) == 1)
            {
                res = mij;
                dr = mij - 1;
            }
            else
                st = mij + 1;
        }
    }

    out << res;

    return 0;
}