Cod sursa(job #2653159)

Utilizator CraiuAndrei Craiu Craiu Data 27 septembrie 2020 11:01:43
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ifstream fin("transport.in");
ofstream fout("transport.out");
int n, k, v[16001];
bool ok(int vol)
{
        int s = 0, cnt = 1;
        for (int i = 1; i <= n; ++i)
        {
                s += v[i];
                if (s > vol)
                        ++cnt, s = v[i];
        }
        if (k < cnt) return false;
        return true;
}
int main()
{
        ios::sync_with_stdio(false);
        fin.tie();

        fin >> n >> k;
        int maxx = -1;
        int suma = 0;
        for (int i = 1; i <= n; ++i)
        {
                fin >> v[i];
                maxx = max(maxx, v[i]);
                suma += v[i];
        }
        int s = maxx, d = suma, m, rez;
        while (s <= d)
        {
                m = (s + d) / 2;
                if (ok(m)) d = m - 1, rez = m;
                else s = m + 1;
        }
        fout << rez;

        return 0;
}