Cod sursa(job #3327983)

Utilizator danipapafatherVladescu Daniel danipapafather Data 5 decembrie 2025 19:19:39
Problema Transport Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>

bool check(std::vector<int>& v, long long c, int k) {
    int transporturi = 1;
    long long current_sum = 0;

    for (auto e : v) {
        if (current_sum + e <= c) {
            current_sum += e;
        } else {
            transporturi++;
            current_sum = e;
        }
    }
    return transporturi <= k;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(NULL);
    freopen("transport.in", "r", stdin);
    freopen("transport.out", "w", stdout);

    int n, k;
    std::cin >> n >> k;
    std::vector<int> v(n);
    for (int i = 0; i < n; i++) {
        std::cin >> v[i];
    }

    long long low = *std::max(v.begin(), v.end());
    long long high = std::accumulate(v.begin(), v.end(), 0LL);

    while (low < high) {
        long long mid = (low + high) / 2;

        if (check(v, mid, k)) {
            high = mid;
        } else {
            low = mid + 1;
        }
    }

    std::cout << low << '\n';

    return 0;
}