Pagini recente » Cod sursa (job #2461880) | Cod sursa (job #1001370) | Cod sursa (job #656946) | Cod sursa (job #1181) | Cod sursa (job #2831718)
#include <fstream>
#include <vector>
#include <iostream>
using namespace std;
long long caut(int st,long long dr,int k,vector<int>a)
{
long long rez=st^st;
while(st<=dr)
{
long long m=(st+dr)/2;
int x=1;
long long s=0;
for(auto c:a)
{
if(s+(long long)c>m)
{
x++;
s=c;
}
else
{
s+=(long long)c;
}
}
if(x<=k)
{
dr=m-1;
rez=m;
}
else
{
st=m+1;
}
}
return rez;
}
ifstream fin("transport.in");
ofstream fout("transport.out");
int main()
{
int n,k;
long long s=0,maxx;
fin>>n>>k;
vector<int>a(n);
for(int i=0;i<n;i++)
{
fin>>a[i];
s+=(long long)a[i];
maxx=max(maxx,(long long)a[i]);
}
fout<<caut(maxx,s,k,a);
}