Cod sursa(job #3274238)

Utilizator AndreiStrugureAndrei AndreiStrugure Data 5 februarie 2025 20:29:30
Problema Transport Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;
ifstream fin("fantastice.in");
ofstream fout("fantastice.out");
#else
#include <bits/stdc++.h>
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
#endif

int n, k, v[16005];
int h, l=16001, mid;

int main()
{
    fin>>n>>k;
    for(int i=1; i<=n; i++){
        fin>>v[i];
        l=min(l, v[i]);
    }
    h=n*16000;
    while(l<h){
        mid=(l+h)/2;
        int cnt=1, s=0;
        for(int i=1; i<=n; i++){
            s+=v[i];
            if(s>mid){
                cnt++;
                s=v[i];
            }
        }
        if(cnt>k){
            l=mid+1;
            //cout<<mid<<" 1\n";
        }else{
            h=mid;
            //cout<<mid<<" 2\n";
        }
    }
    fout<<h;

    return 0;
}