Cod sursa(job #3327741)

Utilizator danipapafatherVladescu Daniel danipapafather Data 4 decembrie 2025 22:48:52
Problema Transport Scor 70
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>

bool check(std::vector<int>& v, int c, int k) {
    int transporturi = 1;
    int 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];
    }

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

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

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

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

    return 0;
}