Cod sursa(job #3280152)

Utilizator Carnu_EmilianCarnu Emilian Carnu_Emilian Data 25 februarie 2025 17:33:09
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fcin("transport.in");
ofstream fcout("transport.out");

const int N = 16e3 + 5;
int n, k, maxx;
int v[N];

bool f(int x)
{
    int s, c;
    s = 0;
    c = 1;
    for (int i = 1; i <= n; i++)
    {
        s += v[i];
        if (s > x)
        {
            c++;
            s = v[i];
        }
    }
    return c <= k;
}

int cb()
{
    int st, dr, p, m;
    st = maxx;
    dr = 5e8;
    while (st <= dr)
    {
        m = (st + dr) / 2;
        if (f(m))
        {
            p = m;
            dr = m - 1;
        }
        else
            st = m + 1;
    }
    return p;
}

int main()
{
    fcin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        fcin >> v[i];
        maxx = max(maxx, v[i]);
    }
    fcout << cb();
    return 0;
}