Pagini recente » Cod sursa (job #2571873) | Cod sursa (job #1278795) | Cod sursa (job #3126978) | Cod sursa (job #2754063) | Cod sursa (job #1504107)
#include <iostream>
using namespace std;
int a[30], i, n, k;
int check(int x)
{int c=0;
i=1;
while(i<=n)
{
int s=0;
int ok=1;
while(s<=x && ok) if(s+a[i]<=x) s=s+a[i], i++;
else ok=0;
c++;
}
if(c>k) return 0;
return 1;
}
int main()
{int max=0, sum=0, n1, n2, b[30], cap=0;
cout<<"n=";
cin>>n;
cout<<"k=";
cin>>k;
for(i=1; i<=n; i++)
{cin>>a[i];
if(a[i]>max) max=a[i];
sum=sum+a[i];}
i=max;
int j=1;
while(i<=sum) b[j]=i, i++, j++;
int l=j-1;
j=1;
int ok=0;
while(j<=l&& !ok)
{
int m =(j+l)/2;
if(check(b[m]) && !check(b[m-1])) cap=b[m], ok=1;
else if(check(b[m])) l=m;
else j=m;
}
cout<<cap;
}