Cod sursa(job #2266934)

Utilizator TeddyDinutaDinuta Eduard Stefan TeddyDinuta Data 22 octombrie 2018 23:03:37
Problema Transport Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <bits/stdc++.h>
using namespace std;
int n,k,a[17000],l,r,m,poz,curr,ans,s;
int main()
{
    freopen("transport.in","r",stdin);
    freopen("transport.out","w",stdout);
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    l=1;
    r=330000000;
    while(l<=r)
    {
        m=(l+r)/2;
        s=0;
        curr=1;
        for(int i=1;i<=n;i++)
        {
           if(s+a[i]<=m) {
             s+=a[i];
           }
           else
           {
               s=a[i];
               curr++;
           }
        }
        if(curr<=k)
        {
            r=m-1;
            ans=m;
        }
        else l=m+1;
    }
    cout<<ans;
    return 0;

}