Cod sursa(job #2610918)

Utilizator alexradu04Radu Alexandru alexradu04 Data 5 mai 2020 21:25:43
Problema Grupuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
	
#include <fstream>
 
using namespace std;
ifstream fin("grupuri.in");
ofstream fout("grupuri.out");
int n,k;
long long s;
int m[100005];
int check(int x)
{
    int i,l,c;
    l=0;
    c=0;
    for(i=1;i<=n;i++)
    {
        if(m[i]<x)
        {
            c+=m[i];
        }
        else c+=x;
        if(c>=x)
        {
            l++;
            c=c-x;

        }
    }
    if(l>=k) return 1;
    else return 0;
}
int cautbin()
{
    long long right,left,mid,sol;
    left=1;
    right=s/k;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(check(mid))
        {
            sol=mid;
            left=mid+1;
        }
        else
        {
            right=mid-1;
        }
    }
    return sol;
}

int main()
{
    int i,z,zmax=0;
    fin>>k>>n;
    for(i=1;i<=n;i++)
    {
        fin>>z;
        if(z>zmax) zmax=z;
        s+=z;
        m[i]=z;
    }
    fout<<cautbin();
    return 0;
}